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

Setting an example value for a field in response object generates deprecation warnings with pydantic >= 2.5 #176

Open
guillaume-alvarez opened this issue Sep 13, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@guillaume-alvarez
Copy link

guillaume-alvarez commented Sep 13, 2024

Environment:

  • Python version: 3.12.3
  • Operating system: Ubuntu
  • Flask version: 2.3.3
  • flask-openapi3 version: 3.1.3

Warning

If I define a model setting Field.example (not defined in pydantic):

class MyResponse(BaseModel):
    stream_id: str = Field(..., example="some_stream")

then it causes warnings because it is not officially supported by pydantic:

PydanticDeprecatedSince20: Using extra keyword arguments on `Field` is deprecated and will be removed. Use `json_schema_extra` instead. (Extra keys: 'example'). Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.9/migration/

Error

If I define a model setting the Field.examples as advised by pydantic:

class MyResponse(BaseModel):
    stream_id: str = Field(..., examples="some_stream")

Then the examples value does not appear in the resulting OpenAPI schema.

This field was added in pydantic 2.5.0 by pydantic/pydantic#8013

According to OpenAPI 3.1.0 specs, the use of example is deprecated:

Deprecated: The example property has been deprecated in favor of the JSON Schema examples keyword. Use of example is discouraged, and later versions of this specification may remove it.

Debugging the code I see that the 'examples' value is present in the json schema generated by pydantic but removed by flask_openapi3 when converting to the Schema model. It only defines an example and no examples.

I think the simplest fix would be to add a Schema.examples field in flask_openapi3/models/schema.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant