diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/ClientApiGenerator.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/ClientApiGenerator.kt index b987ba6b..61595cbf 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/ClientApiGenerator.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/ClientApiGenerator.kt @@ -67,6 +67,16 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document if (it.description != null) { javaType.addJavadoc(it.description.sanitizeJavaDoc()) } + + val deprecatedClassDirective = getDeprecateDirective(it) + if (deprecatedClassDirective != null) { + javaType.addAnnotation(java.lang.Deprecated::class.java) + val deprecationReason = getDeprecatedReason(deprecatedClassDirective) + if (deprecationReason != null) { + javaType.addJavadoc("@deprecated " + deprecationReason.sanitizeJavaDoc()) + } + } + javaType.addMethod( MethodSpec.methodBuilder("getOperationName") .addModifiers(Modifier.PUBLIC) @@ -116,18 +126,8 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document it.inputValueDefinitions.forEach { inputValue -> val findReturnType = TypeUtils(getDatatypesPackageName(), config, document).findReturnType(inputValue.type) - val deprecatedDirective = if (config.addDeprecatedAnnotation) { - inputValue - .getDirectives("deprecated") - ?.firstOrNull() // Should we throw here, if there are multiple "@deprecated"? - } else { - null - } - - val deprecationReason = deprecatedDirective - ?.getArgument("reason") - ?.let { it.value as? StringValue } - ?.value + val deprecatedDirective = getDeprecateDirective(inputValue) + val deprecationReason = deprecatedDirective?.let { it1 -> getDeprecatedReason(it1) } val methodBuilder = MethodSpec.methodBuilder(ReservedKeywordSanitizer.sanitize(inputValue.name)) .addParameter(findReturnType, ReservedKeywordSanitizer.sanitize(inputValue.name)) @@ -636,6 +636,21 @@ class ClientApiGenerator(private val config: CodeGenConfig, private val document return javaType to codeGenResult.merge(concreteTypesResult).merge(unionTypesResult) } + private fun getDeprecateDirective(node: DirectivesContainer<*>): Directive? { + if (config.addDeprecatedAnnotation) { + return node + .getDirectives("deprecated") + ?.firstOrNull() // Should we throw here, if there are multiple "@deprecated"? + } + return null + } + + private fun getDeprecatedReason(directive: Directive): String? { + return directive + ?.getArgument("reason") + ?.let { it.value as? StringValue } + ?.value + } private fun truncatePrefix(prefix: String): String { return if (config.shortProjectionNames) ClassnameShortener.shorten(prefix) else prefix } diff --git a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt index ec2a5345..f2a09753 100644 --- a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt +++ b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt @@ -995,6 +995,33 @@ class CodeGenTest { assertCompilesJava(codeGenResult.javaEnumTypes) } + @Test + fun generateDataWithReservedKeywords() { + val schema = """ + type Query { + people: [Person] + } + + type Person { + package: Parcel + } + + type Parcel { + name: String + } + """.trimIndent() + + val codeGenResult = CodeGen( + CodeGenConfig( + schemas = setOf(schema), + packageName = basePackageName, + generateClientApi = true + ) + ).generate() + + assertCompilesJava(codeGenResult) + } + @Nested inner class EnumAnnotationTest { @Test diff --git a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/clientapi/ClientApiGenBuilderTest.kt b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/clientapi/ClientApiGenBuilderTest.kt index 3db70aff..2f608336 100644 --- a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/clientapi/ClientApiGenBuilderTest.kt +++ b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/clientapi/ClientApiGenBuilderTest.kt @@ -142,6 +142,36 @@ class ClientApiGenBuilderTest { @Nested inner class Deprecation { + @Test + fun `adds @Deprecated annotation on class and reason from schema directives when setting enabled`() { + val schema = """ + type Query { + filter( + nameFilter: String, + idFilter: ID + ): [String] @deprecated(reason: "DO NOT USE") + } + """.trimIndent() + + val codeGenResult = CodeGen( + CodeGenConfig( + schemas = setOf(schema), + packageName = basePackageName, + generateClientApiv2 = true, + maxProjectionDepth = 2, + addDeprecatedAnnotation = true + ) + ).generate() + + assertThat(codeGenResult.javaQueryTypes.size).isEqualTo(1) + assertThat(codeGenResult.javaQueryTypes[0].typeSpec.name).isEqualTo("FilterGraphQLQuery") + assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs).hasSize(1) + assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs[0].methodSpecs).hasSize(4) + assertThat(codeGenResult.javaQueryTypes[0].typeSpec.typeSpecs[0].methodSpecs[1].name).isEqualTo("nameFilter") + assertThat(codeGenResult.javaQueryTypes[0].typeSpec.javadoc.toString()).startsWith( + "@deprecated DO NOT USE".trimMargin() + ) + } @Test fun `adds @Deprecated annotation and reason from schema directives when setting enabled`() {