Remove extra allOf keys in schema (#1438)

resolves #1209
This commit is contained in:
Mostapha Sadeghipour Roudsari
2020-04-30 13:56:11 -04:00
committed by GitHub
parent 028f9bc96f
commit a4adf892dc
3 changed files with 22 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Remove extra `allOf` from schema for fields with `Union` and custom `Field`.
+5
View File
@@ -559,6 +559,11 @@ def field_singleton_sub_fields_schema(
known_models=known_models,
)
definitions.update(sub_definitions)
if schema_overrides and 'allOf' in sub_schema:
# if the sub_field is a referenced schema we only need the referenced
# object. Otherwise we will end up with several allOf inside anyOf.
# See https://github.com/samuelcolvin/pydantic/issues/1209
sub_schema = sub_schema['allOf'][0]
sub_field_schemas.append(sub_schema)
nested_models.update(sub_nested_models)
return {'anyOf': sub_field_schemas}, definitions, nested_models
+16
View File
@@ -960,6 +960,22 @@ def test_schema_overrides():
}
def test_schema_overrides_w_union():
class Foo(BaseModel):
pass
class Bar(BaseModel):
pass
class Model_1(BaseModel):
a: Union[Foo, Bar]
class Model_2(BaseModel):
a: Union[Foo, Bar] = Field(...)
assert Model_1.schema()['properties'] == Model_2.schema()['properties']
def test_schema_from_models():
class Foo(BaseModel):
a: str