swagger-api / swagger-api/swagger-codegen-generators

[Python] NameError exception and error in setter in model

Open
#869 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

I have part of swagger yaml:

UserDto:
    description: User with role property
    properties:
      roles:
        additionalProperties:
          items:
            enum:
            - HEAD     # <-  1. Must be String
            - SIGNER   # <-  1. Must be String
            type: string   
          type: array
        description: User role 
        type: object
      departmentId:
        description: departmentId
        format: int32
        type: integer
      id:
        description: id
        format: uuid
        type: string
      login:
        description: login
        type: string
    title: UserDto
    type: object

swagger-codegen-generators-2.4.18.jar builded model UserDto for me with this setter:

    @roles.setter
    def roles(self, roles):
        allowed_values = [HEAD, SIGNER]  # 1. Must be  allowed_values = ["HEAD", "SIGNER"]
       if not set(roles.keys()).issubset(set(allowed_values)):  # 2. Strange check
            raise ValueError(
                "Invalid keys in `roles` [{0}], must be a subset of [{1}]"  # noqa: E501
                .format(", ".join(map(str, set(roles.keys()) - set(allowed_values))),  # noqa: E501
                        ", ".join(map(str, allowed_values)))
            )

        self._roles = roles

swagger types:

swagger_types = {
        'roles': 'dict(str, list[str])',
        'department_id': 'int',
        'id': 'str',
        'login': 'str'
    }

Problems:

  1. I am getting an exception "NameError", because system don't know about variables HEAD and SIGNER (they must be strings "HEAD" and "SIGNER", don't variables)
  2. If I use strings "HEAD" and "SIGNER", validation of set fails

What do you think? How to fix this problems in my project?

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 the Swagger YAML schema and the generated UserDto.roles setter shown in the issue, reproducing generation with swagger-codegen-generators-2.4.18.jar. Trace the generator path that emits enum allowed_values and dictionary-key validation. Done means generated Python uses string enum values and validates the roles keys against those values without raising NameError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, yaml
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.