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

Add mapper feature to allow explicitly named properties to be renamed with PropertyNamingStrategy #918

Merged
merged 2 commits into from
Sep 10, 2015
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
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ public enum MapperFeature implements ConfigFeature
*/
USE_STD_BEAN_NAMING(false),

/**
* Feature that when enabled will allow explicitly named properties (i.e., fields or methods
* annotated with {@link com.fasterxml.jackson.annotation.JsonProperty}("explicitName")) to
* be re-named by a {@link PropertyNamingStrategy}, if one is configured.
* <p>
* Feature is disabled by default.
*
* @since 2.6
*/
ALLOW_EXPLICIT_PROPERTY_RENAMING(false),

/*
/******************************************************
/* Other features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,9 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
PropertyName fullName = prop.getFullName();
String rename = null;
// As per [#428](https://github.com/FasterXML/jackson-databind/issues/428) need
// to skip renaming if property has explicitly defined name
if (!prop.isExplicitlyNamed()) {
// to skip renaming if property has explicitly defined name, unless feature
// is enabled
if (!prop.isExplicitlyNamed() || _config.isEnabled(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING)) {
if (_forSerialization) {
if (prop.hasGetter()) {
rename = naming.nameForGetterMethod(_config, prop.getGetter(), fullName.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
Expand Down Expand Up @@ -316,4 +317,41 @@ public void testNamingWithObjectNode() throws Exception
assertEquals(2, result.json.size());
assertEquals("bing", result.json.path("baz").asText());
}

static class ExplicitBean {
@JsonProperty("firstName")
String userFirstName = "Peter";
@JsonProperty("lastName")
String userLastName = "Venkman";
@JsonProperty
String userAge = "35";
}

public void testExplicitRename() throws Exception {
ObjectMapper m = new ObjectMapper();
m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
// by default, renaming will not take place on explicitly named fields
assertEquals(aposToQuotes("{'firstName':'Peter','lastName':'Venkman','user_age':'35'}"),
m.writeValueAsString(new ExplicitBean()));

m = new ObjectMapper();
m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
m.enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING);
// w/ feature enabled, ALL property names should get re-written
assertEquals(aposToQuotes("{'first_name':'Peter','last_name':'Venkman','user_age':'35'}"),
m.writeValueAsString(new ExplicitBean()));

// test deserialization as well
ExplicitBean bean =
m.readValue(aposToQuotes("{'first_name':'Egon','last_name':'Spengler','user_age':'32'}"),
ExplicitBean.class);

assertNotNull(bean);
assertEquals("Egon", bean.userFirstName);
assertEquals("Spengler", bean.userLastName);
assertEquals("32", bean.userAge);

}
}