Skip to content
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

Endpoints with both optional and not optional parameters can produce broken python modules #243

Closed
fyhertz opened this issue Nov 18, 2020 · 2 comments
Labels
🐞bug Something isn't working

Comments

@fyhertz
Copy link
Contributor

fyhertz commented Nov 18, 2020

Describe the bug

Endpoints with both optional and not optional parameters can produce broken python modules because there is no guarantee that the optional arguments will be last in the generated function signature.

The problem lies in the implementation of the jinja macro arguments and in the _add_parameters function of Endpoint. A quick fix is to add:

# Optional parameters must be last
for parameters in [endpoint.path_parameters, endpoint.query_parameters, endpoint.header_parameters]:
    parameters.sort(key=lambda e: e.nullable or not e.required)

At the end of Endpoint.__add_parameters.

What should I do for a clean fix? I guess we need to regroup the three list mentioned above in a single one? Any pitfalls?

OpenAPI Spec File

{
  "openapi": "3.0.2",
  "info": {
    "title": "My Test API",
    "description": "An API for testing openapi-python-client",
    "version": "0.1.0"
  },
  "paths": {
    "/tests/": {
      "get": {
        "tags": [
          "tests"
        ],
        "summary": "Get List",
        "description": "Get a list of things ",
        "operationId": "getUserList",
        "parameters": [
          {
            "nullable": true,
            "schema": {
              "title": "An Enum Value 2",
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/AnEnum"
              }
            },
            "name": "an_enum_value_2",
            "in": "query"
          },
          {
            "required": false,
            "schema": {
              "title": "An Enum Value",
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/AnEnum"
              }
            },
            "name": "an_enum_value",
            "in": "query"
          },
          {
            "required": true,
            "schema": {
              "title": "Some Date",
              "anyOf": [
                {
                  "type": "string",
                  "format": "date"
                },
                {
                  "type": "string",
                  "format": "date-time"
                }
              ]
            },
            "name": "some_date",
            "in": "query"
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "title": "Response Get List Tests  Get",
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/AModel"
                  }
                }
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        }
      }
    }
  }
}

Desktop (please complete the following information):

  • OS: Ubuntu 20.04
  • Python Version: 3.8.5
  • openapi-python-client version: 0.6.2
@fyhertz fyhertz added the 🐞bug Something isn't working label Nov 18, 2020
@emann
Copy link
Collaborator

emann commented Nov 18, 2020

0.7.0 has a complete rework of how nullable & optional properties are handled, and this should definitely be fixed in there (I remember running into this). Give that a shot and let me know if you still have troubles!

@fyhertz
Copy link
Contributor Author

fyhertz commented Nov 19, 2020

Thanks @emannguitar, I'm closing the issue.

@fyhertz fyhertz closed this as completed Nov 19, 2020
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
Development

No branches or pull requests

2 participants