Skip to content

Fix #4688 2.18.0-rc1 Regression deserializing with no-arg delegating JsonCreator #4689

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,8 @@ Carter Kozak (carterkozak@github)
* Contributed #3876: `TypeFactory` cache performance degradation with
`constructSpecializedType()`
(2.15.0)
* Contributed #4688: Should allow deserializing with no-arg `@JsonCreator(mode = DELEGATING)`
(2.18.0)

Reinhard Prechtl (dnno@github)
* Reported #2034: Serialization problem with type specialization of nested generic types
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Project: jackson-databind
(contributed by Sim Y-T)
#4678: Java records don't serialize with `MapperFeature.REQUIRE_SETTERS_FOR_GETTERS`
(reported by Mathijs V)
#4688: Should allow deserializing with no-arg `@JsonCreator(mode = DELEGATING)`
(contributed by Carter K)

2.17.2 (05-Jul-2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ private boolean _addExplicitDelegatingCreator(DeserializationContext ctxt,
int ix = -1;
final int argCount = candidate.paramCount();
SettableBeanProperty[] properties = new SettableBeanProperty[argCount];
// [databind#4688]: Should still accept 0-arg (explicitly delegated) creator
// for backwards-compatibility (worked in 2.17 and before)
if (argCount == 0) {
// "Convert" to property-based since that works well
creators.addPropertyCreator(candidate.creator(), true, properties);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not entirely clear if we should use a property creator with no properties, or creators.setDefaultCreator(candidate.creator());. The former seems more explicit, but the latter more closely matches the previous implementation.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with your choice; let's go with that (possible to change, too, if need arises).

return true;
}
for (int i = 0; i < argCount; ++i) {
AnnotatedParameter param = candidate.parameter(i);
JacksonInject.Value injectId = candidate.injection(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.fasterxml.jackson.databind.deser.creators;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertSame;

// [databind#4688]
public class SingletonDelegatingCreatorTest extends DatabindTestUtil
{
static final class NoFieldSingletonWithDelegatingCreator {
static final NoFieldSingletonWithDelegatingCreator INSTANCE = new NoFieldSingletonWithDelegatingCreator();

private NoFieldSingletonWithDelegatingCreator() {}

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
Copy link
Member

Choose a reason for hiding this comment

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

Ok so would the idea here be to basically drop any JSON value that would match this type?
(I guess it must).

This was not something intended to work, although if it did use to work there's something to be said for not changing behavior. Will add a note on issue itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, our intent was to explicitly match an empty json object on the server-side (where we fail-on-unknown-fields) and allow clients which do not set that property to be api-compatible with future additions to the type.

static NoFieldSingletonWithDelegatingCreator of() {
return INSTANCE;
}
}

static final class NoFieldSingletonWithPropertiesCreator {
static final NoFieldSingletonWithPropertiesCreator INSTANCE = new NoFieldSingletonWithPropertiesCreator();

private NoFieldSingletonWithPropertiesCreator() {}

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
static NoFieldSingletonWithPropertiesCreator of() {
return INSTANCE;
}
}

static final class NoFieldSingletonWithDefaultCreator {
static final NoFieldSingletonWithDefaultCreator INSTANCE = new NoFieldSingletonWithDefaultCreator();

private NoFieldSingletonWithDefaultCreator() {}

@JsonCreator
static NoFieldSingletonWithDefaultCreator of() {
return INSTANCE;
}
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

@Test
public void testNoFieldSingletonWithDelegatingCreator() throws Exception
{
NoFieldSingletonWithDelegatingCreator deserialized = MAPPER.readValue("{}",
NoFieldSingletonWithDelegatingCreator.class);
assertSame(NoFieldSingletonWithDelegatingCreator.INSTANCE, deserialized);
}

@Test
public void testNoFieldSingletonWithPropertiesCreator() throws Exception
{
NoFieldSingletonWithPropertiesCreator deserialized = MAPPER.readValue("{}",
NoFieldSingletonWithPropertiesCreator.class);
assertSame(NoFieldSingletonWithPropertiesCreator.INSTANCE, deserialized);
}

@Test
public void testNoFieldSingletonWithDefaultCreator() throws Exception
{
NoFieldSingletonWithDefaultCreator deserialized = MAPPER.readValue("{}",
NoFieldSingletonWithDefaultCreator.class);
assertSame(NoFieldSingletonWithDefaultCreator.INSTANCE, deserialized);
}
}