-
-
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
Allow @JsonCreator
annotated Creator to retun null
#4938
Comments
Seems like reasonable request, but this request conflicts with current behavior. So either of ....
|
Here is reproduction public class JsonCreatoreReturningNull4938Test
extends DatabindTestUtil
{
// The class with the JsonCreator static factory method
static class Localized3 {
public final String en;
public final String de;
public final String fr;
@JsonCreator
public static Localized3 of(
@JsonProperty("en") String en,
@JsonProperty("de") String de,
@JsonProperty("fr") String fr) {
if (en == null && de == null && fr == null) {
return null; // Explicitly return null when all arguments are null
}
return new Localized3(en, de, fr);
}
private Localized3(String en, String de, String fr) {
this.en = en;
this.de = de;
this.fr = fr;
}
}
private final ObjectMapper MAPPER = newJsonMapper();
@Test
void testDeserializeToNullWhenAllPropertiesAreNull() throws Exception {
// JSON input where all properties are null
String json = "{ \"en\": null, \"de\": null, \"fr\": null }";
// Deserialize JSON to Localized3 object
Localized3 result = MAPPER.readValue(json, Localized3.class);
// Verify that the result is null
assertNull(result, "Deserialization should result in null when all properties are null");
}
} |
@JooHyukKim thanks for considering my request and adding the missing test case! That's very nice. I also read the referred issue 597 but no one there seems to demonstrate a usage of the current behavior and I could not think of one either. My guess would be a better exception helped debugging code or simply throwing better error message. But a DeserializationFeat would also work. But you seems to limit them? |
@cowtowncoder WDYT? |
What is the current exception? To me, ability to return If we do want it to be optional, |
Oh. I think I may remember why However, I think in that case we should just skip following properties and take So I think I'm ok actually changing behavior for 2.19, without configurability. That is, unless someone can come up with a scenario where this should be prevented (configurably or not). |
So just make sure: I am +1 for allowing this, and working with @JooHyukKim on a pr to get implemented for 2.19. |
@JsonCreator
annotated Creator to retun null
Is your feature request related to a problem? Please describe.
When using JsonCreator on a factory (static) method, we have the possibility of returning null (because for example all the arguments are empty). However during deserialization an exception is thrown, java.lang.NullPointerException: JSON Creator returned nul.
I think it would make sense to allow deserialization to null
Describe the solution you'd like
Improve the code robustness in the BeanEntityDeserializer to allow Deserialization to null if a static JsonCreator explicitly allows it.
Usage example
should be allowed for Deserialization and { en: null, de: null, fr: null } should deserialize to null
Additional context
No response
The text was updated successfully, but these errors were encountered: