Skip to content

Commit

Permalink
Fix breakage when extracting "data" as the path in GraphQLResponse
Browse files Browse the repository at this point in the history
Recently a change was made in b386856 that broke support for extracting
the value from the response using the path "data". While users could simply
use the "dataAsObject" method, there are many instances where users call
extractValue with "data" as the path. Restore support for this, and add
unit tests.

(cherry picked from commit 163d1bc)
  • Loading branch information
kilink authored and congotej committed Aug 28, 2023
1 parent fd1005a commit 01b5cb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ data class GraphQLResponse(val json: String, val headers: Map<String, List<Strin
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)

fun getDataPath(path: String): String {
return if (!path.startsWith("data.")) "data.$path" else path
return if (path == "data" || path.startsWith("data.")) {
path
} else {
"data.$path"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.netflix.graphql.dgs.client

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
Expand Down Expand Up @@ -53,6 +54,7 @@ class GraphQLResponseTest {
@ParameterizedTest
@CsvSource(
value = [
"data, data",
"foo, data.foo",
"data.foo, data.foo",
"datafoo, data.datafoo"
Expand Down Expand Up @@ -238,4 +240,21 @@ class GraphQLResponseTest {

server.verify()
}

@Test
fun testExtractValue() {
val response = GraphQLResponse("""{"data": {"submitReview": {"submittedBy": "[email protected]"}}}""")
val result = response.extractValue<Map<String, Any?>>("data")
assertEquals(mapOf("submitReview" to mapOf("submittedBy" to "[email protected]")), result)
assertEquals("[email protected]", response.extractValue("data.submitReview.submittedBy"))
assertEquals("[email protected]", response.extractValue("submitReview.submittedBy"))
}

@Test
fun testDataAsObject() {
data class Response(val submitReview: Map<String, String>)
val response = GraphQLResponse("""{"data": {"submitReview": {"submittedBy": "[email protected]"}}}""")
val result = response.dataAsObject(Response::class.java)
assertEquals(Response(submitReview = mapOf("submittedBy" to "[email protected]")), result)
}
}

0 comments on commit 01b5cb2

Please sign in to comment.