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

[BUG] [PYTHON CLIENT] response is None although data is returned HTTP 2xx #12189

Closed
IAmWebSA opened this issue Apr 21, 2022 · 4 comments
Closed

Comments

@IAmWebSA
Copy link

Running openapi cli generator for python results in 'response_type': None for POST although multiple are defined.

The Route can return the following status codes 200, 202, 401, 403 or 500.
The issue is if the route is called via the python client it returns "None" for the 202 status code instead of the actual returned value of "{id:123}".

Is there away to let the client handle also such 2xx responses to return the data?

Via other REST Tools it displays the correct response values as application/json as it should be.

Sample definitions:


openapi: 3.0.1
info:
  title: REST API
  version: "0.1"
paths:
  /test:
    post:
      summary: Trigger a async task
      description: Trigger a async task
      responses:
        200:
          description: OK
          content: {}
        202:
          description: route executed and ID returend.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Response202'
        401:
          description: ohno
          content: {}
        403:
          description: ohno
          content: {}
        500:
          description: Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'


components:
  schemas:
    Error:
      type: object
      properties:
        help:
          type: string
    Response202:
      required:
      - Id
      type: object
      properties:
        Id:
          type: integer
          description: The id
          example: 1

@spacether
Copy link
Contributor

Can you please try python-experimental? It has better deserialization support than python.

@IAmWebSA
Copy link
Author

IAmWebSA commented Apr 27, 2022

I have tried with 5.4,0 and 6.0.0 Beta using:

java -jar openapi-generator-cli.jar generate -i "openapi3.yaml" -g python-experimental -o openapi-python-client --package-name rest_client --additional-properties packageVersion=0.1,packageName=rest_client,modelPropertyNaming=original

But now it is even worse and has syntax issues:

Error : "Union requires two or more type argumentsPylance"

@dataclass
class ApiResponseFor200(api_client.ApiResponse):
    response: urllib3.HTTPResponse
    body: typing.Union[
    ]
    headers: Unset = unset

What I also mentioned is that if you have a HTTP Response 200 with a required field set and you get a HTTP Response 202 with a different return structure it throws an error that it cannot find the required property from the 200 response in the 202 response.

Is this also a known bug?

I also tried to get rid off the snake_case conversion with the "modelPropertyNaming=original" but this also does not work.
Am I doing something wrong?
If I have a property like MyCamelProperty it converts the returned property to "my_camel_property", this is strange to me and has to be prevented.

@spacether
Copy link
Contributor

spacether commented Apr 28, 2022

1

I don't think that those releases contain the fix that you need. Please pull down and build the latest master branch.
Your spec omits response schemas which was only recently added in: #12135
One can see an endpoint that lacks response schemas here:
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/petstore/python-experimental/petstore_api/api/fake_api_endpoints/response_without_schema.py#L75

@dataclass
class ApiResponseFor200(api_client.ApiResponse):
    response: urllib3.HTTPResponse
    body: typing.Union[
        Unset,
        Unset,
    ]
    headers: Unset = unset

Using a commit which includes that PR will fix that SyntaxError

2

if you have a HTTP Response 200 with a required field set and you get a HTTP Response 202 with a different return structure it throws an error that it cannot find the required property from the 200 response in the 202 response

Your explanation here is confusing.
The 200 response lacks any schema definition so when it is deserialized body will remain unset though you will be able to access the urllib3 response
The 202 response does have a schema definition and it will be used when deserializing the body.
The required property cannot be defined in the 200 response if the 200 response (schema) is not defined.
Response bodies are deserialized based on:

  1. what the http status code is
  2. what the content type is (multiple content types are supported under each status code)

Not sure what the bug is that you are mentioning.

3

Where are you seeing property naming that does not conform to the spec?

  • model properties preserve spec case
  • endpoint properties preserves spec case
    Are you talking about model names?

The openapi spec allows component schema names that are invalid python class names like:

  • 'horrible-name'
  • ' '
  • '1BadName'
    Because of that the python generator code generates replacement class names for component schema (model) names.
    It is probably possible to define classes like:
class OneBadName:
    ...
locals()['1BadName'] = OneBadName

but one would also need to fix the class file names and imports too which is non trivial.

@spacether
Copy link
Contributor

spacether commented Oct 4, 2022

Closing this issue because it is handled in the python-experimental generator in v6.0.0 and onward and in the python generator in v6.2.0 and onward
For cases where no response body is defined in the spec, the response.body will have the value of Unset, and one can access response.response to get the raw response from the rest client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants