swagger-api / swagger-api/swagger-codegen

Python/swagger-codegen-cli list types in swagger_types does not work for list of primitives

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

This issue is in relation to the swagger_types dictionary found in the model classes. If a key has a value of a list of primitive types, the setter for that key or attribute does not actually accept a list of primitives. It only accepts a string.

Swagger-codegen version

swagger-codegen-cli version 2.2.1

Swagger declaration file content or url
self.swagger_types = { "foo", "list[str]" }

if I try to set "foo" to ["foo"] or to ["foo1", "foo2"], it will return this error:

ValueError: Invalid value for foo (['foo']), must be one of ['foo', 'foo1', 'foo2']

The issue is found in the setter for foo. For example:

@foo.setter
  def foo(self, foos):
      """
      Sets the foos of this Object.


      :param foos: The foos of this Object.
      :type: list[str]
      """
      allowed_values = ["foo", "foo1", "foo2"]
      if foos not in allowed_values:
          raise ValueError(
              "Invalid value for `foos` ({0}), must be one of {1}"
              .format(foos, allowed_values)
          )

      self._foos = foos
Suggest a fix/enhancement

My suggestion is to loop through the values in the list before checking if that value is in allowed_values.

@foo.setter
def foo(self, foos):
"""
Sets the foos of this Object.

  :param foos: The foos of this Object.
  :type: list[str]
  """
  allowed_values = ["foo", "foo1", "foo2"]
  for foo in foos:
      if foo not in allowed_values:
          raise ValueError(
              "Invalid value for `foo ` ({0}), must be one of {1}"
              .format(foo , allowed_values)
          )

  self._foos = foos

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 by locating the swagger-codegen-cli template or generator responsible for Python model setters, then reproduce the issue with a list[str] declaration and allowed values such as ["foo", "foo1", "foo2"]. Done means generated setters accept valid lists of primitives, reject invalid elements, and retain the existing scalar validation behavior.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.