Skip to content

Commit df8b4b1

Browse files
committed
Merge pull request #918 from dharaburda/rename-explicit
Add mapper feature to allow explicitly named properties to be renamed with PropertyNamingStrategy
2 parents 77f8e89 + b6a26b9 commit df8b4b1

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/MapperFeature.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ public enum MapperFeature implements ConfigFeature
331331
*/
332332
USE_STD_BEAN_NAMING(false),
333333

334+
/**
335+
* Feature that when enabled will allow explicitly named properties (i.e., fields or methods
336+
* annotated with {@link com.fasterxml.jackson.annotation.JsonProperty}("explicitName")) to
337+
* be re-named by a {@link PropertyNamingStrategy}, if one is configured.
338+
* <p>
339+
* Feature is disabled by default.
340+
*
341+
* @since 2.6
342+
*/
343+
ALLOW_EXPLICIT_PROPERTY_RENAMING(false),
344+
334345
/*
335346
/******************************************************
336347
/* Other features

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,9 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
785785
PropertyName fullName = prop.getFullName();
786786
String rename = null;
787787
// As per [#428](https://github.com/FasterXML/jackson-databind/issues/428) need
788-
// to skip renaming if property has explicitly defined name
789-
if (!prop.isExplicitlyNamed()) {
788+
// to skip renaming if property has explicitly defined name, unless feature
789+
// is enabled
790+
if (!prop.isExplicitlyNamed() || _config.isEnabled(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)) {
790791
if (_forSerialization) {
791792
if (prop.hasGetter()) {
792793
rename = naming.nameForGetterMethod(_config, prop.getGetter(), fullName.getSimpleName());

src/test/java/com/fasterxml/jackson/databind/introspect/TestNamingStrategyStd.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.annotation.*;
99
import com.fasterxml.jackson.databind.BaseMapTest;
10+
import com.fasterxml.jackson.databind.MapperFeature;
1011
import com.fasterxml.jackson.databind.ObjectMapper;
1112
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
1213
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@@ -316,4 +317,41 @@ public void testNamingWithObjectNode() throws Exception
316317
assertEquals(2, result.json.size());
317318
assertEquals("bing", result.json.path("baz").asText());
318319
}
320+
321+
static class ExplicitBean {
322+
@JsonProperty("firstName")
323+
String userFirstName = "Peter";
324+
@JsonProperty("lastName")
325+
String userLastName = "Venkman";
326+
@JsonProperty
327+
String userAge = "35";
328+
}
329+
330+
public void testExplicitRename() throws Exception {
331+
ObjectMapper m = new ObjectMapper();
332+
m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
333+
m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
334+
// by default, renaming will not take place on explicitly named fields
335+
assertEquals(aposToQuotes("{'firstName':'Peter','lastName':'Venkman','user_age':'35'}"),
336+
m.writeValueAsString(new ExplicitBean()));
337+
338+
m = new ObjectMapper();
339+
m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
340+
m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
341+
m.enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING);
342+
// w/ feature enabled, ALL property names should get re-written
343+
assertEquals(aposToQuotes("{'first_name':'Peter','last_name':'Venkman','user_age':'35'}"),
344+
m.writeValueAsString(new ExplicitBean()));
345+
346+
// test deserialization as well
347+
ExplicitBean bean =
348+
m.readValue(aposToQuotes("{'first_name':'Egon','last_name':'Spengler','user_age':'32'}"),
349+
ExplicitBean.class);
350+
351+
assertNotNull(bean);
352+
assertEquals("Egon", bean.userFirstName);
353+
assertEquals("Spengler", bean.userLastName);
354+
assertEquals("32", bean.userAge);
355+
356+
}
319357
}

0 commit comments

Comments
 (0)