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

Optional and Union types break automatic API Docs #55

Open
JoshieJay opened this issue Sep 6, 2024 · 3 comments
Open

Optional and Union types break automatic API Docs #55

JoshieJay opened this issue Sep 6, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@JoshieJay
Copy link

JoshieJay commented Sep 6, 2024

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.

@smt5541
Copy link
Collaborator

smt5541 commented Sep 6, 2024

Thanks for reporting this! I'll look into it when I get home :)

@smt5541 smt5541 self-assigned this Sep 6, 2024
@smt5541 smt5541 added the bug Something isn't working label Sep 6, 2024
@smt5541
Copy link
Collaborator

smt5541 commented Sep 9, 2024

Sorry, this weekend was busier than expected - I'll take a look sometime this week

@smt5541
Copy link
Collaborator

smt5541 commented Oct 4, 2024

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:
pip freeze output:

asgiref==3.8.1
attrs==24.2.0
blinker==1.8.2
click==8.1.7
Flask==3.0.3
Flask-Parameter-Validation==2.4.0
itsdangerous==2.2.0
Jinja2==3.1.4
jsonschema==4.23.0
jsonschema-specifications==2023.12.1
MarkupSafe==2.1.5
python-dateutil==2.9.0.post0
referencing==0.35.1
rpds-py==0.20.0
six==1.16.0
Werkzeug==3.0.4

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>"

Resulting documentation:
image

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

When branches are created from issues, their pull requests are automatically linked.

2 participants