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

Jackson module: Get the property name from the existing property if present #4959

Merged
merged 1 commit into from
Feb 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig confi
props = new SettableBeanProperty[constructorArguments.length];
for (int i = 0; i < constructorArguments.length; i++) {
Argument<?> argument = constructorArguments[i];
final JavaType javaType = existing != null && existing.length > i ? existing[i].getType() : newType(argument, typeFactory);
SettableBeanProperty existingProperty = existing != null && existing.length > i ? existing[i] : null;
final JavaType javaType = existingProperty != null ? existingProperty.getType() : newType(argument, typeFactory);
final AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
PropertyMetadata propertyMetadata = newPropertyMetadata(argument, annotationMetadata);
final String simpleName = annotationMetadata.stringValue(JsonProperty.class).orElse(argument.getName());
final String simpleName = existingProperty != null ? existingProperty.getName() : annotationMetadata.stringValue(JsonProperty.class).orElse(argument.getName());
TypeDeserializer typeDeserializer;
try {
typeDeserializer = config.findTypeDeserializer(javaType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.annotation.JsonView
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.annotation.JsonNaming
import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope
import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.Introspected
import io.micronaut.http.hateoas.JsonError
Expand All @@ -21,6 +24,8 @@ import io.micronaut.jackson.modules.wrappers.LongWrapper
import io.micronaut.jackson.modules.wrappers.StringWrapper
import spock.lang.Specification

import java.beans.ConstructorProperties

class BeanIntrospectionModuleSpec extends Specification {

void "Bean introspection works with a bean without JsonInclude annotations"() {
Expand Down Expand Up @@ -281,6 +286,21 @@ class BeanIntrospectionModuleSpec extends Specification {
ctx.close()
}

void "test deserializing with a json naming strategy"() {
given:
ApplicationContext ctx = ApplicationContext.run()
ObjectMapper objectMapper = ctx.getBean(ObjectMapper)

when:
NamingStrategy instance = objectMapper.readValue("{ \"FooBar\": \"bad\" }", NamingStrategy)

then:
instance.fooBar == "bad"

cleanup:
ctx.close()
}

@Introspected
static class Book {
@JsonProperty("book_title")
Expand Down Expand Up @@ -356,4 +376,16 @@ class BeanIntrospectionModuleSpec extends Specification {
static class PublicView {}
static class AllView extends PublicView {}

@Introspected
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
static class NamingStrategy {

@PackageScope
final String fooBar

@ConstructorProperties(["fooBar"])
NamingStrategy(String fooBar) {
this.fooBar = fooBar
}
}
}