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 support for custom object mapper in GraphQLResponse #1737

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ import org.slf4j.LoggerFactory
* Representation of a GraphQL response, which may contain GraphQL errors.
* This class gives convenient JSON parsing methods to get data out of the response.
*/
data class GraphQLResponse(@Language("json") val json: String, val headers: Map<String, List<String>>) {
data class GraphQLResponse(
@Language("json") val json: String,
val headers: Map<String, List<String>>,
val mapper: ObjectMapper
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should make the property private?

) {

/**
* A JsonPath DocumentContext. Typically, only used internally.
*/
val parsed: DocumentContext = JsonPath.using(jsonPathConfig).parse(json)
val parsed: DocumentContext = JsonPath.using(Configuration.builder()
.jsonProvider(JacksonJsonProvider(mapper))
.mappingProvider(JacksonMappingProvider(mapper)).build()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)).parse(json)

/**
* Map representation of data
Expand All @@ -53,6 +60,14 @@ data class GraphQLResponse(@Language("json") val json: String, val headers: Map<
val errors: List<GraphQLError> = parsed.read("errors", jsonTypeRef<List<GraphQLError>>()) ?: emptyList()

constructor(@Language("json") json: String) : this(json, emptyMap())
constructor(@Language("json") json: String, headers: Map<String, List<String>>) : this(json,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a new mapper every time a GraphQLResponse is instantiated. I think we should leave the default mapper construction in the companion object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. +1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we leave the default mapper in the companion object, how can we achieve the dependency injection? I'm not aware of the kotlin semantics, but I suspect that this companion object is some sort of a static instance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suggesting leaving the default ObjectMapper in the companion object so that it's only instantiated once, but can be used as an argument to the constructor. e.g.:

@@ -38,7 +38,9 @@ import org.slf4j.LoggerFactory
  * Representation of a GraphQL response, which may contain GraphQL errors.
  * This class gives convenient JSON parsing methods to get data out of the response.
  */
-data class GraphQLResponse(@Language("json") val json: String, val headers: Map<String, List<String>>) {
+data class GraphQLResponse(@Language("json") val json: String,
+                           val headers: Map<String, List<String>>,
+                           private val mapper: ObjectMapper = DEFAULT_MAPPER) {
 
     /**
      * A JsonPath DocumentContext. Typically, only used internally.
@@ -120,17 +122,12 @@ data class GraphQLResponse(@Language("json") val json: String, val headers: Map<
     companion object {
         private val logger: Logger = LoggerFactory.getLogger(GraphQLResponse::class.java)
 
-        private val mapper: ObjectMapper = jacksonObjectMapper()
+        private val DEFAULT_MAPPER: ObjectMapper = jacksonObjectMapper()
             .registerModule(JavaTimeModule())
             .registerModule(ParameterNamesModule())
             .registerModule(Jdk8Module())
             .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely looks better!

headers,
// default object mapper instead no instance is passed in the constructor
jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(ParameterNamesModule())
.registerModule(Jdk8Module())
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE))

/**
* Deserialize data into the given class.
Expand Down Expand Up @@ -118,18 +133,7 @@ data class GraphQLResponse(@Language("json") val json: String, val headers: Map<
fun hasErrors(): Boolean = errors.isNotEmpty()

companion object {
private val logger: Logger = LoggerFactory.getLogger(GraphQLResponse::class.java)

private val mapper: ObjectMapper = jacksonObjectMapper()
.registerModule(JavaTimeModule())
.registerModule(ParameterNamesModule())
.registerModule(Jdk8Module())
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)

private val jsonPathConfig: Configuration = Configuration.builder()
.jsonProvider(JacksonJsonProvider(mapper))
.mappingProvider(JacksonMappingProvider(mapper)).build()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)
val logger: Logger = LoggerFactory.getLogger(GraphQLResponse::class.java)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change? Seems unrelated?


fun getDataPath(path: String): String {
return if (path == "data" || path.startsWith("data.")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package com.netflix.graphql.dgs.client

import com.fasterxml.jackson.databind.ObjectMapper
import graphql.relay.DefaultPageInfo
import graphql.relay.PageInfo
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -257,4 +260,15 @@ class GraphQLResponseTest {
val result = response.dataAsObject(Response::class.java)
assertEquals(Response(submitReview = mapOf("submittedBy" to "[email protected]")), result)
}

@Test
fun injectObjectMapper() {
val customObjectMapper = ObjectMapper()
val graphQLResponse =
GraphQLResponse(
"""{"data": {"submitReview": {"submittedBy": "[email protected]"}}}""",
emptyMap(),
customObjectMapper)
assertEquals(customObjectMapper, graphQLResponse.mapper)
}
}