Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Spring Boot Object mapper #65

Closed
alexcibotari opened this issue Apr 3, 2018 · 6 comments
Closed

Spring Boot Object mapper #65

alexcibotari opened this issue Apr 3, 2018 · 6 comments

Comments

@alexcibotari
Copy link

alexcibotari commented Apr 3, 2018

Hello,

I there any possibility to share Jackson Object Mapper between Spring Boot and GraphQL?

Instead of duplicating code like this

GraphQL

@Component
public class GraphQlObjectMapperConfigurer implements ObjectMapperConfigurer {
  @Override
  public void configure(ObjectMapper mapper, ObjectMapperConfigurerContext context) {
    mapper.registerModule(new JavaTimeModule())
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  }
}

Spring Boot

@Configuration
public class JacksonConfiguration {

  @Bean
  Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
    JavaTimeModule module = new JavaTimeModule();
    return new Jackson2ObjectMapperBuilder()
        .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .serializationInclusion(JsonInclude.Include.NON_NULL)
        .findModulesViaServiceLoader(true)
        .modulesToInstall(module);
  }

}
@apottere
Copy link
Collaborator

This is tough because there are cases where you'll want your graphql object mapper to behave differently than your spring object mapper. Passing the spring object mapper to graphql-java-servlet automatically would make it impossible to differentiate.

@mxab
Copy link

mxab commented Jul 22, 2018

I think by default you would expect spring's default object mapper but I also understand that you may want to configure it differently.
Could you add some property like
...graphql.use-default-objectmapper = false and then provide a objectmapper with a certain qualifier?

@mxab
Copy link

mxab commented Aug 15, 2018

@oliemansm I have some suggestion to solve this issue.

First we would need to move the instantiations of the per field objectmapper in the to a provider/factory
graphql-java-kickstart/graphql-java-tools@master...mxab:wip/unify-objectmapper
I added also a backward compatible Provider that behaves like the original instantiations using the configurerer.

Similar but only for the one objectmapper that is constructed by the servlet
graphql-java-kickstart/graphql-java-servlet@master...mxab:wip/unify-objectmapper

Finally can provide these providers that just passthrough the available objectmapper provided by spring boot for both tools and servlet
master...mxab:wip/unify-objectmapper

@oliemansm
Copy link
Member

@mxab Nice! I've created PRs for them so we can get them merged and released.

@oliemansm
Copy link
Member

@mxab Can you give me write permissions on your branch in spring-boot? I merged in master to fix conflicts. That has to be pushed before it can be merged.

@mxab
Copy link

mxab commented Aug 25, 2018

Cool!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants