OpenAPITools / OpenAPITools/openapi-generator
[BUG] [JAVA] Passed in Content-Type is overwritten as `application/json`
Nobody has claimed this yet.
- 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?
- Have you searched for related issues/PRs?
Description
We have a spec where one of the request bodies allows for multiple content-types. If a user specifies a particular content type, say image/jpeg, the generated SDK will be default overwrite that content type always to application/json with a selectHeaderContentType function defined below. This should be changed so that passed in Content-Types are not overwritten by this generated function. I couldn't figure out any way to override this with mustache templates since I had no way to pull in the content-type type defined by the user when using the SDK
/**
* Select the Content-Type header's value from the given array:
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty,
* returns null. If it matches "any", JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
return null;
}
if (contentTypes[0].equals("*/*")) {
return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
openapi-generator version
OpenAPI declaration file content or url
Here is a minimal version that just includes the part that's breaking
openapi: 3.0.3
info:
title: Messaging
version: 4.3.0
contact:
name: Bandwidth
url: https://support.bandwidth.com
email: support@bandwidth.com
description: |-
The API Specification for Bandwidth's Messaging Platform
## Base URL
`https://messaging.bandwidth.com/api/v2`
servers:
- url: https://messaging.bandwidth.com/api/v2
description: Production
paths:
/users/{accountId}/media:
put:
summary: Upload Media
description: |-
Upload a file. You may add headers to the request in order to provide some control to your media file.
operationId: uploadMedia
tags:
- Media
parameters:
- $ref: "#/components/parameters/accountId"
- $ref: "#/components/parameters/mediaId"
- $ref: "#/components/parameters/contentType"
- $ref: "#/components/parameters/cacheControl"
requestBody:
$ref: "#/components/requestBodies/uploadMediaRequest"
responses:
"204":
description: No Content
components:
parameters:
accountId:
in: path
name: accountId
required: true
schema:
type: string
description: Your Bandwidth Account ID.
example: "9900000"
mediaId:
in: path
name: mediaId
required: true
description: Media ID to retrieve.
example: 14762070468292kw2fuqty55yp2b2/0/bw.png
schema:
type: string
contentType:
in: header
name: Content-Type
style: simple
explode: false
description: The media type of the entity-body.
example: audio/wav
schema:
type: string
cacheControl:
in: header
name: Cache-Control
style: simple
explode: false
description: >-
General-header field is used to specify directives that MUST be obeyed by
all caching mechanisms along the request/response chain.
example: no-cache
schema:
type: string
schemas:
requestBodies:
uploadMediaRequest:
content:
application/json:
schema:
type: string
format: binary
application/ogg:
schema:
type: string
format: binary
application/pdf:
schema:
type: string
format: binary
application/rtf:
schema:
type: string
format: binary
application/zip:
schema:
type: string
format: binary
application/x-tar:
schema:
type: string
format: binary
application/xml:
schema:
type: string
format: binary
application/gzip:
schema:
type: string
format: binary
application/x-bzip2:
schema:
type: string
format: binary
application/x-gzip:
schema:
type: string
format: binary
application/smil:
schema:
type: string
format: binary
application/javascript:
schema:
type: string
format: binary
audio/mp4:
schema:
type: string
format: binary
audio/mpeg:
schema:
type: string
format: binary
audio/ogg:
schema:
type: string
format: binary
audio/flac:
schema:
type: string
format: binary
audio/webm:
schema:
type: string
format: binary
audio/wav:
schema:
type: string
format: binary
audio/amr:
schema:
type: string
format: binary
audio/3gpp:
schema:
type: string
format: binary
image/bmp:
schema:
type: string
format: binary
image/gif:
schema:
type: string
format: binary
image/jpeg:
schema:
type: string
format: binary
image/pjpeg:
schema:
type: string
format: binary
image/png:
schema:
type: string
format: binary
image/svg+xml:
schema:
type: string
format: binary
image/tiff:
schema:
type: string
format: binary
image/webp:
schema:
type: string
format: binary
image/x-icon:
schema:
type: string
format: binary
text/css:
schema:
type: string
format: binary
text/csv:
schema:
type: string
format: binary
text/calendar:
schema:
type: string
format: binary
text/plain:
schema:
type: string
format: binary
text/javascript:
schema:
type: string
format: binary
text/vcard:
schema:
type: string
format: binary
text/vnd.wap.wml:
schema:
type: string
format: binary
text/xml:
schema:
type: string
format: binary
video/avi:
schema:
type: string
format: binary
video/mp4:
schema:
type: string
format: binary
video/mpeg:
schema:
type: string
format: binary
video/ogg:
schema:
type: string
format: binary
video/quicktime:
schema:
type: string
format: binary
video/webm:
schema:
type: string
format: binary
video/x-ms-wmv:
schema:
type: string
format: binary
required: true
securitySchemes:
Basic:
type: http
scheme: basic
description: |-
Basic authentication is a simple authentication scheme built into the
HTTP protocol. To use it, send your HTTP requests with an Authorization
header that contains the word Basic followed by a space and a
base64-encoded string `username:password`Example: `Authorization: Basic
ZGVtbZpwQDU1dzByZA==`
security:
- Basic: []
tags:
- name: Media
Steps to reproduce
Reproducible by using the generated SDK and designating a specific content-type in the UploadMedia call. If you check what content-type header is actually being sent out it is different from what was specified by the user
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the generated Java SDK's selectHeaderContentType function and the UploadMedia call in the reproduced SDK. Check how the user-supplied Content-Type reaches header selection and add or update coverage for a non-JSON type such as image/jpeg. Done means an explicitly passed Content-Type is preserved rather than replaced with application/json.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100