Skip to content

Commit

Permalink
Make compatible with other JVM Locales
Browse files Browse the repository at this point in the history
  • Loading branch information
oryan-block committed Aug 13, 2021
1 parent 1b1803e commit ad8ef15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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}")
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/graphql/kickstart/tools/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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() {}
}
Expand All @@ -97,6 +111,10 @@ class FieldResolverScannerTest {
fun getHullType(): HullType = HullType()
}

class CapitalizeQuery : GraphQLQueryResolver {
fun getId(): HullType = HullType()
}

class HullType

open class ParentPropertyQuery {
Expand Down

0 comments on commit ad8ef15

Please sign in to comment.