OpenAPITools / OpenAPITools/openapi-generator

[BUG] Do not auto-select content type as application/json if available in content types list

Open
#9,963 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?

Couldn't build the project in local due to missing JDK. However, I see no changes to the code templates.

  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If multiple payload content types are set, ex: json and multipart, json is always picked as default header.

My requirement: If the payload is a json payload, the server expects a file_key which was uploaded via an upload endpoint, and if it is multipart, a file is expected.

openapi-generator version

5.1.1

OpenAPI declaration file content or url

https://gist.github.com/tiholic/99cf45e7eebe24ff4fc3bc02802f4485

Generation Details
openapi-generator generate -i http://nestjs-server.localhost/docs-json/ -o ./ -g python --additional-properties generateSourceCodeOnly=true,packageName=example_api.example
Steps to reproduce

Use the schema from gist and try to call the action endpoint with a file payload open('a.txt', 'rb') and you can see that Content-Type is posted as application/json.

api_client = example.ApiClient(configuration)
api_client.action(open('a.txt', 'rb'))  # errors out from server as server receives no file
Related issues/PRs

None that I can find very close, but there are PRs and issues reported that revolve around enhancing select_header_content_type function.

Suggest a fix

A Content-Type vs Open API Type map must be available to auto select content type based in the input.

For example, from this schema (see gist for full schema),

  "requestBody": {
    "content": {
      "multipart/form-data": {
        "schema": {
          "$ref": "#/components/schemas/FileInput"
        }
      },
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/FileInputAsJson"
        }
      }
    }
  }

we can create a map like this (or whatever makes sense w.r.t language)

{
  "#/components/schemas/FileInput": "multipart/form-data",
  "#/components/schemas/FileInputAsJson": "application/json"
}
A hack I use to get out of this situation, currently:

I create an 2 api clients, one for forcing multipart content type,

multipart_api_client = example.ApiClient(configuration)
multipart_api_client.set_default_headers('Content-Type', 'multipart/form-data')

multipart_api_client.action(open('a.txt', 'rb'))  # works

and other for default

default_api_client = example.ApiClient(configuration)

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 with the generated Python client's select_header_content_type function and reproduce the action endpoint from the linked gist using the provided openapi-generator command. Compare the multipart and application/json schemas and verify that the generated client selects the content type appropriate to the supplied file payload without breaking other content-type selection cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, python
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.