Skip to content

Commit

Permalink
Merge pull request #24 from cfieber/objectmapper_configurer
Browse files Browse the repository at this point in the history
Changes mapper configuration to a helper method.
  • Loading branch information
cfieber authored Oct 11, 2016
2 parents 661e099 + 81ac586 commit 2b4fa8e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.netflix.awsobjectmapper;

import com.google.common.collect.ImmutableSet;
import com.amazonaws.services.ecs.model.VersionInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.ClassPath;
import com.google.common.io.Resources;

Expand Down Expand Up @@ -63,7 +64,8 @@ private boolean isModelClass(Class<?> c) {

@Test
public void mapRandomAwsObjects() throws Exception {
final AmazonObjectMapper mapper = new AmazonObjectMapper();
final ObjectMapper mapper = new ObjectMapper();
AmazonObjectMapperConfigurer.configure(mapper);
final Populator p = (new PopulatorBuilder()).build();
final Set<ClassPath.ClassInfo> classes = ClassPath
.from(getClass().getClassLoader())
Expand All @@ -72,23 +74,34 @@ public void mapRandomAwsObjects() throws Exception {
if (cinfo.getName().contains(".model.")
&& !cinfo.getSimpleName().startsWith("GetConsole")
&& !cinfo.getName().contains(".s3.model.")) { // TODO: problem with CORSRule
System.out.println(cinfo.getName());
final Class<?> c = cinfo.load();
if (isModelClass(c)) {
Object obj = p.populateBean(c);
String j1 = mapper.writeValueAsString(obj);
Object d1 = mapper.readValue(j1, c);
String j2 = mapper.writeValueAsString(d1);
Assert.assertEquals(j1, j2);
//System.out.println(c.getName() + " => " + obj);
}
}
}
}

@Test
public void namingStrategy() throws Exception {
@SuppressWarnings("deprecation")
public void testDeprecatedMapper() throws Exception {
final AmazonObjectMapper mapper = new AmazonObjectMapper();
final Populator p = new PopulatorBuilder().build();
Object obj = p.populateBean(VersionInfo.class);
String j1 = mapper.writeValueAsString(obj);
Object d1 = mapper.readValue(j1, VersionInfo.class);
String j2 = mapper.writeValueAsString(d1);
Assert.assertEquals(j1, j2);
}

@Test
public void namingStrategy() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
AmazonObjectMapperConfigurer.configure(mapper);
byte[] json = Resources.toByteArray(Resources.getResource("recordSet.json"));
ResourceRecordSet recordSet = mapper.readValue(json, ResourceRecordSet.class);
Assert.assertEquals(60L, (long) recordSet.getTTL());
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ buildscript {

plugins {
id 'nebula.netflixoss' version '3.4.0'
id 'nebula.resolution-rules' version '2.0.0'
}

// Establish version and status
ext.githubProjectName = 'awsobjectmapper'

allprojects {
apply plugin: 'nebula.resolution-rules'
apply plugin: 'nebula.netflixoss'
apply plugin: 'java'
apply plugin: 'idea'
Expand All @@ -37,8 +39,9 @@ subprojects {
}

dependencies {
resolutionRules 'com.netflix.nebula:gradle-resolution-rules:0.32.1'
compile 'com.amazonaws:aws-java-sdk:1.11.39'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.6'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.3'
testCompile 'junit:junit:4.10'
testCompile 'com.google.guava:guava:18.0'
testCompile 'io.github.benas:jpopulator:1.0.1'
Expand Down
56 changes: 42 additions & 14 deletions buildSrc/src/main/groovy/AwsMixinGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class AwsMixinGenerator implements Plugin<Project> {
new File(dir, "${mixinName}.java").withWriter { w ->
createMixin(w, prefix, it.load())
}
out.writeLine(" addMixIn(${it.name}.class, ${mixinName}.class);")
out.writeLine(" objectMapper.addMixIn(${it.name}.class, ${mixinName}.class);")
}
}

Expand All @@ -196,8 +196,8 @@ class AwsMixinGenerator implements Plugin<Project> {
URL[] compileClasspath = project.configurations.getByName('compile').files.collect { it.toURI().toURL() }
ClassLoader cl = new URLClassLoader(compileClasspath)

new File(outputDir, "AmazonObjectMapper.java").withWriter { out ->
out.writeLine(mapperHeader)
new File(outputDir, "AmazonObjectMapperConfigurer.java").withWriter { out ->
out.writeLine(mapperConfigurerHeader)
String pkg = "com.amazonaws"
ClassPath.from(cl).getTopLevelClassesRecursive(pkg).each { cinfo ->
Matcher m = clientPattern.matcher(cinfo.simpleName)
Expand Down Expand Up @@ -226,6 +226,9 @@ class AwsMixinGenerator implements Plugin<Project> {
createNameForSetterMethod(out, outputDir, overrides)
out.writeLine("}\n")
}
new File(outputDir, "AmazonObjectMapper.java").withWriter { out ->
out.writeLine(mapper);
}
})
}

Expand All @@ -247,25 +250,30 @@ class AwsMixinGenerator implements Plugin<Project> {
*/
"""

def mapperHeader = """\
def mapperConfigurerHeader = """\
$licenseHeader
package com.netflix.awsobjectmapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class AmazonObjectMapper extends ObjectMapper {
public AmazonObjectMapper() {
configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
configure(SerializationFeature.INDENT_OUTPUT, true);
configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
setPropertyNamingStrategy(new AmazonNamingStrategy());
public class AmazonObjectMapperConfigurer {
public static ObjectMapper createConfigured() {
ObjectMapper objectMapper = new ObjectMapper();
configure(objectMapper);
return objectMapper;
}
public static void configure(ObjectMapper objectMapper) {
objectMapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setPropertyNamingStrategy(new AmazonNamingStrategy());
"""

def mixinHeader = """\
Expand All @@ -287,5 +295,25 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
class AmazonNamingStrategy extends PropertyNamingStrategy {
"""

def mapper = """\
$licenseHeader
package com.netflix.awsobjectmapper;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* AmazonObjectMapper.
*
* @deprecated Use AmazonObjectMapperConfigurer and supply an ObjectMapper
*/
@Deprecated
public class AmazonObjectMapper extends ObjectMapper {
public AmazonObjectMapper() {
AmazonObjectMapperConfigurer.configure(this);
}
}
"""
}

0 comments on commit 2b4fa8e

Please sign in to comment.