Fix json_schema() dropping constraints for a Schema nested in a Schema - #354
Open
gaoflow wants to merge 1 commit into
Open
Fix json_schema() dropping constraints for a Schema nested in a Schema#354gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
json_schema() rendered a Schema nested directly inside another Schema --
e.g. Const, or Schema(Schema(...)) -- as an empty {} schema, which matches
anything and so contradicts what validate() enforces. _priority() classifies
a Schema instance as VALIDATOR, and the only VALIDATOR branch handled Regex,
so any other nested Schema fell through to the no-op default and was dropped.
Recurse into the nested Schema and merge its generated schema, so
Const("x") -> {"const": "x"}, Const(int) -> {"type": "integer"} and
Schema(Schema(int)) -> {"type": "integer"}.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
json_schema()renders aSchemanested directly inside anotherSchemaas an empty{}schema, which matches anything — silently dropping the constraint and contradicting whatvalidate()enforces.So the generated JSON schema disagrees with the validator on a representable construct. The same happens for any directly-nested schema, e.g.
Schema(Schema(int)).json_schema(...)→{}instead of{"type": "integer"}.Const("fixed").json_schema(...)andSchema("fixed").json_schema(...)both work — the loss only happens when theSchema/Constis wrapped in anotherSchema.Root cause
In
Schema.json_schema,_priority()classifies aSchemainstance asVALIDATOR, and the onlyVALIDATORbranch in the generator handlesRegex:validate()unwraps a nested schema (s = s.schema), butjson_schema()never recurses into it, so every non-RegexnestedSchemafalls through to the no-op default.Fix
Recurse into a nested
Schemaand merge its generated schema instead of dropping it:Or/And/Regexare notSchemasubclasses, so their existing branches are unaffected; only nestedSchemainstances (Const,Schema(Schema(...)), …) now expand.Tests / verification
Added
test_json_schema_const_and_nested_schemacoveringConst(literal)→const,Const(type)→type,Schema(Schema(int))→type, andConstas a dict value. The new test fails onmasterand passes with the fix. Full suite incl. the README doctests is green (121 passed);ruff/ruff-formatclean; no newmypyfindings.