def test_field_with_examples() -> None:
class SampleModel(BaseModel):
field: str = Field(default="default", examples=["example1", "example2"])
part_api = construct_sample_api(SampleModel)
api = construct_open_api_with_schema_class(part_api)
assert api.components is not None
assert api.components.schemas is not None
if PYDANTIC_V2:
json_api: Any = api.model_dump(mode="json", by_alias=True, exclude_none=True)
else:
json_api: Any = api.dict(by_alias=True, exclude_none=True)
validate(json_api)
As a simple fix, we currently replace this line in our fork:
When using a Pydantic
Field
withexamples
in a request or response model, the generated OpenAPI 3.0.x spec is invalid. See #45 for details.Here is a test case that can be added to https://github.com/mike-oakley/openapi-pydantic/blob/main/tests/v3_0/test_validated_schema.py:
As a simple fix, we currently replace this line in our fork: