Skip to content

[BUG][JAVA][SPRING] getNativeResponse could return null and must be enforced #22602

@pkernevez

Description

@pkernevez

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.
#22598

Suggest a fix

See PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions