OpenAPITools / OpenAPITools/openapi-generator
[BUG] Incompatible Content type handling leading to NPE
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Version 5.2.1 generated Java class ApiClient which contained method
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) {
**return "application/json";**
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
Version 5.3.x (0 and 1) generates modified body of the method
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];
}
Returning null when argument is empty finally causes NPE in existing code. I guess that we should avoid returning as a rule of clean code.
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?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
NPE thrown in a java client code
openapi-generator version
5.3.x
5.2.x works fine
Suggest a fix
instead of
if (contentTypes.length == 0) {
return null;
}
use
if (contentTypes.length == 0) {
return "application/json";
}
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 with the generated Java ApiClient method selectHeaderContentType shown in the issue and compare its 5.2.x and 5.3.x behavior for an empty contentTypes array. Verify that the generated client no longer returns null in this case and that the existing NPE scenario is prevented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100