From b19e74921194d8eba330ab7c3b72707553721fca Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Sat, 7 May 2022 14:31:15 +0200 Subject: [PATCH 1/4] Do not use 'header' as a value nama as it breaks the Kotlin compiler --- .../com/apollographql/apollo3/compiler/ReservedKeywords.kt | 3 ++- tests/enums/src/main/graphql/operation.graphql | 1 + tests/enums/src/main/graphql/schema.graphqls | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt index fd82cceb017..67af2b41575 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt @@ -13,7 +13,8 @@ private val JAVA_RESERVED_WORDS = arrayOf( // Reference: // https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position -private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal") +// header is added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 +private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal", "header") fun String.escapeJavaReservedWord() = if (this in JAVA_RESERVED_WORDS) "${this}_" else this diff --git a/tests/enums/src/main/graphql/operation.graphql b/tests/enums/src/main/graphql/operation.graphql index 76a000d3113..3b759fef23f 100644 --- a/tests/enums/src/main/graphql/operation.graphql +++ b/tests/enums/src/main/graphql/operation.graphql @@ -1,4 +1,5 @@ query GetEnums { direction gravity + foo } diff --git a/tests/enums/src/main/graphql/schema.graphqls b/tests/enums/src/main/graphql/schema.graphqls index fc0d1a38fd5..b805ba9eb76 100644 --- a/tests/enums/src/main/graphql/schema.graphqls +++ b/tests/enums/src/main/graphql/schema.graphqls @@ -1,6 +1,7 @@ type Query { direction: Direction gravity: Gravity + foo: Foo } enum Direction { @@ -29,3 +30,9 @@ enum Gravity { name, ordinal, } + +# See https://youtrack.jetbrains.com/issue/KT-52315 +enum Foo { + header, + footer, +} From 57f33210df20c1d5481b1fc9b3ce3dab477bdc0b Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Sat, 7 May 2022 14:37:34 +0200 Subject: [PATCH 2/4] also add impl --- .../com/apollographql/apollo3/compiler/ReservedKeywords.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt index 67af2b41575..efce07c77b6 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt @@ -13,8 +13,8 @@ private val JAVA_RESERVED_WORDS = arrayOf( // Reference: // https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position -// header is added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 -private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal", "header") +// "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 +private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal", "header", "impl") fun String.escapeJavaReservedWord() = if (this in JAVA_RESERVED_WORDS) "${this}_" else this From 67b372ca2a9ac9dcbeb11155c9a3c447ddf846cb Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Mon, 9 May 2022 11:49:29 +0200 Subject: [PATCH 3/4] better fix --- .../apollo3/compiler/ReservedKeywords.kt | 16 +++++++++------- tests/enums/src/test/kotlin/test/EnumsTest.kt | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt index efce07c77b6..62c3d4a4f23 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt @@ -10,15 +10,17 @@ private val JAVA_RESERVED_WORDS = arrayOf( "transient", "try", "true", "void", "volatile", "while" ) - -// Reference: -// https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position -// "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 -private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal", "header", "impl") - fun String.escapeJavaReservedWord() = if (this in JAVA_RESERVED_WORDS) "${this}_" else this // Does nothing. KotlinPoet will add the backticks fun String.escapeKotlinReservedWord() = this -fun String.escapeKotlinReservedEnumValueNames() = if (this in KOTLIN_RESERVED_ENUM_VALUE_NAMES) "${this}_" else this +fun String.escapeKotlinReservedEnumValueNames() : String { + return when { + // https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position + this in arrayOf("name", "ordinal") -> "${this}_" + // "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 + this in arrayOf("header", "impl") -> "`${this}`" + else -> this + } +} diff --git a/tests/enums/src/test/kotlin/test/EnumsTest.kt b/tests/enums/src/test/kotlin/test/EnumsTest.kt index 631492a6c8d..9ebbb4b5c9a 100644 --- a/tests/enums/src/test/kotlin/test/EnumsTest.kt +++ b/tests/enums/src/test/kotlin/test/EnumsTest.kt @@ -1,6 +1,7 @@ package test import enums.type.Direction +import enums.type.Foo import enums.type.Gravity import org.junit.Test import kotlin.test.assertEquals @@ -24,6 +25,11 @@ class EnumsTest { assertEquals(Gravity.ordinal, Gravity.safeValueOf("ordinal")) } + @Test + fun headerAndImpl() { + assertEquals(Foo.header.rawValue, "header") + } + @Test fun sealedClassesKnownValues() { // Order is important From a6ce5898614c22941c40c70420aca7606308047c Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Mon, 9 May 2022 19:32:14 +0200 Subject: [PATCH 4/4] be robust against schemas defining both name and name_ (and possibly others) Thanks @ephemient! --- .../com/apollographql/apollo3/compiler/ReservedKeywords.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt index 62c3d4a4f23..10fc0d3ba7a 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ReservedKeywords.kt @@ -18,7 +18,7 @@ fun String.escapeKotlinReservedWord() = this fun String.escapeKotlinReservedEnumValueNames() : String { return when { // https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position - this in arrayOf("name", "ordinal") -> "${this}_" + "(?:name|ordinal)_*".toRegex().matches(this) -> "${this}_" // "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315 this in arrayOf("header", "impl") -> "`${this}`" else -> this