OpenAPITools / OpenAPITools/openapi-generator

[REQ] Deserialize error response models from response

Open
#12,588 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

We use custom error responses, including details, such as debug info or textual error description in a JSON object in the response body. For example, our definition looks somehow like this:

schemas:
    genericErrorResponse:
      properties:
        debug:
          properties:
            source:
              type: string
            stages:
              properties:
                failure:
                  items:
                    type: string
                  type: array
                success:
                  items:
                    type: string
                  type: array
              type: object
          type: object
        error:
          properties:
            code:
              type: integer
            message:
              type: string
          required:
            - code
            - message
          type: object
      type: object
....
responses:
    genericNotFound:
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/genericErrorResponse"
....            
  /resource.json/{identifier}:
    delete:
      responses:
        200:
          $ref: "#/components/responses/genericDelete"
        404:
          $ref: "#/components/responses/genericNotFound"

The generator generates python code for the response model, but the REST client throws an exception NotFoundException, never actually creating a model instance from the response body.

Describe the solution you'd like

Instead of only being able to access the raw response body as an exception attribute, I would like to access the correctly parsed response body

try:
  x = client.delete_resource(identifier="123")
except NotFoundException as e:
  logger.warn(e.body.error.message)

Describe alternatives you've considered

Currently we catch ApiException and do something along the following lines to make sure the message ends up in a place where logging picks it up. without logging the whiole response body (ApiException.__str__)

api_error = json.loads(e.body)["error"]
e.reason = client_models.Error(**api_error).message
e.body = None
raise e

Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the generated Python client's NotFoundException and ApiException handling, then inspect how response models are created from successful response bodies. Determine how the referenced error response schema can be deserialized before the exception is raised. Done means callers can access the parsed error model through the exception while retaining the existing exception behavior, with coverage for the documented 404 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.