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
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ 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
) {

/**
* 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 +62,12 @@ 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,
headers,
// default object mapper instead no instance is passed in the constructor
DEFAULT_MAPPER
)

/**
* Deserialize data into the given class.
Expand Down Expand Up @@ -118,19 +133,14 @@ 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)
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?


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)

private val jsonPathConfig: Configuration = Configuration.builder()
.jsonProvider(JacksonJsonProvider(mapper))
.mappingProvider(JacksonMappingProvider(mapper)).build()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)

fun getDataPath(path: String): String {
return if (path == "data" || path.startsWith("data.")) {
path
Expand Down