swagger-api / swagger-api/swagger-codegen

[python] multipart-form with multiple properties generates a None for non-binary properties in python-flask

Open
#9,696 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Multipart-form with multiple properties generates a None for non-binary properties in python-flask

with the given yaml, the following code was generated

def test(job_id=None, file=None):  # noqa: E501
    return 'do some magic!'

But the non-binary fields (ie. job_id) returns a None always!

workaround:
add a 'body' field to the method and the job_id can be accessed using the body dict

def test(body, job_id=None, file=None):  # noqa: E501
    print(job_id) 
    print(file)
    print(body) # job_id is in this dict
    return 'do some magic!'
Swagger-codegen version

3.0.11

Swagger declaration file content or url

https://gist.github.com/nirandaperera/dba9a70f7f8e189b0b035e4a040d7c82

Command line used for generation

java -jar ~/bin/swagger-codegen-cli-3.0.11.jar generate -i bug.yaml -l python-flask -o bug

Steps to reproduce
  • generate the code
  • add the following snippet to the generated code in
    bug/swagger_server/controllers/default_controller.py
def test(job_id=None, file=None):  # noqa: E501
    print(job_id)
    print(file)
    return 'do some magic!'
  • add a dummy file aaa.txt
  • use this curl cmd
curl -X POST "http://localhost:8080/bug/test" -H "accept: text/csv" -H "Content-Type: multipart/form-data" -F "job_id=aaa" -F "file=@aaa.csv;type=text/csv"
Related issues/PRs

I am not sure if these are related, but I found this issue in another project
https://github.com/OpenAPITools/openapi-generator/issues/1666

and this is where I found the workaround!

Suggest a fix/enhancement

please see the given workaround

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by generating the python-flask server with Swagger Codegen 3.0.11 using the YAML linked in the issue, then reproduce the request with the shown curl command. Inspect bug/swagger_server/controllers/default_controller.py and the generated multipart handling to determine why job_id is None while file is supplied. Done means a multipart request populates both the non-binary job_id and binary file arguments without requiring a body workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.