Skip to content

ServerHttpRequest.Builder.contextPath() does not accept null but the implementation is nullable #37098

Description

@garvit-joshi

While working on spring-cloud/spring-cloud-gateway#4129 I needed to clear the context path
of a request through mutate(). My first try was contextPath(null) but NullAway fails
the build with:

passing @Nullable parameter 'null' where @NonNull is required

The package is @NullMarked and the builder method dosen't have @Nullable on the
parameter, so null is not allowed. I ended up using contextPath("") as a workarround.
In the review it was asked if null could work here instead, discussion is here:
spring-cloud/spring-cloud-gateway#4129 (comment)

The confusing part is that everything below the builder method already accepts null:

  • DefaultServerHttpRequestBuilder stores it in a @Nullable String contextPath field
  • MutatedServerHttpRequest constructor also takes @Nullable String contextPath
  • DefaultRequestPath.initContextPath() handles null and treats it same as empty string

So only the public builder method forbids null. Is this intentional or an oversight from
the JSpecify migration? If its an oversight, can the parameter be marked @Nullable?

Checked on 7.0.8 and also on current main, same behaviour in both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions