-
Notifications
You must be signed in to change notification settings - Fork 12
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
Optional and Union types break automatic API Docs #55
Labels
bug
Something isn't working
Comments
Thanks for reporting this! I'll look into it when I get home :) |
Sorry, this weekend was busier than expected - I'll take a look sometime this week |
Apologies again for the delay, finally got a chance to look at this today and am having issues reproducing the issue on Python 3.12:
Code Tested: from flask import Flask
from flask_parameter_validation import ValidateParameters
from flask_parameter_validation.parameter_types import Query, Json
from flask_parameter_validation.docs_blueprint import docs_blueprint
from typing import Optional, Union
app = Flask(__name__)
app.register_blueprint(docs_blueprint)
@app.route("/")
@ValidateParameters()
def hello_world():
return "Hello, World!"
@app.get("/optional")
@ValidateParameters()
def optional(q: Optional[int] = Query()):
return f"<strong>{q}</strong>"
@app.post("/union")
@ValidateParameters()
def union(j: Union[int, float] = Json()):
return f"<strong>{j}</strong>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using an Optional or Union type with the @ValidateParameters decorator works for validation purposes, but it causes a 500 error when accessing the /docs route.
It looks like it may be this function that is causing the issue.
def get_arg_type_hint(fdocs, arg_name): """ Extract the type hint for a specific argument. """ arg_type = fdocs["argspec"].annotations[arg_name] if hasattr(arg_type, "__args__"): return ( f"{arg_type.__name__}[{', '.join([a.__name__ for a in arg_type.__args__])}]" ) return arg_type.__name__
Specifically the
arg_type.__name__
seems to not exist for Optional and Union types.It looks like there is a note about Optional parameters in 2.3.0 (https://github.com/Ge0rg3/flask-parameter-validation/releases/tag/v2.3.0), but I think this may just be for validation.
The text was updated successfully, but these errors were encountered: