Skip to content

Commit 4c682f2

Browse files
authored
Merge pull request #180 from sammyhk/provide-ability-to-plug-in-pre-configured-objectmapper
Provide ability to plug in pre configured objectmapper
2 parents 30780ce + d891507 commit 4c682f2

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
package graphql.servlet;
22

3-
import com.fasterxml.jackson.databind.InjectableValues;
43
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.SerializationFeature;
6-
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
74

85
public class ConfiguringObjectMapperProvider implements ObjectMapperProvider {
96

7+
private final ObjectMapper objectMapperTemplate;
8+
109
private final ObjectMapperConfigurer objectMapperConfigurer;
1110

11+
public ConfiguringObjectMapperProvider(ObjectMapper objectMapperTemplate, ObjectMapperConfigurer objectMapperConfigurer) {
12+
this.objectMapperTemplate = objectMapperTemplate == null ? new ObjectMapper() : objectMapperTemplate;
13+
this.objectMapperConfigurer = objectMapperConfigurer == null ? new DefaultObjectMapperConfigurer() : objectMapperConfigurer;
14+
}
15+
16+
public ConfiguringObjectMapperProvider(ObjectMapper objectMapperTemplate) {
17+
this(objectMapperTemplate, null);
18+
}
19+
1220
public ConfiguringObjectMapperProvider(ObjectMapperConfigurer objectMapperConfigurer) {
13-
this.objectMapperConfigurer = objectMapperConfigurer;
21+
this(null, objectMapperConfigurer);
1422
}
1523

1624
public ConfiguringObjectMapperProvider() {
17-
this.objectMapperConfigurer = new DefaultObjectMapperConfigurer();
25+
this(null, null);
1826
}
1927

2028
@Override
2129
public ObjectMapper provide() {
22-
ObjectMapper mapper = new ObjectMapper().disable(
23-
SerializationFeature.FAIL_ON_EMPTY_BEANS).registerModule(new Jdk8Module());
24-
objectMapperConfigurer.configure(mapper);
25-
30+
ObjectMapper mapper = this.objectMapperTemplate.copy();
31+
this.objectMapperConfigurer.configure(mapper);
2632
return mapper;
2733
}
2834
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package graphql.servlet;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
34
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
47

58
/**
69
* @author Andrew Potter
710
*/
811
public class DefaultObjectMapperConfigurer implements ObjectMapperConfigurer {
12+
913
@Override
1014
public void configure(ObjectMapper mapper) {
11-
15+
// default configuration for GraphQL Java Servlet
16+
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
17+
mapper.registerModule(new Jdk8Module());
18+
mapper.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS);
1219
}
1320
}

src/main/java/graphql/servlet/GraphQLObjectMapper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package graphql.servlet;
22

3-
import com.fasterxml.jackson.annotation.JsonInclude;
43
import com.fasterxml.jackson.core.JsonProcessingException;
54
import com.fasterxml.jackson.core.type.TypeReference;
65
import com.fasterxml.jackson.databind.MappingIterator;
@@ -45,8 +44,7 @@ public ObjectMapper getJacksonMapper() {
4544
synchronized(this) {
4645
result = mapper;
4746
if (result == null) { // Second check (with locking)
48-
mapper = result = objectMapperProvider.provide().copy();
49-
mapper.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS);
47+
mapper = result = objectMapperProvider.provide();
5048
}
5149
}
5250
}

0 commit comments

Comments
 (0)