Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seeM opened this issue Jan 18, 2021 · 1 comment · Fixed by #406
Closed

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

seeM opened this issue Jan 18, 2021 · 1 comment · Fixed by #406
Labels
🐞bug Something isn't working

Comments

@seeM
Copy link

seeM commented Jan 18, 2021

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
@seeM seeM added the 🐞bug Something isn't working label Jan 18, 2021
@dbanty
Copy link
Collaborator

dbanty commented Jan 18, 2021

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!

dbanty added a commit that referenced this issue May 2, 2021
dbanty added a commit that referenced this issue May 4, 2021
… and location (fixes #305) (#406)

* fix(parser): Attempt to deduplicate endpoint parameters based on name and location (fixes #305)

* refactor: Use location prefix for all duplicate params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞bug Something isn't working
Projects
None yet
2 participants