Skip to content

Commit

Permalink
Merge pull request #741 from npwork/refactor-jsontype-extension-method
Browse files Browse the repository at this point in the history
refactor: Added util function jsonTypeRef for simplification of TypeR…
  • Loading branch information
berngp committed Nov 24, 2021
2 parents 8911a18 + be546f2 commit 43fe209
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
Expand Up @@ -49,7 +49,7 @@ data class GraphQLResponse(val json: String, val headers: Map<String, List<Strin
*/

val data: Map<String, Any> = parsed.read("data") ?: emptyMap()
val errors: List<GraphQLError> = parsed.read("errors", object : TypeRef<List<GraphQLError>>() {}) ?: emptyList()
val errors: List<GraphQLError> = parsed.read("errors", jsonTypeRef<List<GraphQLError>>()) ?: emptyList()

constructor(json: String) : this(json, emptyMap())

Expand Down Expand Up @@ -136,3 +136,5 @@ data class GraphQLResponse(val json: String, val headers: Map<String, List<Strin

@JsonIgnoreProperties(ignoreUnknown = true)
data class RequestDetails(val requestId: String?, val edgarLink: String?)

inline fun <reified T> jsonTypeRef(): TypeRef<T> = object : TypeRef<T>() {}
Expand Up @@ -16,8 +16,8 @@

package com.netflix.graphql.dgs.client

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.netflix.graphql.types.subscription.*
import graphql.GraphQLException
import org.springframework.web.reactive.socket.WebSocketMessage
Expand Down Expand Up @@ -263,7 +263,7 @@ class OperationMessageWebSocketClient(

private fun decodeMessage(message: WebSocketMessage): OperationMessage {
val messageText = message.payloadAsText
val type = object : TypeReference<OperationMessage>() {}
val type = jacksonTypeRef<OperationMessage>()

return MAPPER.readValue(messageText, type)
}
Expand Down
Expand Up @@ -16,7 +16,6 @@

package com.netflix.graphql.dgs.client

import com.jayway.jsonpath.TypeRef
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.http.HttpEntity
Expand Down Expand Up @@ -184,9 +183,9 @@ class GraphQLResponseTest {
emptyMap(), requestExecutor
)

val listOfSubmittedBy: List<String> = graphQLResponse.extractValueAsObject(
val listOfSubmittedBy = graphQLResponse.extractValueAsObject(
"submitReview.edges[*].node.submittedBy",
object : TypeRef<List<String>>() {}
jsonTypeRef<List<String>>()
)
assertThat(listOfSubmittedBy).isInstanceOf(ArrayList::class.java)
assertThat(listOfSubmittedBy.size).isEqualTo(2)
Expand Down
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.netflix.graphql.types.subscription.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -76,7 +76,7 @@ class OperationMessageTest {
}

private fun deserialize(message: String) =
MAPPER.readValue(message, object : TypeReference<OperationMessage>() {})
MAPPER.readValue(message, jacksonTypeRef<OperationMessage>())

companion object {
val MAPPER = jacksonObjectMapper()
Expand Down

0 comments on commit 43fe209

Please sign in to comment.