Skip to content

SyntaxError: Parameters with the same name but different locations #305

Closed

Description

@seeM

Describe the bug

If you have two parameters with the same name but different locations (in), the generated client has a function with repeated variable names which is a Python syntax error. The OpenAPI spec requires parameters to be unique only on (name, location), so this is a bug.

In my case, the spec was a result of using a very standard Django Rest Framework ModelViewSet (which added the path parameter) with a django_filters.FilterSet (which added the query parameter).

EDIT: Perhaps my specific case should be fixed in django-filters... Maybe those filter parameters shouldn't be added to actions other than list.

To Reproduce

Generate a client with this spec:

openapi: 3.0.2
info:
  title: test
  version: "0.0.0"
paths:
  /pets/{name}/:
    get:
      operationId: retrievePet
      parameters:
        - name: name
          required: true
          in: path
          schema:
            type: string
        - name: name
          in: query
          required: false
          schema:
            type: string
      responses:
        "200":
          description: pet response
          content:
            application/json:
              schema:
                type: string

The result has this function (among others) which don't distinguish between the parameters and are thus Python syntax errors:

# in test-client/test_client/api/default/retrieve_pet.py
def _get_kwargs(
    *,
    client: Client,
    name: str,
    name: Union[Unset, str] = UNSET,    # <-- a duplicate `name` variable which leads to a syntax error

) -> Dict[str, Any]:
    url = "{}/pets/{name}/".format(
        client.base_url,name=name)

    headers: Dict[str, Any] = client.get_headers()

Expected behavior

I'm not sure the best way to handle this. openapi-generator does so by suffixing duplicate parameter names with integers. E.g., the arguments to its endpoint functions are name for the path parameter and name2 for the query parameter.

Desktop (please complete the following information):

  • Python version: 3.6.4
  • openapi-python-client version: 0.7.3

Activity

  1. added
    🐞bug
    on Jan 18, 2021
  2. dbanty commented on Jan 18, 2021

    @dbanty
    Collaborator

    Ick... yeah that's definitely a thing we need to do something about. My initial thought is to suffix the names with the location in the event of a conflict. So in your example it would change them to name_path and name_query. Not the most elegant thing, but I can't think of a solution that is particularly elegant.

    Thanks for reporting this issue!

  3. added a commit that references this issue on May 2, 2021
    ff784ad
  4. added a commit that references this issue on May 4, 2021
    cdc0d8c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions