Skip to content

Support type-use @Nullable annotation in annotated service #6457

Merged
minwoox merged 9 commits intoline:mainfrom
sh-cho:allow-type-use-nullable
Nov 13, 2025
Merged

Support type-use @Nullable annotation in annotated service #6457
minwoox merged 9 commits intoline:mainfrom
sh-cho:allow-type-use-nullable

Conversation

@sh-cho
Copy link
Copy Markdown
Contributor

@sh-cho sh-cho commented Oct 26, 2025

Motivation:

In annotated service, type-use @Nullable annotation (ex. jspecify) is not respected.

Modifications:

Result:

Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
@minwoox minwoox added this to the 1.34.0 milestone Oct 30, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 30, 2025

Codecov Report

❌ Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.11%. Comparing base (8150425) to head (c016834).
⚠️ Report is 229 commits behind head on main.

Files with missing lines Patch % Lines
...rnal/server/annotation/AnnotatedValueResolver.java 81.25% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #6457      +/-   ##
============================================
- Coverage     74.46%   74.11%   -0.35%     
- Complexity    22234    23005     +771     
============================================
  Files          1963     2062      +99     
  Lines         82437    86144    +3707     
  Branches      10764    11314     +550     
============================================
+ Hits          61385    63846    +2461     
- Misses        15918    16883     +965     
- Partials       5134     5415     +281     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left small suggestions. Thanks, @sh-cho!

}

static boolean isAnnotatedNullable(AnnotatedElement annotatedElement) {
// 1) declaration annotation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add examples so that readers can distinguish them easily? e.g.

@Nullable // declaration annotation
public String declarationAnnotatedMethod() {
    return null;
}

// type-use annotation
public @Nullable String typeUseAnnotatedMethod() {
    return null;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I added comment
ca1e429 (#6457)

}

@Get("/type_use_nullable")
public String typeUseNullable(@Header @org.jspecify.annotations.Nullable String value) {
Copy link
Copy Markdown
Contributor

@minwoox minwoox Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test with this?

public @Nullable String typeUseAnnotatedMethod() {
    return null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may use @RequestObject to test it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test for nullable response
8d850d9 (#6457)

Also I tried to test with @RequestObject like below, but looks like @Nullable doesn't affect 👀 (empty body is rejected even if there's @Nullable)

@Post("/foo")
@ProducesJson
public Map<String, Object> foo(@RequestObject Map<String, Object> map) {
    return map;
}

@Post("/bar")
@ProducesJson
public Map<String, Object> bar(@RequestObject @Nullable Map<String, Object> map) {
    if (map == null) {
        return Map.of("this is", "null");
    }
    return map;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty body is rejected even if there's @nullable

I think it shouldn't throw an exception if it's annotated with the @Nullable.
What do you think of it? @line/dx

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the behavior sounds reasonable - I'm not sure whether this is enforceable for all cases though. I have no objection if for Jackson's case where a literal null body is received, a null value is passed as the request object.

Having said this, I'm fine with going ahead with merging this PR and handling this separately as they seem to be separate issues

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with going ahead with merging this PR and handling this separately as they seem to be separate issues

I agree. We don't have to deal with this in this PR. Let me merge this. 😉

Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
@minwoox minwoox merged commit 21e4000 into line:main Nov 13, 2025
14 of 16 checks passed
@minwoox
Copy link
Copy Markdown
Contributor

minwoox commented Nov 13, 2025

Thanks a lot, @sh-cho! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@Nullable TYPE_USE annotation is not respected in annotated service

4 participants