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

In docs, DelimitedList field doesn't produce the expected query parameter format #416

Open
pamelafox opened this issue Mar 20, 2023 · 2 comments

Comments

@pamelafox
Copy link
Contributor

pamelafox commented Mar 20, 2023

I am using the DelimitedList field like so:

class PieChartParams(Schema):
    title = String(required=False, validate=Length(0, 30))
    values = DelimitedList(Number(validate=Range(min=0, max=3.4028235e38)), required=True)
    labels = DelimitedList(String)

And I am using it for the query parameters:

@app.get("/charts/pie")
@app.input(PieChartParams, location="query")
@app.output(ImageOutSchema, content_type='image/png')
def pie_chart(data: dict):
    fig = Figure()
    axes: Axes = fig.add_subplot()
    axes.pie(data["values"], labels=data.get("labels"))
    axes.set_title(data.get("title"))
    return send_figure_as_png(fig)

That field lets me send a query like so:

charts/pie?title=Enrolled%20students&labels=C,Ruby,Java,Python&values=10,20,15,30.5

However, if I enable the APIFlask docs and add multiple values to the DelimitedList fields, the generated URL is instead like this:

charts/pie?title=Enrolled%2520students&values=10&values=20&values=30&values=15&labels=C&labels=Ruby&labels=Java&labels=Python

That query parameter is repeating the fields instead of putting them as comma separated values. That isn't what the parser expects, apparently, as the values and labels come in to APIFlask as lists with single values (the first of each parameter).

For full code, see:
https://github.com/pamelafox/flask-charts-api-container-app/blob/main/src/api/__init__.py

Environment:

  • Python version: 3.11
  • Flask version: 2.2.3
  • APIFlask version: 1.3.0

Docs screenshot here:
Screenshot of APIFlask docs for DelimitedList field

@pamelafox pamelafox added the bug Something isn't working label Mar 20, 2023
@tkzt
Copy link
Contributor

tkzt commented Mar 22, 2023

@pamelafox Hi there. After some source code reading, I found the query parameters generating behavior above is a default behavior to List field in marshmallow, which seems can't be customized even though there are many parameter serializing ways.

Hope it helpful to you :/

@greyli greyli added this to To do in APIFlask 2.0 Jul 15, 2023
@greyli greyli added this to the 2.0.0 milestone Jul 18, 2023
@greyli greyli removed this from To do in APIFlask 2.0 Jul 26, 2023
@greyli greyli added the apispec label Jul 26, 2023
@greyli greyli modified the milestones: 2.0.0, 2.1.0 Jul 26, 2023
@greyli
Copy link
Member

greyli commented Jul 26, 2023

Thanks tkzt for the explanation. After some investigation, I think it needs to be fixed in apispec. It shouldn't set the param explode option to true. Instead, it should set the correct param style and explode for DelimitedList. At least, the explode and style should be allowed to pass in the Field(metadata={}).

I will try to create an issue/PR to apispec later. Moved this issue to the 2.1.0 milestone.

Ref:

@greyli greyli added upstream-bug and removed bug Something isn't working labels Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Todo
Development

No branches or pull requests

3 participants