Skip to content

Commit a4c82c8

Browse files
authored
Fix #5020: add support for @JsonProperty.isRequired (#5021)
1 parent 9b3c372 commit a4c82c8

File tree

4 files changed

+8
-3
lines changed

4 files changed

+8
-3
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Project: jackson-databind
6262
fails on JDK 17+
6363
#4997: `ObjectNode` put methods should do null check for key
6464
#5014: Add `java.lang.Runnable` as unsafe base type in `DefaultBaseTypeLimitingValidator`
65+
#5020: Support new `@JsonProperty.isRequired` for overridable definition of "required-ness"
6566
6667
2.18.3 (28-Feb-2025)
6768

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

+5
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ public Boolean hasRequiredMarker(AnnotatedMember m)
462462
{
463463
JsonProperty ann = _findAnnotation(m, JsonProperty.class);
464464
if (ann != null) {
465+
// 11-Mar-2025, tatu: [databind#5020] Support new "isRequired" annotation
466+
OptBoolean required = ann.isRequired();
467+
if (required != OptBoolean.DEFAULT) {
468+
return required.asBoolean();
469+
}
465470
return ann.required();
466471
}
467472
return null;

src/test/java/com/fasterxml/jackson/databind/deser/creators/RequiredCreatorTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static class FascistPoint {
2020

2121
@JsonCreator
2222
public FascistPoint(@JsonProperty(value="x", required=true) Integer x,
23-
@JsonProperty(value="y", required=false) Integer y)
23+
@JsonProperty(value="y", isRequired=OptBoolean.FALSE) Integer y)
2424
{
2525
this.x = x;
2626
this.y = y;
@@ -34,7 +34,7 @@ static class LoginUserResponse {
3434
private String userType;
3535

3636
@JsonCreator
37-
public LoginUserResponse(@JsonProperty(value = "otp", required = true) String otp,
37+
public LoginUserResponse(@JsonProperty(value = "otp", isRequired = OptBoolean.TRUE) String otp,
3838
@JsonProperty(value = "userType", required = true) String userType) {
3939
this.otp = otp;
4040
this.userType = userType;

src/test/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospectorTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public static class DummyBuilder extends StdTypeResolverBuilder
129129
@JsonTypeResolver(DummyBuilder.class)
130130
static class TypeResolverBean { }
131131

132-
// @since 1.7
133132
@JsonIgnoreType
134133
static class IgnoredType { }
135134

0 commit comments

Comments
 (0)