openapi-python-client icon indicating copy to clipboard operation
openapi-python-client copied to clipboard

Handle Default Responses in response generation

Open bladecoates opened this issue 4 years ago • 1 comments

Describe the bug In the specifications, default MAY be used as a default response object for all HTTP codes that are not covered individually by the specification.

response = response_from_data(status_code=int(code), data=response_data)

ValueError: invalid literal for int() with base 10: 'default'

To Reproduce Sample code from a openapi spec.

      responses:
        "200":
          description: An array of items
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/ItemsArrayObj"
        default:
          description: Unknown error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

Expected behavior Code should generate a custom ApiResponseError class if default is defined, with content of error being a model of the reference/schema items

OpenAPI Spec File Sample code from a openapi spec.

      responses:
        "200":
          description: An array of items
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/ItemsArrayObj"
        default:
          description: Unknown error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"

Desktop (please complete the following information):

  • OS: Linux Mint
  • Python Version: 3.8.3
  • openapi-python-client version: git main

bladecoates avatar Aug 04 '20 17:08 bladecoates

fwiy I just monkeypatched parser/openapi.py temporarily with edit: actually the default was the valid response so for me it ended up being

response_from_data(status_code=int(200 if code == 'default' else code)

I think it is atypical

@staticmethod
def _add_responses(endpoint: "Endpoint", data: oai.Responses) -> "Endpoint":
    endpoint = deepcopy(endpoint)
    for code, response_data in data.items():
+       if code == 'default':
+           continue
        response = response_from_data(status_code=int(code), data=response_data)
        if isinstance(response, ParseError):
            endpoint.errors.append(
                ParseError(
                    detail=(
                        f"Cannot parse response for status code {code}, "
                        f"response will be ommitted from generated client"
                    ),
                    data=response.data,
                )
            )
            continue
        if isinstance(response, (RefResponse, ListRefResponse)):
            endpoint.relative_imports.add(import_string_from_reference(response.reference, prefix="...models"))
        endpoint.responses.append(response)
    return endpoint

micimize avatar Sep 28 '20 16:09 micimize