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

Generate Python code compatible with Connexion 3.0.1 #4622

Open
SrSoto opened this issue Nov 29, 2023 · 1 comment
Open

Generate Python code compatible with Connexion 3.0.1 #4622

SrSoto opened this issue Nov 29, 2023 · 1 comment

Comments

@SrSoto
Copy link

SrSoto commented Nov 29, 2023

Content & configuration

For this feature request we can simply stay on the Pet Store example from the web editor

This request relates more to a general characteristic of Python code generation rather than an specific example given a config YAML.

Is your feature request related to a problem?

Connexion updated to a new major version one month ago, leading to breaking changes on common objects like connexion.request or connexion.context. Here are their docs on what's new and how we should adapt.

One 'minor' breaking change is that request object is now a Starlette Request and not a Flask one, and this is related with the generation of code when given a swagger.yaml like the one from the pet store.

The following generated code example for add_pet does no longer work with connexion==3.0.1:

def add_pet(body):  # noqa: E501
    """Add a new pet to the store

    Add a new pet to the store # noqa: E501

    :param body: Create a new pet in the store
    :type body: dict | bytes

    :rtype: Pet
    """
    if connexion.request.is_json:
        body = Pet.from_dict(connexion.request.get_json())  # noqa: E501
    return my_add_pet_function(body)  # Added by myself, but usually is 'do some magic!'

Describe the solution you'd like

Following the previous example, this is how add_pet should be generated for correctly manipulating the Starlette Request object:

def add_pet(body):  # noqa: E501
    """Add a new pet to the store

    Add a new pet to the store # noqa: E501

    :param body: Create a new pet in the store
    :type body: dict | bytes

    :rtype: Pet
    """
    if connexion.request.mimetype == "application/json":
        body = Pet.from_dict(body)  # noqa: E501
    return my_add_pet_function(body)  # Added by myself, but usually is 'do some magic!'

Some points about the given solution, working on our APIs:

  • is_json() is a method from Flask.Request which does not appear on starlette.request.Request. One has to use mimetype property instead.
  • body is now given on a dictionary format with the correct arguments passed by connexion middleware, instead of having to access the request by get_json
  • What's more, get_json method does not exist for starlette.request.Request, and its corresponding json method is an async function whose await has been already taken by connexion

Describe alternatives you've considered

I'm not fully aware of the consequences of accessing directly the body argument on controller functions, but in case it is necessary to get the request data from connexion.request I tried with asyncio.run, but it hangs permanently waiting for an object already taken.

Additional context

It is important to highlight a security issue on previous versions of Werkzeug, which depends on this new major version of connexion, that leads to certain urgency on this update for those who have on any production environment their swagger APIs.

Thanks in advance!

@MacwinWin
Copy link

MacwinWin commented Dec 1, 2023

I found the latest version of connexion and flask that adapt to the current version of swagger editor is:

  • connexion==2.14.2
  • flask==2.3.3
  • Python==3.12.0

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

No branches or pull requests

2 participants