swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.98k stars 6.03k forks source link

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

Open jakobkogler opened 3 years ago

jakobkogler commented 3 years ago
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.

ghost commented 2 years ago

I bumped into a similar problem, but with @ instead of $

StudioSpindle commented 1 year ago

@jakobkogler Did you find a workaround?

zovippro1996 commented 1 year ago

@StudioSpindle , @jakobkogler ,

In my project, we discovered a workaround (our project using Angular and Dotnet). I described it here, hope it would still relavant and help you guys: https://github.com/swagger-api/swagger-codegen/issues/12055#issuecomment-1532426777