We have a discriminator field type
which we want to hide from the Swagger UI docs:
class Foo(BDCBaseModel):
type: Literal["Foo"] = Field("Foo", exclude=True)
Name: str
class Bar(BDCBaseModel):
type: Literal["Bar"] = Field("Bar", exclude=True)
Name: str
class Demo(BDCBaseModel):
example: Union[Foo, Bar] = Field(discriminator="type")
The following router:
@router.post("/demo")
async def demo(
foo: Foo,
):
demo = Demo(example=foo)
return demo
And this is shown in the Swagger docs:
We don't want the user to see the type field as it is useless for him/her anyways.
We tried making the field private: _type
which hides it from the docs but then it cannot be used as discriminator anymore:
class Demo(BDCBaseModel):
File "pydantic\main.py", line 205, in pydantic.main.ModelMetaclass.__new__
File "pydantic\fields.py", line 491, in pydantic.fields.ModelField.infer
File "pydantic\fields.py", line 421, in pydantic.fields.ModelField.__init__
File "pydantic\fields.py", line 537, in pydantic.fields.ModelField.prepare
File "pydantic\fields.py", line 639, in pydantic.fields.ModelField._type_analysis
File "pydantic\fields.py", line 753, in pydantic.fields.ModelField.prepare_discriminated_union_sub_fields
File "pydantic\utils.py", line 739, in pydantic.utils.get_discriminator_alias_and_values
pydantic.errors.ConfigError: Model 'Foo' needs a discriminator field for key '_type'