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

AssertionError: invalid pydantic.BaseModel when using pydantic.BaseModel subclass in spectree.Response #382

Closed
kenichi-cg opened this issue Nov 14, 2024 · 2 comments · Fixed by #387
Labels
bug Something isn't working

Comments

@kenichi-cg
Copy link

Describe the bug

When specifying a subclass of pydantic.BaseModel in spectree.Response, an AssertionError: invalid pydantic.BaseModel is raised. This issue seems to be caused by the assertion at line 87 in spectree/response.py, which checks the model using issubclass(model, BaseModel). Despite importing BaseModel from spectree._pydantic, the assertion does not use the is_base_model() function from spectree._pydantic, which would correctly identify pydantic.BaseModel subclasses.

To Reproduce

  1. Create a Flask application with pydantic and spectree.
  2. Define a response model using pydantic.BaseModel.
  3. Use the response model in spectree.Response.
  4. Run the Flask application and make a request to the endpoint.

Example code:

from flask import Flask, jsonify
from pydantic import BaseModel, Field
from spectree import Response, SpecTree

class MyResponse(BaseModel):
    message: str = Field(..., description="response message")

app = Flask(__name__)
spec = SpecTree('flask')

@app.route('/hello', methods=['GET'])
@spec.validate(resp=Response(HTTP_200=MyResponse))
def hello():
    return jsonify(message='hello world')

if __name__ == '__main__':
    spec.register(app)
    app.run(debug=True)

Expected behavior

The response model should be validated correctly without raising an AssertionError.

The spectree version

  • SpecTree: 1.2.11
  • Flask: 3.0.2
  • pydantic: 2.9.2

Additional context

The issue seems to be caused by the assertion at line 87 in spectree/response.py, which checks the model using issubclass(model, BaseModel). A potential fix could be to adjust the assertion to use the is_base_model() function from spectree._pydantic to correctly identify pydantic.BaseModel subclasses.

@kenichi-cg kenichi-cg added the bug Something isn't working label Nov 14, 2024
@kemingy
Copy link
Member

kemingy commented Nov 14, 2024

Related to #329

So far, it only support v1, let me check if we can support both.

@kemingy
Copy link
Member

kemingy commented Nov 24, 2024

Hi @kenichi-cg you can try it with v1.4.1. It should work now.

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

Successfully merging a pull request may close this issue.

2 participants