openapi-generators / openapi-generators/openapi-python-client

Support for multiple content types in error response

Aperta
#1,248 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
2k
Fork
293
Merge medio
34m
PR unite (30g)
1

Descrizione

Describe the bug
Similar to previous issues about multiple content types support like https://github.com/openapi-generators/openapi-python-client/issues/453 or https://github.com/openapi-generators/openapi-python-client/issues/1004

For a single endpoint, the same error code can be returned by different piece of software. For example, an application can return error details as application/json but have some infra elements in front (cloud gateway/load balancer/reverse proxy/firewall) that can block request and also return their own errors in their own format text/json/xml (usually for network errors 401,403,502,503,504).

In my use case, I realized a firewall can sometimes block requests and returns a text/plain with a string Access Forbidden. So I'm trying to represent that in my OpenAPI spec so the generated client can handle these errors properly without crashing, and this works for some of the other openapi generators. But it doesn't work well with this python client, because the content type is not validated in the generated code. It only depends on the first content declared in the schema and seems to ignore the other ones.

So I'm getting the following implementation:

if response.status_code == 403:
  response_403 = APIError.from_dict(response.json())

Which works for json error, but json parsing throw an exception if it's something else (like text/plain)

And if I declare text/plain before application/json, I get:

if response.status_code == 403:
  response_403 = response.text

But in that case, I'm losing any detail about the json error.

What I expect here, is something similar to the oapi-codegen golang implementation.
If there are multiple content types defined for an error code, check that the response content type match before handling it.
So each content type can coexist and be handled separately.

case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 403:
    // this is a json error
    var dest APIError
    if err := json.Unmarshal(bodyBytes, &dest); err != nil {
      return nil, err
    }
    response.JSON403 = &dest
case rsp.StatusCode == 403:    
    ... // default case for text/plain error

OpenAPI Spec File

403:
  description: Forbidden
  content:
    application/json:
      schema:
        $ref: '#/components/schemas/APIError'
    text/plain:
      schema:
        type: string

Desktop (please complete the following information):

  • OS: macOS 14.7.4
  • Python Version: 3.11.88
  • openapi-python-client version: 0.24.3

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Usa la risposta 403 OpenAPI fornita con application/json e text/plain per riprodurre la gestione attuale del client generato. Ispeziona il percorso della risposta di errore del client generato ed esegui la generazione pertinente del client e i test delle risposte, se disponibili. Il lavoro è completato quando il client generato seleziona la gestione in base al Content-Type della risposta, così che i dettagli JSON rimangano disponibili senza tentare di analizzare text/plain come JSON.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
api
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.