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

Respect JsonProperty on outer property for builders #585

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 @@ -68,4 +68,15 @@ class BuilderSpec extends Specification {
value.getClass() == TestBuildSupertype2
value.foo == 'fizz'
}

void "test deserialize builder with JsonProperty on outer class"() {
given:
def json = '{"bar":"baz"}'

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

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

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

@JsonDeserialize(builder = TestBuildName.Builder.class)
public class TestBuildName {
@JsonProperty("bar")
private final String foo;

private TestBuildName(String foo) {
this.foo = foo;
}

public String getFoo() {
return foo;
}

public static class Builder {
private String foo;

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

public TestBuildName build() {
return new TestBuildName(foo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,15 @@ public DeserBean(
for (int i = 0; i < builderArguments.length; i++) {
Argument<Object> builderArgument = (Argument<Object>) builderArguments[i];
AnnotationMetadata annotationMetadata = builderArgument.getAnnotationMetadata();
Optional<BeanProperty<T, Object>> matchingOuterProperty = introspection.getProperty(builderArgument.getName());
PropertyNamingStrategy propertyNamingStrategy = getPropertyNamingStrategy(annotationMetadata, decoderContext, entityPropertyNamingStrategy);
final String jsonProperty = resolveName(builderArgument, annotationMetadata, propertyNamingStrategy);
final String jsonProperty = resolveName(
builderArgument,
matchingOuterProperty
.map(outer -> List.of(annotationMetadata, outer.getAnnotationMetadata()))
.orElse(List.of(annotationMetadata)),
propertyNamingStrategy
);
final DerProperty<T, Object> derProperty = new DerProperty<>(
conversionService,
introspection,
Expand Down Expand Up @@ -532,15 +539,24 @@ Map<String, Argument<?>> getBounds() {
}

private String resolveName(AnnotatedElement annotatedElement, AnnotationMetadata annotationMetadata, PropertyNamingStrategy namingStrategy) {
return resolveName(annotatedElement, List.of(annotationMetadata), namingStrategy);
}

private String resolveName(AnnotatedElement annotatedElement, List<AnnotationMetadata> annotationMetadata, PropertyNamingStrategy namingStrategy) {
if (namingStrategy != null) {
return namingStrategy.translate(annotatedElement);
}
return annotationMetadata
.stringValue(SerdeConfig.class, SerdeConfig.PROPERTY)
.orElseGet(() ->
annotationMetadata.stringValue(JK_PROP)
.orElseGet(annotatedElement::getName)
);
for (AnnotationMetadata metadataElement : annotationMetadata) {
Optional<String> serde = metadataElement.stringValue(SerdeConfig.class, SerdeConfig.PROPERTY);
if (serde.isPresent()) {
return serde.get();
}
Optional<String> jackson = metadataElement.stringValue(JK_PROP);
if (jackson.isPresent()) {
return jackson.get();
}
}
return annotatedElement.getName();
}

private static <T> Deserializer<T> findDeserializer(Deserializer.DecoderContext decoderContext, Argument<T> argument) throws SerdeException {
Expand Down