Skip to content

Commit

Permalink
Fix JSON schema representation when there are extra keys (pydantic#3896)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukancagatay committed Dec 22, 2022
1 parent 1a1497e commit 4edfe28
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pydantic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def field_schema(
)

# $ref will only be returned when there are no schema_overrides
if '$ref' in f_schema:
if '$ref' in f_schema and not schema_overrides:
return f_schema, f_definitions, f_nested_models
else:
s.update(f_schema)
Expand Down Expand Up @@ -818,7 +818,7 @@ def get_schema_ref(name: str, ref_prefix: Optional[str], ref_template: str, sche
schema_ref = {'$ref': ref_prefix + name}
else:
schema_ref = {'$ref': ref_template.format(model=name)}
return {'allOf': [schema_ref]} if schema_overrides else schema_ref
return schema_ref


def field_singleton_schema( # noqa: C901 (ignore complexity)
Expand Down
16 changes: 7 additions & 9 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ class Model(BaseModel):
},
'properties': {
'pikalias': {
'allOf': [{'$ref': '#/definitions/FooBarEnum'}],
'$ref': '#/definitions/FooBarEnum',
'description': 'Pika is definitely the best!',
'title': 'Pikapika!',
},
'bulbialias': {
'allOf': [{'$ref': '#/definitions/FooBarEnum'}],
'$ref': '#/definitions/FooBarEnum',
'description': 'Bulbi is not...',
'title': 'Bulbibulbi!',
'default': 'foo',
Expand Down Expand Up @@ -387,12 +387,12 @@ class Foo(BaseModel):
'enum': {'$ref': '#/definitions/Names'},
'model': {'$ref': '#/definitions/Pika'},
'titled_enum': {
'allOf': [{'$ref': '#/definitions/Names'}],
'$ref': '#/definitions/Names',
'description': 'Description of enum',
'title': 'Title of enum',
},
'titled_model': {
'allOf': [{'$ref': '#/definitions/Pika'}],
'$ref': '#/definitions/Pika',
'description': 'Description of model',
'title': 'Title of model',
},
Expand Down Expand Up @@ -424,7 +424,7 @@ class Foo(BaseModel):
},
'properties': {
'enum': {'$ref': '#/definitions/Names'},
'extra_enum': {'allOf': [{'$ref': '#/definitions/Names'}], 'extra': 'Extra field'},
'extra_enum': {'$ref': '#/definitions/Names', 'extra': 'Extra field'},
},
'required': ['enum', 'extra_enum'],
'title': 'Foo',
Expand Down Expand Up @@ -1273,7 +1273,7 @@ class Model(BaseModel):
'Bar': {
'title': 'Bar',
'type': 'object',
'properties': {'b': {'title': 'B', 'default': {'a': 'foo'}, 'allOf': [{'$ref': '#/definitions/Foo'}]}},
'properties': {'b': {'title': 'B', 'default': {'a': 'foo'}, '$ref': '#/definitions/Foo'}},
},
'Baz': {'title': 'Baz', 'type': 'object', 'properties': {'c': {'$ref': '#/definitions/Bar'}}},
},
Expand Down Expand Up @@ -1547,9 +1547,7 @@ class Outer(BaseModel):
'type': 'object',
}
},
'properties': {
'inner': {'allOf': [{'$ref': '#/definitions/Inner'}], 'default': {'a': {'.': ''}}, 'title': 'Inner'}
},
'properties': {'inner': {'$ref': '#/definitions/Inner', 'default': {'a': {'.': ''}}, 'title': 'Inner'}},
'title': 'Outer',
'type': 'object',
}
Expand Down

0 comments on commit 4edfe28

Please sign in to comment.