OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SPRING] getNativeResponse could return null and must be enforced
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?
- 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
With Java & spring generator, when using JSpecify Nullable annotation.
Nullaway create a compilation error because the generated code is not nullsafe for ApiUtil.
openapi-generator version
7.18.2, this is not a regression
OpenAPI declaration file content or url
My Maven plugin conf:
<configuration>
<generatorName>spring</generatorName>
...
<importMappings>
<importMapping>Nullable=org.jspecify.annotations.Nullable</importMapping>
</importMappings>
</configuration>
Generation Details
The generated code is:
public class ApiUtil {
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
try {
HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); /* <== could be null because getNativeResponse return @Nullable */
res.setCharacterEncoding("UTF-8");
res.addHeader("Content-Type", contentType);
res.getWriter().print(example);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
The code should be:
public class ApiUtil {
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
try {
HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); /* <== could be null because getNativeResponse return @Nullable */
if (res != null) {
res.setCharacterEncoding("UTF-8");
res.addHeader("Content-Type", contentType);
res.getWriter().print(example);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Steps to reproduce
Setup JSpecify (like in my pom example) and NullAway.
Run the generator, when compiling the following Warns are generated:
[WARNING] .../target/generated-sources/src/main/java/tech/generated/api/ApiUtil.java:[12,16] [NullAway] dereferenced expression res is @Nullable
(see http://t.uber.com/nullaway )
Related issues/PRs
New bugs detected with the generalisation of JSpecify in Spring and other frameworks.
https://github.com/OpenAPITools/openapi-generator/issues/22598
Suggest a fix
See PR.
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
Inspect the Spring generator template or entry point responsible for emitting ApiUtil, using the shown Maven configuration and generated method as the starting point. Reproduce generation with JSpecify and NullAway, then verify that the generated response handling compiles without a nullable dereference warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100