-
Notifications
You must be signed in to change notification settings - Fork 301
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
Changes from 1 commit
b1139f6
540cf43
e9e2be9
d8a3d07
b37dd71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
) { | ||
|
||
/** | ||
* 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 | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a new mapper every time a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.")) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
} |
There was a problem hiding this comment.
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?