swagger-api / swagger-api/swagger-codegen

Codegen creates invalid python code when removing underscore from names

Open
#8,782 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

There seem to many other examples of this for other languages but for python client generation the option of modelPropertyNaming=original has no effect.

We have a vendor published schema that uses item names such as '_id'. To make things worse, within the same object, they use both 'id' and '_id' which after client code generation in python results in;

Traceback (most recent call last):
  File "./example_get_devices.py", line 5, in <module>
    import dnac_api_client
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/__init__.py", line 19, in <module>
    from dnac_api_client.api.authentication_api import AuthenticationApi
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/api/__init__.py", line 6, in <module>
    from dnac_api_client.api.authentication_api import AuthenticationApi
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/api/authentication_api.py", line 21, in <module>
    from dnac_api_client.api_client import ApiClient
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/api_client.py", line 27, in <module>
    import dnac_api_client.models
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/models/__init__.py", line 113, in <module>
    from dnac_api_client.models.get_pn_p_global_settings_response import GetPnPGlobalSettingsResponse
  File "/Users/cunningr/git-projects/dnac-api-client/dnac_api_client/models/get_pn_p_global_settings_response.py", line 62
    def __init__(self, sava_mapping_list=None, task_time_outs=None, tenant_id=None, aaa_credentials=None, default_profile=None, accept_eula=None, id=None, id=None, version=None):  # noqa: E501
    ^
SyntaxError: duplicate argument 'id' in function definition
Swagger-codegen version
swagger-codegen version
2.3.1
Swagger declaration file content or url
  GetPnPGlobalSettingsResponse:
    type: object
    properties:
      savaMappingList:
        type: array
        items:
          type: object
          properties:
            syncStatus:
              type: string
            syncStartTime:
              type: number
            syncResult:
              type: object
              properties:
                syncList:
                  type: array
                  items:
                    type: object
                    properties:
                      syncType:
                        type: string
                      deviceSnList:
                        type: array
                        items:
                          type: string
                syncMsg:
                  type: string
            lastSync:
              type: number
            tenantId:
              type: string
            profile:
              type: object
              properties:
                port:
                  type: number
                addressIpV4:
                  type: string
                addressFqdn:
                  type: string
                profileId:
                  type: string
                proxy:
                  type: boolean
                makeDefault:
                  type: boolean
                cert:
                  type: string
                name:
                  type: string
            token:
              type: string
            expiry:
              type: number
            ccoUser:
              type: string
            smartAccountId:
              type: string
            virtualAccountId:
              type: string
            autoSyncPeriod:
              type: number
            syncResultStr:
              type: string
      taskTimeOuts:
        type: object
        properties:
          imageDownloadTimeOut:
            type: number
          configTimeOut:
            type: number
          generalTimeOut:
            type: number
      tenantId:
        type: string
      aaaCredentials:
        type: object
        properties:
          password:
            type: string
          username:
            type: string
      defaultProfile:
        type: object
        properties:
          fqdnAddresses:
            type: array
            items:
              type: string
          proxy:
            type: boolean
          cert:
            type: string
          ipAddresses:
            type: array
            items:
              type: string
          port:
            type: number
      acceptEula:
        type: boolean
      id:
        type: string
      _id:
        type: string
      version:
        type: number

Swagger codegen seems to remove the '_'.

So is this a problem with the schema definition or the way we generate the client?

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

Reproduce the issue with swagger-codegen version 2.3.1 and the supplied schema, then inspect the generated dnac_api_client/models/get_pn_p_global_settings_response.py model and its constructor parameters. Done means the generated Python client imports without a duplicate-argument SyntaxError and retains distinct handling for id and _id when modelPropertyNaming=original is used.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
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.