Skip to content

Provide ability to plug in pre configured objectmapper #180

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

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
28 changes: 15 additions & 13 deletions src/main/java/graphql/servlet/ConfiguringObjectMapperProvider.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package graphql.servlet;

import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

public class ConfiguringObjectMapperProvider implements ObjectMapperProvider {

private final ObjectMapper objectMapperTemplate;

private final ObjectMapperConfigurer objectMapperConfigurer;

public ConfiguringObjectMapperProvider(ObjectMapper objectMapperTemplate, ObjectMapperConfigurer objectMapperConfigurer) {
this.objectMapperTemplate = objectMapperTemplate == null ? new ObjectMapper() : objectMapperTemplate;
this.objectMapperConfigurer = objectMapperConfigurer == null ? new DefaultObjectMapperConfigurer() : objectMapperConfigurer;
}

public ConfiguringObjectMapperProvider(ObjectMapper objectMapperTemplate) {
this(objectMapperTemplate, null);
}

public ConfiguringObjectMapperProvider(ObjectMapperConfigurer objectMapperConfigurer) {
this.objectMapperConfigurer = objectMapperConfigurer;
this(null, objectMapperConfigurer);
}

public ConfiguringObjectMapperProvider() {
this.objectMapperConfigurer = new DefaultObjectMapperConfigurer();
this(null, null);
}

@Override
public ObjectMapper provide() {
ObjectMapper mapper = new ObjectMapper().disable(
SerializationFeature.FAIL_ON_EMPTY_BEANS).registerModule(new Jdk8Module());
objectMapperConfigurer.configure(mapper);

InjectableValues.Std injectableValues = new InjectableValues.Std();
injectableValues.addValue(ObjectMapper.class, mapper);
mapper.setInjectableValues(injectableValues);

ObjectMapper mapper = this.objectMapperTemplate.copy();
this.objectMapperConfigurer.configure(mapper);
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package graphql.servlet;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

/**
* @author Andrew Potter
*/
public class DefaultObjectMapperConfigurer implements ObjectMapperConfigurer {

@Override
public void configure(ObjectMapper mapper) {

// default configuration for GraphQL Java Servlet
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.registerModule(new Jdk8Module());
mapper.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS);
}
}
4 changes: 1 addition & 3 deletions src/main/java/graphql/servlet/GraphQLObjectMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package graphql.servlet;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MappingIterator;
Expand Down Expand Up @@ -45,8 +44,7 @@ public ObjectMapper getJacksonMapper() {
synchronized(this) {
result = mapper;
if (result == null) { // Second check (with locking)
mapper = result = objectMapperProvider.provide().copy();
mapper.setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS);
mapper = result = objectMapperProvider.provide();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package graphql.servlet.internal;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Map;
Expand All @@ -15,17 +15,17 @@
public class VariablesDeserializer extends JsonDeserializer<Map<String, Object>> {
@Override
public Map<String, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return deserializeVariablesObject(p.readValueAs(Object.class), (ObjectMapper) ctxt.findInjectableValue(ObjectMapper.class.getName(), null, null));
return deserializeVariablesObject(p.readValueAs(Object.class), p.getCodec());
}

public static Map<String, Object> deserializeVariablesObject(Object variables, ObjectMapper mapper) {
public static Map<String, Object> deserializeVariablesObject(Object variables, ObjectCodec codec) {
if (variables instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> genericVariables = (Map<String, Object>) variables;
return genericVariables;
} else if (variables instanceof String) {
try {
return mapper.readValue((String) variables, new TypeReference<Map<String, Object>>() {});
return codec.readValue(codec.getFactory().createParser((String) variables), new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down