Skip to content

Commit 6b33b1d

Browse files
committed
Fix serialization of default values for input object enums fix #276
1 parent cf387b0 commit 6b33b1d

File tree

3 files changed

+142
-72
lines changed

3 files changed

+142
-72
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParser.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import graphql.schema.idl.DirectiveBehavior
4646
import graphql.schema.idl.RuntimeWiring
4747
import graphql.schema.idl.ScalarInfo
4848
import graphql.schema.idl.SchemaGeneratorHelper
49+
import org.slf4j.LoggerFactory
4950
import java.util.*
5051
import kotlin.reflect.KClass
5152

@@ -57,6 +58,7 @@ import kotlin.reflect.KClass
5758
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, private val options: SchemaParserOptions, private val runtimeWiring: RuntimeWiring) {
5859

5960
companion object {
61+
val log = LoggerFactory.getLogger(SchemaClassScanner::class.java)!!
6062
const val DEFAULT_DEPRECATION_MESSAGE = "No longer supported"
6163

6264
@JvmStatic
@@ -201,7 +203,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
201203
.name(inputDefinition.name)
202204
.definition(inputDefinition)
203205
.description(if (inputDefinition.description != null) inputDefinition.description.content else getDocumentation(inputDefinition))
204-
.defaultValue(inputDefinition.defaultValue)
206+
.defaultValue(buildDefaultValue(inputDefinition.defaultValue))
205207
.type(determineInputType(inputDefinition.type))
206208
.withDirectives(*buildDirectives(inputDefinition.directives, setOf(), Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
207209
builder.field(fieldBuilder.build())
@@ -226,6 +228,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, privat
226228
val enumName = enumDefinition.name
227229
val enumValue = type.unwrap().enumConstants.find { (it as Enum<*>).name == enumName }
228230
?: throw SchemaError("Expected value for name '$enumName' in enum '${type.unwrap().simpleName}' but found none!")
231+
229232
val enumValueDirectives = buildDirectives(enumDefinition.directives, setOf(), Introspection.DirectiveLocation.ENUM_VALUE)
230233
getDeprecated(enumDefinition.directives).let {
231234
val enumValueDefinition = GraphQLEnumValueDefinition.newEnumValueDefinition()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.coxautodev.graphql.tools
2+
3+
import graphql.GraphQL
4+
import graphql.execution.AsyncExecutionStrategy
5+
import graphql.schema.GraphQLSchema
6+
import spock.lang.Specification
7+
8+
class EnumDefaultValueSpec extends Specification {
9+
10+
def "enumvalue is not passed down to graphql-java"() {
11+
when:
12+
GraphQLSchema schema = SchemaParser.newParser().schemaString('''\
13+
type Query {
14+
test(input: MySortSpecifier): SortBy
15+
}
16+
input MySortSpecifier {
17+
sortBy: SortBy = createdOn
18+
value: Int = 10
19+
}
20+
enum SortBy {
21+
createdOn
22+
updatedOn
23+
}
24+
''').resolvers(new GraphQLQueryResolver() {
25+
26+
SortBy test(MySortSpecifier input) {
27+
return input.sortBy
28+
}
29+
30+
})
31+
.build()
32+
.makeExecutableSchema()
33+
GraphQL gql = GraphQL.newGraphQL(schema)
34+
.queryExecutionStrategy(new AsyncExecutionStrategy())
35+
.build()
36+
def data = Utils.assertNoGraphQlErrors(gql, [input: [value: 1]]) {
37+
'''
38+
query test($input: MySortSpecifier) {
39+
test(input: $input)
40+
}
41+
'''
42+
}
43+
44+
then:
45+
noExceptionThrown()
46+
data.test == 'createdOn'
47+
}
48+
49+
static class MySortSpecifier {
50+
SortBy sortBy
51+
int value
52+
}
53+
54+
enum SortBy {
55+
createdOn,
56+
updatedOn
57+
}
58+
59+
}

0 commit comments

Comments
 (0)