From 3f361fc8af3ae8032e409ed436b1ed900b1bd55c Mon Sep 17 00:00:00 2001 From: Oryan M Date: Fri, 13 Aug 2021 17:03:11 -0400 Subject: [PATCH] Make compatible with other JVM Locales --- .../tools/resolver/FieldResolverScanner.kt | 10 +++++----- .../graphql/kickstart/tools/util/Utils.kt | 2 +- .../tools/FieldResolverScannerTest.kt | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolverScanner.kt b/src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolverScanner.kt index f89d4138..c9613012 100644 --- a/src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolverScanner.kt +++ b/src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolverScanner.kt @@ -86,11 +86,11 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) { return methods.find { it.name == name && verifyMethodArguments(it, argumentCount, search) } ?: methods.find { - (isBoolean(field.type) && it.name == "is${name.capitalize()}") && verifyMethodArguments(it, argumentCount, search) + (isBoolean(field.type) && it.name == "is${name.replaceFirstChar(Char::titlecase)}") && verifyMethodArguments(it, argumentCount, search) } ?: methods.find { - it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search) + it.name == "get${name.replaceFirstChar(Char::titlecase)}" && verifyMethodArguments(it, argumentCount, search) } ?: methods.find { - it.name == "getField${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search) + it.name == "getField${name.replaceFirstChar(Char::titlecase)}" && verifyMethodArguments(it, argumentCount, search) } ?: methods.find { it.name == "get${name.snakeToCamelCase()}" && verifyMethodArguments(it, argumentCount, search) } @@ -179,9 +179,9 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) { signatures.add("${baseType.name}.${field.name}($argString)") if (isBoolean) { - signatures.add("${baseType.name}.is${field.name.capitalize()}($argString)") + signatures.add("${baseType.name}.is${field.name.replaceFirstChar(Char::titlecase)}($argString)") } - signatures.add("${baseType.name}.get${field.name.capitalize()}($argString)") + signatures.add("${baseType.name}.get${field.name.replaceFirstChar(Char::titlecase)}($argString)") if (scannedProperties) { signatures.add("${baseType.name}.${field.name}") } diff --git a/src/main/kotlin/graphql/kickstart/tools/util/Utils.kt b/src/main/kotlin/graphql/kickstart/tools/util/Utils.kt index 55efc2ca..eb72f885 100644 --- a/src/main/kotlin/graphql/kickstart/tools/util/Utils.kt +++ b/src/main/kotlin/graphql/kickstart/tools/util/Utils.kt @@ -79,5 +79,5 @@ private fun isBooleanGetter(method: Method) = (method.name.startsWith("is") && (method.returnType == java.lang.Boolean::class.java) || method.returnType == Boolean::class.java) -internal fun String.snakeToCamelCase(): String = split("_").joinToString(separator = "") { it.capitalize() } +internal fun String.snakeToCamelCase(): String = split("_").joinToString(separator = "") { it.replaceFirstChar(Char::titlecase) } diff --git a/src/test/kotlin/graphql/kickstart/tools/FieldResolverScannerTest.kt b/src/test/kotlin/graphql/kickstart/tools/FieldResolverScannerTest.kt index 4646ddf4..f092b3b3 100644 --- a/src/test/kotlin/graphql/kickstart/tools/FieldResolverScannerTest.kt +++ b/src/test/kotlin/graphql/kickstart/tools/FieldResolverScannerTest.kt @@ -11,6 +11,7 @@ import graphql.relay.Connection import graphql.relay.DefaultConnection import graphql.relay.DefaultPageInfo import org.junit.Test +import java.util.* class FieldResolverScannerTest { @@ -81,6 +82,20 @@ class FieldResolverScannerTest { assertEquals((meta as MethodFieldResolver).method.returnType, HullType::class.java) } + @Test + fun `scanner finds field resolver method using capitalize field_name in different locale`() { + val default = Locale.getDefault() + Locale.setDefault(Locale.forLanguageTag("tr-TR")) + + val resolverInfo = RootResolverInfo(listOf(CapitalizeQuery()), options) + val fieldResolver = scanner.findFieldResolver(FieldDefinition("id", TypeName("HullType")), resolverInfo) + + assert(fieldResolver is MethodFieldResolver) + assertEquals((fieldResolver as MethodFieldResolver).method.returnType, HullType::class.java) + + Locale.setDefault(default) + } + class RootQuery1 : GraphQLQueryResolver { fun field1() {} } @@ -97,6 +112,10 @@ class FieldResolverScannerTest { fun getHullType(): HullType = HullType() } + class CapitalizeQuery : GraphQLQueryResolver { + fun getId(): HullType = HullType() + } + class HullType open class ParentPropertyQuery {