Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: invalid OpenAPI 3.0.x spec for Pydantic fields with examples #51

Open
kolditz-senec opened this issue Nov 25, 2024 · 0 comments
Open

Comments

@kolditz-senec
Copy link

kolditz-senec commented Nov 25, 2024

When using a Pydantic Field with examples 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:

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:

-    example: Optional[Any] = None
+    examples: Optional[Any] = Field(default=None, alias="example")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant