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

Move SimpleBuilderDeserializer into SpecificObjectDeserializer #583

Merged
merged 1 commit into from
Sep 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ class BuilderSpec extends Specification {
then:
result == json
}

void "test deserialize builder on subtype"() {
given:
def json = '{"sub":{"foo":"fizz","bar":"buzz"}}'

when:
def value = objectMapper.readValue(json, TestBuildSupertype)

then:
value.foo == 'fizz'
value.bar == 'buzz'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.micronaut.serde.jackson.builder;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize(builder = TestBuildSubtype.Builder.class)
public class TestBuildSubtype extends TestBuildSupertype {
private final String bar;

private TestBuildSubtype(String foo, String bar) {
super(foo);
this.bar = bar;
}

public String getBar() {
return bar;
}

public static class Builder {
private String foo;
private String bar;

public Builder foo(String foo) {
this.foo = foo;
return this;
}

public Builder bar(String bar) {
this.bar = bar;
return this;
}

public TestBuildSubtype build() {
return new TestBuildSubtype(foo, bar);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micronaut.serde.jackson.builder;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonSubTypes({
@JsonSubTypes.Type(value = TestBuildSubtype.class, name = "sub")
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
public abstract class TestBuildSupertype {
private final String foo;

TestBuildSupertype(String foo) {
this.foo = foo;
}

public String getFoo() {
return foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ public Deserializer<Object> createSpecific(DecoderContext context, Argument<? su
return (Decoder decoder, DecoderContext context1, Argument<? super Object> type1) -> decoder.decodeArbitrary();
}
DeserBean<? super Object> deserBean = getDeserializableBean(type, context);
if (deserBean.hasBuilder) {
return new SimpleBuilderDeserializer(
deserBean.readProperties,
deserBean.introspection,
preInstantiateCallback,
ignoreUnknown,
strictNullable
);
}
if (deserBean.simpleBean) {
return new SimpleObjectDeserializer(ignoreUnknown, strictNullable, deserBean, preInstantiateCallback);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.reflect.exception.InstantiationException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArrayUtils;
Expand Down Expand Up @@ -328,6 +329,42 @@ public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argume
decoderContext
);
}
} else if (db.hasBuilder) {
BeanIntrospection.Builder<? super Object> builder;
try {
if (preInstantiateCallback != null) {
preInstantiateCallback.preInstantiate(db.introspection);
}
builder = db.introspection.builder();
} catch (InstantiationException e) {
throw new SerdeException(PREFIX_UNABLE_TO_DESERIALIZE_TYPE + type + "]: " + e.getMessage(), e);
}
if (hasProperties) {
while (true) {
final String prop = objectDecoder.decodeKey();
if (prop == null) {
break;
}
final DeserBean.DerProperty<Object, Object> property = readProperties.consume(prop);
if (property != null) {
property.deserializeAndCallBuilder(objectDecoder, decoderContext, builder);
} else {
skipOrSetAny(
decoderContext,
objectDecoder,
prop,
anyValues,
ignoreUnknown,
type
);
}
}
}
try {
obj = builder.build();
} catch (InstantiationException e) {
throw new SerdeException(PREFIX_UNABLE_TO_DESERIALIZE_TYPE + type + "]: " + e.getMessage(), e);
}
} else {
try {
if (preInstantiateCallback != null) {
Expand Down