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

Support referencing self in serialize/deserialize as #777

Merged
merged 1 commit into from
Feb 27, 2024
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 @@ -2,6 +2,54 @@ package io.micronaut.serde.jackson

abstract class JsonSerializeDeserializeSpec extends JsonCompileSpec {

void 'test json serialize/deserialize as self'() {
given:
def context = buildContext('test.TestImpl', """
package test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.serde.annotation.Serdeable;

interface Test {
String getValue();
}

@Serdeable
@JsonSerialize(as = TestImpl.class)
@JsonDeserialize(as = TestImpl.class)
class TestImpl implements Test {

private final String value;

@JsonCreator
TestImpl(@JsonProperty("value") String value) {
this.value = value;
}

@Override
public String getValue() {
return value;
}
}
""")

when:
def result = jsonMapper.readValue('{"value":"test"}', typeUnderTest)

then:
result.getClass().name == 'test.TestImpl'
result.value == 'test'

when:
def json = writeJson(jsonMapper, result)

then:
json == '{"value":"test"}'
}

void 'test json serialize/deserialize as'() {
given:
def context = buildContext('test.Test', """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public <T> BeanIntrospection<T> getDeserializableIntrospection(Argument<T> type)
declaredMetadata.hasDeclaredAnnotation(DefaultImplementation.class)) {
deserializeType = introspection.classValue(DefaultImplementation.class).orElse(null);
}
if (deserializeType != null) {
if (deserializeType != null && !deserializeType.equals(type.getType())) {
Argument resolved = Argument.of(
deserializeType,
type.getName(),
Expand All @@ -197,10 +197,9 @@ public <T> BeanIntrospection<T> getDeserializableIntrospection(Argument<T> type)
}
}

private <T> Class<?> resolveDeserAsType(
Class<?> beanType,
AnnotationValue<SerdeConfig> serdeConfig,
String configMember) {
private Class<?> resolveDeserAsType(Class<?> beanType,
AnnotationValue<SerdeConfig> serdeConfig,
String configMember) {
Class<?> deserializeType = null;
if (serdeConfig != null) {
deserializeType = serdeConfig.classValue(configMember).orElse(null);
Expand Down
Loading