-
Notifications
You must be signed in to change notification settings - Fork 19
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
[Feature] - Support Pydantic 2 and remove support for Pydantic V1 #70
base: main
Are you sure you want to change the base?
Conversation
664b414
to
a8c6f69
Compare
…t provide a response for a path, remove bypass logic
ab6158f
to
a749a3a
Compare
36187dc
to
c51a3f2
Compare
This would be really awesome! Running into the issue that I can't use this package without downgrading Pydantic to <2 |
@robyoung , is it realistic to expect this to be merged any time soon and result in a new version? |
Sorry, this totally dropped off my radar. |
@robyoung , any idea when this will be available on pypi? |
@robyoung , what is needed for this PR to be merged and deployed to pypi? |
@@ -18,10 +15,9 @@ class Config: | |||
def __init__(self, **kwargs: Dict[str, Any]) -> None: | |||
self.PATH: str = "apidoc" | |||
self.FILENAME: str = "openapi.json" | |||
self.OPENAPI_VERSION: str = "3.0.3" | |||
self.OPENAPI_VERSION: str = "3.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverting it to self.OPENAPI_VERSION: str = "3.0.3"
resolves the issue for me.
Please merge |
Hello, when trying to test this branch I found a couple of issues.
Steps to reproduce...Add the following dummy route to @app.route('/api/test-error', methods=['POST'])
@api.validate(body=Data, resp=Response('HTTP_200'))
def test_error():
return "PASS" Call the route without passing a Resulting exception: * Serving Flask app 'flask_demo'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:8000
Press CTRL+C to quit
[2025-02-18 17:36:50,449] ERROR in app: Exception on /api/test-error [POST]
Traceback (most recent call last):
File "/Users/kedvall/dev/flask-pydantic-spec/.venv/lib/python3.10/site-packages/flask/app.py", line 1511, in wsgi_app
response = self.full_dispatch_request()
File "/Users/kedvall/dev/flask-pydantic-spec/.venv/lib/python3.10/site-packages/flask/app.py", line 919, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/kedvall/dev/flask-pydantic-spec/.venv/lib/python3.10/site-packages/flask/app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/kedvall/dev/flask-pydantic-spec/.venv/lib/python3.10/site-packages/flask/app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "/Users/kedvall/dev/flask-pydantic-spec/flask_pydantic_spec/spec.py", line 129, in sync_validate
return self.backend.validate(
File "/Users/kedvall/dev/flask-pydantic-spec/flask_pydantic_spec/flask_backend.py", line 200, in validate
before(request, response, req_validation_error, None)
File "/Users/kedvall/dev/flask-pydantic-spec/flask_pydantic_spec/utils.py", line 186, in default_before_handler
"spectree_model": req_validation_error.model.__name__,
AttributeError: 'pydantic_core._pydantic_core.ValidationError' object has no attribute 'model'
127.0.0.1 - - [18/Feb/2025 17:36:50] "POST /api/test-error HTTP/1.1" 500 - I think this can be fixed via changing def default_before_handler(
req: Request, resp: Response, req_validation_error: Any, instance: BaseModel
) -> None:
"""
default handler called before the endpoint function after the request validation
:param req: request provided by the web framework
:param resp: response generated by Flask_Pydantic_Spec that will be returned
if the validation error is not None
:param req_validation_error: request validation error
:param instance: class instance if the endpoint function is a class method
"""
if req_validation_error:
logger.info(
"Validation Error",
extra={
"spectree_model": req_validation_error.__class__.__name__, # .model --> .__class__
"spectree_validation": req_validation_error.errors(),
},
)
def default_after_handler(
req: Request, resp: Response, resp_validation_error: Any, instance: BaseModel
) -> None:
"""
default handler called after the response validation
:param req: request provided by the web framework
:param resp: response from the endpoint function (if there is no validation error)
or response validation error
:param resp_validation_error: response validation error
:param instance: class instance if the endpoint function is a class method
"""
if resp_validation_error:
logger.info(
"500 Response Validation Error",
extra={
"spectree_model": resp_validation_error.__class__.__name__, # .model --> .__class__
"spectree_validation": resp_validation_error.errors(),
},
)
class Data(BaseModel):
uid: str
limit: int = 5
vip: bool
class Config:
json_schema_extra = { # schema_extra --> json_schema_extra
'example': {
'uid': 'very_important_user',
'limit': 10,
'vip': True,
}
} |
This PR updates Flask-Pydantic-Spec to only support Pydantic V2. After this PR is merged, only Pydantic V2 will be supported by Flask-Pydantic-Spec.
As part of this upgrade, we have decided to fully target OpenAPI 3.1 as the output of the library. As such, there are a few changes to keep in mind.
Flask-Pydantic-Spec
will now raise an error if there is no response defined for a view.[
and]
is not allowed in the name of the schema, so we will replace these character with_
.