-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Required primitive properties #3392
Comments
One quick note: There is nothing particular about primitives as far as I know, however. |
This behavior is also a problem in I think a failure should occur for unexpected input. |
I will share the workaround I have found. The following is an example of how this can be accomplished by implementing an import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import java.lang.Class;
public class AI extends NopAnnotationIntrospector {
@Override
public JavaType refineDeserializationType(
final MapperConfig<?> config,
final Annotated a,
final JavaType baseType
) {
if (a instanceof AnnotatedParameter && baseType.isPrimitive()) {
Class<?> primitiveType = baseType.getRawClass();
return config.getTypeFactory().constructType(TODO("convert primitiveType to wrapperType"));
} else {
return baseType;
}
}
} |
@k163377 I think your problem is slightly different from original authors. I am not sure I follow request on But I guess one possible corner-case that could be addressed is that of:
which should result in failure. If not, that's a bug. Otherwise I think you mentioned that supporting Kotlin's nullability aspects is difficult -- this is true. But I think separate issue(s) are needed; I think you have already filed some? |
Oh, I must have been mistaken. import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
public class Github3392 {
public static class Dto {
public final int foo;
public final int bar;
public final String baz;
public final String qux;
@JsonCreator
public Dto(
@JsonProperty(value = "foo", required = false) int foo,
@JsonProperty(value = "bar", required = true) int bar,
@JsonProperty(value = "baz", required = false) String baz,
@JsonProperty(value = "qux", required = true) String qux
) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
this.qux = qux;
}
}
@Test
public void test() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// not fail, foo:0, bar:0, baz:null, qux: null
Dto dto1 = mapper.readValue("{\"foo\":null,\"bar\":null,\"baz\":null,\"qux\":null}", Dto.class);
// fails
Dto dto2 = mapper.readValue("{}", Dto.class);
}
} On the other hand, this isssue is about setters and not about creators, right? |
It seems to me that the second case should (and does) throw an exception, but the first case has values for all properties so it should pass. Nothing in settings forbids nulls, unless And when enabling that setting, exception is indeed thrown. But the original report is too vague to specify actual problem -- there is no reproduction, just general description lacking info on whether setters or constructor is used (for example). |
Ok, since I can not reproduce reported issue, will close -- may be re-opened with actual reproduction. But anyone finding this issue please note that you need to use Creator (with |
Kotlin |
@Boeing1337 for Kotlin-specific issues, please file on |
Is your feature request related to a problem? Please describe.
I have a request model containing IDs which are numbers (
long
),In order to make them required I need to make them nullable (
Long
) and add@JsonSettter(nulls = Nulls.FAIL)
There is
DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES
, but it only works when property in JSON explicitly set tonull
. But when the property itself is missing it does not work, but simply sets the value to0
Describe the solution you'd like
DeserializationFeature.FAIL_ON_MISSING_PROPERTIES
and/or@JsonProperty(required = true)
to work for primitives as well.The text was updated successfully, but these errors were encountered: