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

default value for to string classes #611

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -42,21 +42,22 @@ import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible

open class InputValueSerializer(private val scalars: Map<Class<*>, Coercing<*, *>> = emptyMap()) :
open class InputValueSerializer(
private val scalars: Map<Class<*>, Coercing<*, *>> = emptyMap(),
private val toStringClasses: Set<KClass<*>> = setOf(
String::class,
LocalDateTime::class,
LocalDate::class,
LocalTime::class,
TimeZone::class,
Date::class,
OffsetDateTime::class,
Currency::class,
Instant::class,
UUID::class
)
) :
InputValueSerializerInterface {
companion object {
private val toStringClasses = setOf(
String::class,
LocalDateTime::class,
LocalDate::class,
LocalTime::class,
TimeZone::class,
Date::class,
OffsetDateTime::class,
Currency::class,
Instant::class
)
}

override fun serialize(input: Any?): String {
return AstPrinter.printAst(toValue(input))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ object DefaultTracker {
}

@QueryProjectionMarker
abstract class GraphQLProjection(defaultFields: Set<String> = setOf("__typename")) {
abstract class GraphQLProjection(
defaultFields: Set<String> = setOf("__typename"),
private val inputValueSerializer: InputValueSerializer = InputValueSerializer()
) {

companion object {

private val inputSerializer = InputValueSerializer()

@JvmStatic
protected fun <T> default0(arg: String): T? {
DefaultTracker.add(this::class.qualifiedName!!, arg)
Expand Down Expand Up @@ -105,7 +105,7 @@ abstract class GraphQLProjection(defaultFields: Set<String> = setOf("__typename"
.map { (arg, value) ->
Argument.newArgument()
.name(arg)
.value(inputSerializer.toValue(value))
.value(inputValueSerializer.toValue(value))
.build()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package com.netflix.graphql.dgs.client.codegen

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import graphql.language.StringValue
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class InputValueSerializerTest {

Expand Down Expand Up @@ -55,7 +57,8 @@ class InputValueSerializerTest {
DateRange(LocalDate.of(2020, 1, 1), LocalDate.of(2021, 1, 1))
)

val serialize = InputValueSerializer(mapOf(DateRange::class.java to DateRangeScalar())).serialize(listOf(movieInput))
val serialize =
InputValueSerializer(mapOf(DateRange::class.java to DateRangeScalar())).serialize(listOf(movieInput))
assertThat(serialize).isEqualTo("""[{movieId : 1, title : "Some movie", genre : ACTION, director : {name : "The Director"}, actor : [{name : "Actor 1", roleName : "Role 1"}, {name : "Actor 2", roleName : "Role 2"}], releaseWindow : "01/01/2020-01/01/2021"}]""")
}

Expand Down Expand Up @@ -144,6 +147,13 @@ class InputValueSerializerTest {
assertThat(serialize).isEqualTo("""{date : "2021-05-13T04:34"}""")
}

@Test
fun `UUID value`() {
val expected = UUID.randomUUID()
val actual = InputValueSerializer().serialize(expected)
assertThat(actual).isEqualTo(""""$expected"""")
}

@Test
fun `issue 337`() {
val input = EvilGenre.ACTION
Expand All @@ -157,7 +167,9 @@ class InputValueSerializerTest {
val baseField: Boolean = true
open val field: String = "default"
}

class QueryInput(override val field: String) : Base()

val serialized = InputValueSerializer().serialize(QueryInput("hello"))
assertThat(serialized).isEqualTo("""{field : "hello", baseField : true}""")
}
Expand All @@ -168,16 +180,19 @@ class InputValueSerializerTest {
@Transient
val notVisible = "do not serialize"
}

val serialized = InputValueSerializer().serialize(QueryInput("serialize me"))
assertThat(serialized).isEqualTo("""{visible : "serialize me"}""")
}

enum class EvilGenre {
ACTION;

override fun toString(): String {
return "Genre[$name]"
}
}

data class MyDataWithCompanion(val title: String) {
companion object
}
Expand Down