swagger-api / swagger-api/swagger-codegen

[Python] client None value set query parameters into literal 'None'

Open
#10,572 0 comments 0 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

With the generated Python API client, None value in the parameter with string type has been literally transformed to "None" and added to API call, because in Python string representation of None is "None"

But given that None in Python generally means a non-exist value, the expected behavior should not pass the value in query parameters or set the parameter value with a default value if specified.

Swagger-codegen version

2.4.17

Swagger declaration file content or url

If you post the code inline, please wrap it with

...
parameters:  [{
    "name": "test",
    "in": "query",
    "required": false,
    "type": "string"
  }]
...

-->

Command line used for generation
  java -jar swagger-codegen-cli-2.4.17.jar generate -l python -i swagger.json
Steps to reproduce

We generated the swagger.json file using swagger annotation (@ApiParam) from a Dropwizard application written in Java.

Suggest a fix/enhancement

Add None check in api.mustache.

{{#queryParams}}
        if '{{paramName}}' in params and params['{{paramName}}'] is not None:       
                query_params.append(('{{baseName}}', params['{{paramName}}'])){{#isListContainer}}  # noqa: E501
{{/queryParams}}

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 with modules/swagger-codegen/src/main/resources/python/api.mustache and inspect how optional query parameters are appended in generated Python clients. Generate a client from a definition containing an optional string query parameter, then verify that passing None omits the parameter rather than sending the literal value "None".

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.