Skip to content

Endpoints with hyphens in parameter names trigger Incorrect path templating warning, are not generated #976

Closed
@harabat

Description

@harabat

Describe the bug

openapi-python-client fails to generate endpoints and throws incorrect path templating warnings when the path has a parameter with a hyphen.

Renaming the parameters fixes the issue. Hyphens in parameter names don't seem to go against Swagger/OpenAPI naming conventions, which would indicate that the issue is with openapi-python-client, though I haven't found any existing issues around this.

OpenAPI Spec File

This is from an OpenAPI spec generated from a Swagger spec with swagger-converter.

  "paths": {
    "/activitypub/user-id/{user-id}":
      "get": {
        "parameters": [
          {
            "name": "user-id",
            ...
          }
        ],
        ...
      }
    },
    "/activitypub/user-id/{user-id}/inbox": {
      "post": {
        "parameters": [
          {
            "name": "user-id",
            ...
          }
        ],
        ...
      }
    },
  ...
  }

Desktop (please complete the following information):

  • OS: Linux Manjaro
  • Python Version: 3.12.2
  • openapi-python-client version: 0.17.3

Additional context

Warnings:

WARNING parsing GET /activitypub/user-id/{user-id} within activitypub. Endpoint will not be generated.
Incorrect path templating for /activitypub/user-id/{user-id} (Path parameters do not match with path)

WARNING parsing POST /activitypub/user-id/{user-id}/inbox within activitypub. Endpoint will not be generated.
Incorrect path templating for /activitypub/user-id/{user-id}/inbox (Path parameters do not match with path)

To diagnose, I added the following print statements to parser/openapi.py:

    def sort_parameters(*, endpoint: "Endpoint") -> Union["Endpoint", ParseError]:
        # ...
        print(endpoint.name, endpoint.path)                                                   # <- new
        print(f'\tparameters_from_path: {parameters_from_path}')                              # <- new
        print(f'\tendpoint {endpoint.name}.path_parameters: {endpoint.path_parameters}')      # <- new

        if parameters_from_path != [param.name for param in endpoint.path_parameters]:
            return ParseError(
                detail=f"Incorrect path templating for {endpoint.path} (Path parameters do not match with path)",
            )
        return endpoint

For comparison, replacing one of the user-id occurrences with userid and generating a client yields this:

('activitypubPerson /activitypub/user-id/{userid}'
 "      parameters_from_path: ['userid']"
 "      endpoint activitypubPerson.path_parameters: [IntProperty(name='userid', "
 "required=True, default=None, python_name='userid', description=None, "
 'example=None)]'
 'activitypubPersonInbox /activitypub/user-id/{user-id}/inbox'
 '      parameters_from_path: []'
 '      endpoint activitypubPersonInbox.path_parameters: '
 "[IntProperty(name='user-id', required=True, default=None, "
 "python_name='user_id', description=None, example=None)]"
 ...
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions