Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger/flask: how to handle query parameters with dashes

I have generated a flask server with Swagger/OpenAPI, using an YAML definition file. An extract of a GET endpoint:

 parameters:
    - in: query
      name: project-name
      required: true
      schema:
        type: string
      description: name of the project
    - in: query
      name: file-name
      required: true
      schema:
        type: string
      description: name of the origin file of the dataset
    - in: query
      name: null-management
      required: true
      schema:
        type: string
      description: value to use to fill null values in datasets
    - in: query
      name: category-name
      schema:
        type: string
      description: name of the first level category
    - in: query
      name: filter-multi-occurrence
      schema:
        type: boolean
        description: if multi occurrences are to be filtered or not

the problem is that I cannot map it with Python/Flask controller, because the variables should be like this

def dataset_add(project_name: str, file_name: str, null_management: str, first_level_category_name: str = None,
            filter_multi_occurrence: str = None) -> str:  # noqa: E501

... and dashes are not allowed in Python's variable names. But I don't wanna use underscores in GET query... how can I solve this problem in Flask?

like image 907
Lore Avatar asked Oct 17 '25 08:10

Lore


1 Answers

You can actually do that but using a workaround.

First you need to update the swagger/swagger.yaml in the generated code and make sure all the param names have - instead of _

Then you need to update your function and remove the _ from the parameter name

def dataset_add(projectname: str, filename: str, nullmanagement: str, firstlevelcategoryname: str = None,
            filtermultioccurrence: str = None) -> str:  # noqa: E501

Working fine

like image 143
Tarun Lalwani Avatar answered Oct 19 '25 21:10

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!