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 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ private boolean _addExplicitDelegatingCreator(DeserializationContext ctxt,
int ix = -1;
final int argCount = candidate.paramCount();
SettableBeanProperty[] properties = new SettableBeanProperty[argCount];
if (argCount == 0) {
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 com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.newJsonMapper;
import static org.junit.jupiter.api.Assertions.assertSame;

// [databind#4688]
public class SingletonDelegatingCreatorTest
{

static final class NoFieldSingletonWithDelegatingCreator {
private 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 {
private static final NoFieldSingletonWithPropertiesCreator INSTANCE = new NoFieldSingletonWithPropertiesCreator();

private NoFieldSingletonWithPropertiesCreator() {}

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

static final class NoFieldSingletonWithDefaultCreator {
private 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);
}
}