swagger-api / swagger-api/swagger-codegen

[PYTHON] duplicated function arguments when model properties have dollar symbols

Open
#10,596 3 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

When model properties have dollar symbols $, then the swagger-codegen strips out these characters.
This can lead to duplicated function arguments (and also duplicated class members) in the Python code.

E.g. when a model has the properties bar and $bar:

definitions:
  Foo:
    properties:
      bar:
        type: integer
      $bar:
        type: string

This creates a class with an __init__ method with duplicated parameters:

class Foo(object):
    ...
    def __init__(self, bar=None, bar=None):  # noqa: E501
        self._bar = bar
        self._bar = bar
Swagger-codegen version

2.4.17
and
3.0.23

Swagger declaration file content or url

Full swagger file: https://gist.github.com/jakobkogler/d54d50e8188adf70a668b70d087f8a7e

Command line used for generation
./run-in-docker.sh generate -i foo.yaml -l python -o /gen/foo
Steps to reproduce
  1. Download the swagger-file from the gist linked above.
  2. Run ./run-in-docker.sh generate -i foo.yaml -l python -o /gen/foo
  3. pip install -e foo
  4. In a Python repl: import swagger_client fails because of duplicated function arguments
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/10276
https://github.com/swagger-api/swagger-codegen/issues/10447

Suggest a fix/enhancement

Looks like the dollar sign gets removed at https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java#L397
Not sure what a good solution is, maybe replace $ with _dollar_, if the stripped out variable name clashes with another one.

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 at modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PythonClientCodegen.java around line 397 and reproduce with the linked gist, foo.yaml, and the run-in-docker.sh command. Check the generated Python model for collisions between bar and $bar; done means generation no longer creates duplicated arguments or class members and pip installation followed by import succeeds.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.