dgs-framework icon indicating copy to clipboard operation
dgs-framework copied to clipboard

bug: [DgsQueryExecutor] HttpHeaders are not being processed

Open MessiasLima opened this issue 3 years ago • 0 comments
trafficstars

Expected behavior

When I am writing tests using DgsQueryExecutor, and passing a http header. That header should be processed by Spring boot and be available on the data fetcher if necessary.

Actual behavior

The headers are ignored and the requested header is null on test cases.

Steps to reproduce

  1. Retrieve some header on fetcher class:
@DgsComponent
class MyDataFetcher {
    @DgsQuery
    fun getSomeDataUsingHeader(
        @RequestHeader(HttpHeaders.AUTHORIZATION) authorization: String,
    ): ChatAuthorizationResponse {
        // do some stuff using the authorization token
    }
}

  1. Pass the header on a test case
@SpringBootTest(
    classes = [
        DgsAutoConfiguration::class,
        DgsExtendedScalarsAutoConfiguration::class,
        UUIDScalar::class,
        MyDataFetcher::class,
    ]
)
internal class MyDataFetcher {
    @Autowired
    lateinit var dgsQueryExecutor: DgsQueryExecutor

    @Test
    fun `chat query successfully`() {
        val gqlRequest = // request creation 

        val headers = HttpHeaders().apply {
            setBearerAuth("abc123")
        }

        val response = dgsQueryExecutor.executeAndExtractJsonPath<ChatAuthorizationResponse>(
            gqlRequest.serialize(),
            "data",
            headers,
        )

        // Assertions
    }
}
  1. You will get this error when running the test
java.lang.NullPointerException: Parameter specified as non-null is null: method package.ommited.MyDataFetcher.getSomeDataUsingHeader, parameter authorization
	at package.ommited.MyDataFetcher.getSomeDataUsingHeader(MyDataFetcher.kt) ~[main/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282) ~[spring-core-5.3.23.jar:5.3.23]
	at com.netflix.graphql.dgs.internal.DataFetcherInvoker.get(DataFetcherInvoker.kt:68) ~[graphql-dgs-5.2.4.jar:5.2.4]
	at graphql.schema.DataFetcherFactories.lambda$wrapDataFetcher$2(DataFetcherFactories.java:37) ~[graphql-java-19.2.jar:na]
	at graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:282) ~[graphql-java-19.2.jar:na]

MessiasLima avatar Oct 12 '22 11:10 MessiasLima