Skip to content
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

Change BeanDeserializerFactory.addBeanProps to check creator properties #4982

Open
wants to merge 1 commit into
base: 2.19
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@ protected void addBeanProps(DeserializationContext ctxt,
BeanDescription beanDesc, BeanDeserializerBuilder builder)
throws JsonMappingException
{
final boolean isConcrete = !beanDesc.getType().isAbstract();
final SettableBeanProperty[] creatorProps = isConcrete
? builder.getValueInstantiator().getFromObjectArguments(ctxt.getConfig())
final ValueInstantiator valueInstantiator = builder.getValueInstantiator();
final SettableBeanProperty[] creatorProps = (valueInstantiator != null)
? valueInstantiator.getFromObjectArguments(ctxt.getConfig())
: null;
final boolean hasCreatorProps = (creatorProps != null);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.fasterxml.jackson.databind.deser;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.jsonMapperBuilder;

/**
* Unit test for [databind#4920]: Creator properties are ignored on abstract types when collecting bean properties,
* breaking {@link com.fasterxml.jackson.databind.jsontype.impl.AsExternalTypeDeserializer}.
*/
public class BeanDeserializerFactory4920Test {

@Test
void testDeserializeAbstract() throws Exception {
ObjectMapper objectMapper = jsonMapperBuilder().build();

//language=JSON
String json = "{ \"value\": \"1234567890\", \"type\": \"" + StringValue.class.getName() + "\" }";

TypedData actual = objectMapper.readValue(json, TypedData.class);

Assertions.assertNotNull(actual);
Assertions.assertInstanceOf(StringValue.class, actual.getValue());
Assertions.assertEquals("1234567890", ((StringValue) actual.getValue()).getValue());
}

private interface TypedData {
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type", visible = true)
@JsonTypeIdResolver(ValueTypeIdResolver.class)
Value getValue();

String getType();

@JsonCreator
static TypedData immutableOf(@JsonProperty("value") Value value, @JsonProperty("type") String type) {
return new TypedData.Immutable(value, type);
}

final class Immutable implements TypedData {

private final Value value;
private final String type;

public Immutable(Value value, String type) {
this.value = value;
this.type = type;
}

@Override
public Value getValue() {
return value;
}

@Override
public String getType() {
return type;
}
}

final class ValueTypeIdResolver extends TypeIdResolverBase {
@Override
public String idFromValue(Object value) {
throw new UnsupportedOperationException();
}

@Override
public String idFromValueAndType(Object value, Class<?> suggestedType) {
throw new UnsupportedOperationException();
}

@Override
public JsonTypeInfo.Id getMechanism() {
return JsonTypeInfo.Id.CUSTOM;
}

@Override
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
Class<?> type;
try {
type = Class.forName(id);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}

return context.constructType(type);
}
}
}

private interface Value {
}

private static final class StringValue implements Value {

private final String value;

public StringValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

private static final class LongValue implements Value {

private final long value;

public LongValue(long value) {
this.value = value;
}

public long getValue() {
return value;
}
}
}