#1566 - Consider @JsonProperty-defined property name.
We now consider Jackson's @JsonProperty annotation to rename PropertyMetadata instances accordingly. Original pull request: #1594.
This commit is contained in:
@@ -448,7 +448,16 @@ public class PropertyUtils {
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return property.getName();
|
||||
|
||||
MergedAnnotation<JsonProperty> annotation = property.getAnnotation(JsonProperty.class);
|
||||
|
||||
if (!annotation.isPresent()) {
|
||||
return property.getName();
|
||||
}
|
||||
|
||||
String annotatedName = annotation.getString("value");
|
||||
|
||||
return StringUtils.hasText(annotatedName) ? annotatedName.trim() : property.getName();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
@@ -54,6 +55,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
@@ -191,6 +193,14 @@ class PropertyUtilsTest {
|
||||
return DynamicTest.stream(source, InputTypes::toString, it -> it.verify(metadata));
|
||||
}
|
||||
|
||||
@Test // #1563
|
||||
void considersJacksonRenamedProperty() {
|
||||
|
||||
InputPayloadMetadata metadata = PropertyUtils.getExposedProperties(JacksonCustomizations.class);
|
||||
|
||||
assertThat(getProperty(metadata, "renamed")).isPresent();
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@JsonIgnoreProperties({ "ignoreThisProperty" })
|
||||
@@ -283,6 +293,11 @@ class PropertyUtilsTest {
|
||||
@Range int ranged;
|
||||
}
|
||||
|
||||
@Value
|
||||
static class JacksonCustomizations {
|
||||
@JsonProperty("renamed") String property;
|
||||
}
|
||||
|
||||
// Test fixtures
|
||||
|
||||
@Value(staticConstructor = "of")
|
||||
@@ -303,4 +318,8 @@ class PropertyUtilsTest {
|
||||
return String.format("Expecting input type %s for %s.", type, property);
|
||||
}
|
||||
}
|
||||
|
||||
private static Optional<PropertyMetadata> getProperty(PayloadMetadata metadata, String name) {
|
||||
return metadata.stream().filter(it -> it.hasName(name)).findFirst();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user