Skip to content

Commit

Permalink
Use Jackson2ObjectMapperBuilder and add jdk8 serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
kailyak committed Jul 1, 2024
1 parent afc6b12 commit 567d6cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions graphql-dgs-subscriptions-sse/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
dependencies {
implementation(project(":graphql-dgs"))
implementation(project(":graphql-dgs-subscription-types"))
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.springframework:spring-web")
implementation("org.springframework:spring-webmvc")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.netflix.graphql.dgs.subscriptions.sse

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.graphql.dgs.DgsQueryExecutor
import com.netflix.graphql.types.subscription.QueryPayload
import com.netflix.graphql.types.subscription.SSEDataPayload
Expand All @@ -32,6 +32,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.MediaType
import org.springframework.http.codec.ServerSentEvent
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand Down Expand Up @@ -139,7 +140,7 @@ open class DgsSSESubscriptionHandler(open val dgsQueryExecutor: DgsQueryExecutor
}

companion object {
private val mapper = jacksonObjectMapper()
private val mapper: ObjectMapper = Jackson2ObjectMapperBuilder.json().build()
private val logger: Logger = LoggerFactory.getLogger(DgsSSESubscriptionHandler::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.request
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import reactor.core.publisher.Flux
import java.util.Base64
import java.util.Optional

@Disabled("Avoiding stuck builds")
@WebMvcTest(DgsSSESubscriptionHandler::class, DgsSSESubscriptionHandlerTest.App::class)
Expand Down Expand Up @@ -172,4 +173,38 @@ internal class DgsSSESubscriptionHandlerTest {
assertEquals("message 1", messages[0].data)
assertEquals("message 2", messages[1].data)
}

@Test
fun `success with Optional types`() {
val query = "subscription { stocks { name, price }}"
val queryPayload = QueryPayload(operationName = "MySubscription", query = query)
val encodedQuery = Base64.getEncoder().encodeToString(mapper.writeValueAsBytes(queryPayload))

val publisher = Flux.just(
ExecutionResultImpl.newExecutionResult().data(Optional.of("optional message")).build(),
ExecutionResultImpl.newExecutionResult().data(Optional.empty<String>()).build()
)
val executionResult = ExecutionResultImpl.newExecutionResult().data(publisher).build()

`when`(dgsQueryExecutor.execute(eq(query), any())).thenReturn(executionResult)

val result = mockMvc.perform(get("/subscriptions").param("query", encodedQuery))
.andExpect(request().asyncStarted())
.andExpect(status().is2xxSuccessful)
.andReturn()

mockMvc.perform(asyncDispatch(result))
.andExpect(content().contentType(MediaType.TEXT_EVENT_STREAM))
.andReturn()

val messages = result.response.contentAsString.lineSequence()
.filter { line -> line.startsWith("data:") }
.map { line -> line.substring("data:".length) }
.map { line -> mapper.readValue<SSEDataPayload>(line) }
.toList()

assertEquals(2, messages.size)
assertEquals("optional message", messages[0].data)
assertEquals(null, messages[1].data)
}
}

0 comments on commit 567d6cf

Please sign in to comment.