Skip to content

Path parameters as function positional arguments #429

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

Conversation

tsotnikov
Copy link
Contributor

OpenAPI Specification indicates that if parameter location is path the required property is REQUIRED and its value MUST be true

Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-object

With this in mind, this PR:

  • Tighten the parsing by throwing an error for path parameters with required=false
  • Update the templates to pass the path parameters to the function as positional arguments instead of kwargs
    to improve usability

OpenAPI Specification indicates that if parameter location is `path`
the `required` property is REQUIRED and its value MUST be `true`

Ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-object

With this in mind, this PR:

- Tighten the parsing by throwing an error for path parameters with required=false
- Update the templates to pass the path parameters to the function as positional arguments instead of kwargs
to improve usability
Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Requiring required seems like a good move. Only thing we'll have to work on is how we approach positional arguments since they're inherently more fragile than the kwarg-only approach we've been taking.

Comment on lines +76 to +79
{# path parameters #}
{% for parameter in endpoint.path_parameters %}
{{ parameter.to_string() }},
{% endfor %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only problem with this is that the order of endpoint.path_parameters is not guaranteed. If the declaration order swaps around or there's a slight change in implementation (e.g. at some point we decide to alphabetize kwargs) then generated clients will break and potentially do so in a way that consumers don't catch.

If we want to allow positional args, we'll have to guarantee the order for them. For Path params we could use the order in which they appear in the path, which seems the most logical solution to me. It's going to require a bit more up front parsing work though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I will work on sort them in the order they appear in the path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b9e8057

@tsotnikov tsotnikov requested a review from dbanty May 18, 2021 17:22
Copy link
Collaborator

@dbanty dbanty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks! Just one suggestion which I think will make the sorting a bit more efficient.

Also fix typo

Co-authored-by: Dylan Anthony <43723790+dbanty@users.noreply.github.com>
@tsotnikov
Copy link
Contributor Author

The lint is failing:

openapi_python_client/parser/openapi.py:283: error: Argument "data" to "ParseError" has incompatible type "List[Property]"; expected "Optional[BaseModel]"  [arg-type]

I guess I am forced to pass endpoint instead of endpoint.path_parameters to ParseError.data as the type is incompatible.

@dbanty
Copy link
Collaborator

dbanty commented Aug 7, 2021

The lint is failing:

openapi_python_client/parser/openapi.py:283: error: Argument "data" to "ParseError" has incompatible type "List[Property]"; expected "Optional[BaseModel]"  [arg-type]

I guess I am forced to pass endpoint instead of endpoint.path_parameters to ParseError.data as the type is incompatible.

That field is intended to take and then display the part of the schema that's wrong. I think we can just omit it and include the path in the error message.

@dbanty
Copy link
Collaborator

dbanty commented Aug 7, 2021

Sorry for the delay on this @tsotnikov, I've created a branch in-repo and a PR at #464 so I can update this and finish it off. Thanks for all the hard work!

@dbanty dbanty closed this Aug 7, 2021
dbanty added a commit that referenced this pull request Aug 15, 2021
@tsotnikov!

Co-authored-by: Taras Sotnikov <taras.sotnikov@telefonica.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants