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

Upload file logic seems to be wrong on 3.1 schema

Aperta
#1,417 2 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
I just upgraded my API, and the schema changed from 3.0 to 3.1. The uploads suddenly broke. It seems that the code generated for the body class now looks like this:

    file: str
 
    ...
    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        files.append(("file", (None, str(self.file).encode(), "text/plain")))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

While the old looked like this:

    file: File

    python
    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        files.append(("file", self.file.to_tuple()))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

Where the File object is this:

@define
class File:
    """Contains information for file uploads"""

    payload: BinaryIO
    file_name: str | None = None
    mime_type: str | None = None

    def to_tuple(self) -> FileTypes:
        """Return a tuple representation that httpx will accept for multipart/form-data"""
        return self.file_name, self.payload, self.mime_type

OpenAPI Spec File
A link to an OpenAPI document which produces this issue. Ideally, write a minimal reproduction only containing the problematic pieces.

The API spec for the body (new 3.1):

          "file": {
            "type": "string",
            "contentMediaType": "application/octet-stream",
            "title": "File"
          }

Old (3.0) schema:

          "file": {
            "type": "string",
            "format": "binary",
            "title": "File"
          }

Desktop:

  • OS: [e.g. macOS 10.15.1]: MacOS (M4) 26.3.1
  • Python Version: [e.g. 3.8.0]: 3.11.14
  • openapi-python-client version [e.g. 0.1.0]: 0.28.3

Additional context
I have some code pre-massaging around a different name-related issue and adding this to the pre-processing before this tool is called fixed it, but it's definitely more of a temporary workaround type of thing:

for prop in props.values():
      if prop.get("contentMediaType") == "application/octet-stream":
          prop["format"] = "binary"

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

Inizia riproducendo la classe body generata dallo schema OpenAPI 3.1 fornito e confrontala con lo schema 3.0 usando format: binary. Traccia come viene interpretato contentMediaType: application/octet-stream durante la generazione; il lavoro è completato quando il campo generato usa File e la serializzazione multipart chiama to_tuple() come nell’output precedente.

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

Valutazione

Stack tecnologico
openapi, python
Ambito
api, tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.