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

prevent UnboundLocalError if the user did not provide a request body #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FelixSchwarz
Copy link

@FelixSchwarz FelixSchwarz commented Dec 4, 2023

Traceback (most recent call last):
  File "…/openapi/client/client.py", line 33, in <module>
    result = api.call_post_greeting(parameters={'name': 'foo'})
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "…/openapi/venv/lib64/python3.12/site-packages/openapi3/openapi.py", line 256, in __call__
    return self.operation(self.base_url, *args, security=self.security, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "…/openapi/venv/lib64/python3.12/site-packages/openapi3/paths.py", line 337, in request
    self._request_handle_body(data)
  File "…/openapi/venv/lib64/python3.12/site-packages/openapi3/paths.py", line 282, in _request_handle_body
    self._request.data = body
                         ^^^^
UnboundLocalError: cannot access local variable 'body' where it is not associated with a value

Even though the request might still be invalid with the modification, at least there is a chance that the server response contains some helpful information.

For example a simple Connexion app responds with: HTTP status code 400 / "Request body must not be empty".

The openapi3 client used:

result = api.call_post_greeting(parameters={'name': 'foo'})

instead of

result = api.call_post_greeting(parameters={'name': 'foo'}, data={})

Even though the request might be invalid, at least there is a chance
that the server response contains some helpful information.

For example a simple Connexion app responds with:
  HTTP status code 400
  Request body must not be empty
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.

1 participant