From 5aa60004a823587bce37b4bce480f376017bdbff Mon Sep 17 00:00:00 2001 From: pbakker Date: Tue, 11 Jun 2024 17:52:20 -0700 Subject: [PATCH 1/2] Use jakarta package for @Generated annotation --- graphql-dgs-codegen-core/build.gradle | 2 ++ .../dgs/codegen/generators/shared/SharedTypeUtils.kt | 9 +++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/graphql-dgs-codegen-core/build.gradle b/graphql-dgs-codegen-core/build.gradle index 45fdcb53d..4933f6390 100644 --- a/graphql-dgs-codegen-core/build.gradle +++ b/graphql-dgs-codegen-core/build.gradle @@ -39,6 +39,8 @@ dependencies { implementation 'com.github.ajalt.clikt:clikt:4.4.+' implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.+' + implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1' + testImplementation 'com.google.testing.compile:compile-testing:0.+' testImplementation "org.jetbrains.kotlin:kotlin-compiler" diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/SharedTypeUtils.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/SharedTypeUtils.kt index ebe76c886..6fa29968b 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/SharedTypeUtils.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/SharedTypeUtils.kt @@ -158,10 +158,7 @@ internal fun findSchemaTypeMapping(document: Document, typeName: String): String } internal val generatedAnnotationClassName: String? = runCatching { - Class.forName("javax.annotation.processing.Generated").canonicalName -}.getOrElse { - runCatching { - Class.forName("javax.annotation.Generated").canonicalName - }.getOrNull() -} + Class.forName("jakarta.annotation.Generated").canonicalName +}.getOrNull() + internal val generatedDate: String = Instant.now().toString() From adb0c933a540d6a480c762012ddea2c193b7c121 Mon Sep 17 00:00:00 2001 From: pbakker Date: Fri, 19 Jul 2024 16:06:42 -0700 Subject: [PATCH 2/2] Dates in @Generated are now optional --- graphql-dgs-codegen-core/dependencies.lock | 54 +++++++++++++++++++ .../netflix/graphql/dgs/codegen/CodeGen.kt | 1 + .../codegen/generators/java/JavaPoetUtils.kt | 14 ++--- .../graphql/dgs/codegen/CodeGenTest.kt | 50 ++++++++++++++++- .../graphql/dgs/codegen/KotlinCodeGenTest.kt | 2 +- .../netflix/graphql/dgs/codegen/TestUtils.kt | 18 ++++--- graphql-dgs-codegen-gradle/dependencies.lock | 14 +++++ .../dgs/codegen/gradle/GenerateJavaTask.kt | 4 ++ 8 files changed, 143 insertions(+), 14 deletions(-) diff --git a/graphql-dgs-codegen-core/dependencies.lock b/graphql-dgs-codegen-core/dependencies.lock index f3d66c235..a3593c820 100644 --- a/graphql-dgs-codegen-core/dependencies.lock +++ b/graphql-dgs-codegen-core/dependencies.lock @@ -116,6 +116,12 @@ "com.squareup:kotlinpoet" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "org.jetbrains.kotlin:kotlin-bom": { "locked": "1.9.25" }, @@ -287,6 +293,12 @@ "com.squareup:kotlinpoet": { "locked": "1.17.0" }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "net.java.dev.jna:jna": { "locked": "5.14.0", "transitive": [ @@ -530,6 +542,12 @@ "com.squareup:kotlinpoet" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ @@ -929,6 +947,12 @@ "com.squareup:kotlinpoet": { "locked": "1.17.0" }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ @@ -1337,6 +1361,12 @@ "org.jetbrains:markdown-jvm" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ @@ -2042,6 +2072,12 @@ "org.jetbrains:markdown-jvm" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "net.java.dev.jna:jna": { "locked": "5.14.0", "transitive": [ @@ -2336,6 +2372,12 @@ "com.squareup:kotlinpoet" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ @@ -2735,6 +2777,12 @@ "com.squareup:kotlinpoet": { "locked": "1.17.0" }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ @@ -3143,6 +3191,12 @@ "org.jetbrains:markdown-jvm" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "junit:junit": { "locked": "4.13.2", "transitive": [ diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/CodeGen.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/CodeGen.kt index b0404855d..d4f5d9202 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/CodeGen.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/CodeGen.kt @@ -507,6 +507,7 @@ class CodeGenConfig( var javaGenerateAllConstructor: Boolean = true, var implementSerializable: Boolean = false, var addGeneratedAnnotation: Boolean = false, + var disableDatesInGeneratedAnnotation: Boolean = false, var addDeprecatedAnnotation: Boolean = false ) { val packageNameClient: String = "$packageName.$subPackageNameClient" diff --git a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/JavaPoetUtils.kt b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/JavaPoetUtils.kt index 1489af08a..d448903eb 100644 --- a/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/JavaPoetUtils.kt +++ b/graphql-dgs-codegen-core/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/java/JavaPoetUtils.kt @@ -156,7 +156,7 @@ fun String.toTypeName(isGenericParam: Boolean = false): TypeName { } } -private fun generatedAnnotation(packageName: String): List { +private fun generatedAnnotation(packageName: String, generateDate: Boolean): List { val graphqlGenerated = AnnotationSpec .builder(ClassName.get(packageName, "Generated")) .build() @@ -166,19 +166,21 @@ private fun generatedAnnotation(packageName: String): List { } else { val generatedAnnotation = ClassName.bestGuess(generatedAnnotationClassName) - val javaxGenerated = AnnotationSpec.builder(generatedAnnotation) + var jakartaGeneratedBuilder = AnnotationSpec.builder(generatedAnnotation) .addMember("value", "${'$'}S", CodeGen::class.qualifiedName!!) - .addMember("date", "${'$'}S", generatedDate) - .build() - listOf(javaxGenerated, graphqlGenerated) + if (generateDate) { + jakartaGeneratedBuilder = jakartaGeneratedBuilder.addMember("date", "${'$'}S", generatedDate) + } + + listOf(jakartaGeneratedBuilder.build(), graphqlGenerated) } } fun TypeSpec.Builder.addOptionalGeneratedAnnotation(config: CodeGenConfig): TypeSpec.Builder = apply { if (config.addGeneratedAnnotation) { - generatedAnnotation(config.packageName).forEach { addAnnotation(it) } + generatedAnnotation(config.packageName, !config.disableDatesInGeneratedAnnotation).forEach { addAnnotation(it) } } } 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 572afc6e8..cf561edd3 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 @@ -3651,7 +3651,55 @@ It takes a title and such. val (generatedAnnotationFile, allSources) = codeGenResult.javaSources() .partition { it.typeSpec.name == "Generated" && it.typeSpec.kind == TypeSpec.Kind.ANNOTATION } - allSources.assertJavaGeneratedAnnotation() + allSources.assertJavaGeneratedAnnotation(true) + assertThat(generatedAnnotationFile.single().toString()) + .contains("java.lang.annotation.Retention", "RetentionPolicy.CLASS") + assertCompilesJava(codeGenResult) + } + + @Test + fun generateSourceWithGeneratedAnnotationWithoutDate() { + val schema = """ + type Query { + employees(filter:EmployeeFilterInput) : [Person] + } + + interface Person { + firstname: String + lastname: String + } + + type Employee implements Person { + firstname: String + lastname: String + company: String + } + enum EmployeeTypes { + ENGINEER + MANAGER + DIRECTOR + } + + input EmployeeFilterInput { + rank: String + } + """.trimIndent() + + val codeGenResult = CodeGen( + CodeGenConfig( + schemas = setOf(schema), + packageName = basePackageName, + language = Language.JAVA, + addGeneratedAnnotation = true, + disableDatesInGeneratedAnnotation = true, + generateClientApi = true + ) + ).generate() + + val (generatedAnnotationFile, allSources) = codeGenResult.javaSources() + .partition { it.typeSpec.name == "Generated" && it.typeSpec.kind == TypeSpec.Kind.ANNOTATION } + + allSources.assertJavaGeneratedAnnotation(false) assertThat(generatedAnnotationFile.single().toString()) .contains("java.lang.annotation.Retention", "RetentionPolicy.CLASS") assertCompilesJava(codeGenResult) diff --git a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/KotlinCodeGenTest.kt b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/KotlinCodeGenTest.kt index 4edbd70e9..3497fa8c4 100644 --- a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/KotlinCodeGenTest.kt +++ b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/KotlinCodeGenTest.kt @@ -3671,7 +3671,7 @@ It takes a title and such. .partition { it.name == "Generated" } allKotlinSources.assertKotlinGeneratedAnnotation() - codeGenResult.javaSources().assertJavaGeneratedAnnotation() + codeGenResult.javaSources().assertJavaGeneratedAnnotation(true) assertThat(generatedAnnotationFile.single().toString()) .contains("@Retention(value = AnnotationRetention.BINARY)") diff --git a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/TestUtils.kt b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/TestUtils.kt index 85e78f93d..578dd6e48 100644 --- a/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/TestUtils.kt +++ b/graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/TestUtils.kt @@ -126,8 +126,8 @@ fun List.assertKotlinGeneratedAnnotation() = onEach { .forEach { typeSpec -> typeSpec.assertKotlinGeneratedAnnotation(it) } } -fun List.assertJavaGeneratedAnnotation() = onEach { - it.typeSpec.assertJavaGeneratedAnnotation() +fun List.assertJavaGeneratedAnnotation(shouldHaveDate: Boolean) = onEach { + it.typeSpec.assertJavaGeneratedAnnotation(shouldHaveDate) } fun KTypeSpec.assertKotlinGeneratedAnnotation(fileSpec: FileSpec) { @@ -146,20 +146,26 @@ fun KTypeSpec.assertKotlinGeneratedAnnotation(fileSpec: FileSpec) { typeSpecs.forEach { it.assertKotlinGeneratedAnnotation(fileSpec) } } -fun TypeSpec.assertJavaGeneratedAnnotation() { +fun TypeSpec.assertJavaGeneratedAnnotation(shouldHaveDate: Boolean) { val generatedSpec = annotations .firstOrNull { it.canonicalName() == "$basePackageName.Generated" } assertThat(generatedSpec) .`as`("@Generated annotation exists in %s", this) .isNotNull - val javaxGeneratedSpec = + val jakartaGeneratedSpec = annotations.firstOrNull { it.canonicalName() == generatedAnnotationClassName } - assertThat(javaxGeneratedSpec) + assertThat(jakartaGeneratedSpec) .`as`("$generatedAnnotationClassName annotation exists in %s", this) .isNotNull - this.typeSpecs.forEach { it.assertJavaGeneratedAnnotation() } + if (shouldHaveDate) { + assertThat(jakartaGeneratedSpec!!.members.keys).contains("date") + } else { + assertThat(jakartaGeneratedSpec!!.members.keys).doesNotContain("date") + } + + this.typeSpecs.forEach { it.assertJavaGeneratedAnnotation(shouldHaveDate) } } fun AnnotationSpec.canonicalName(): String = (type as ClassName).canonicalName() diff --git a/graphql-dgs-codegen-gradle/dependencies.lock b/graphql-dgs-codegen-gradle/dependencies.lock index 60c5483b8..4695e91a8 100644 --- a/graphql-dgs-codegen-gradle/dependencies.lock +++ b/graphql-dgs-codegen-gradle/dependencies.lock @@ -637,6 +637,13 @@ "org.jetbrains:markdown-jvm" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-core", + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "net.java.dev.jna:jna": { "locked": "5.14.0", "transitive": [ @@ -1286,6 +1293,13 @@ "org.jetbrains:markdown-jvm" ] }, + "jakarta.annotation:jakarta.annotation-api": { + "locked": "2.1.1", + "transitive": [ + "com.netflix.graphql.dgs.codegen:graphql-dgs-codegen-core", + "com.netflix.graphql.dgs:graphql-dgs-platform-dependencies" + ] + }, "net.bytebuddy:byte-buddy": { "locked": "1.14.18", "transitive": [ diff --git a/graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt b/graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt index 4b642af30..8ec1162b8 100644 --- a/graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt +++ b/graphql-dgs-codegen-gradle/src/main/kotlin/com/netflix/graphql/dgs/codegen/gradle/GenerateJavaTask.kt @@ -141,6 +141,9 @@ open class GenerateJavaTask @Inject constructor( @Input var addGeneratedAnnotation = false + @Input + var disableDatesInGeneratedAnnotation = false + @Input var addDeprecatedAnnotation = false @@ -208,6 +211,7 @@ open class GenerateJavaTask @Inject constructor( snakeCaseConstantNames = snakeCaseConstantNames, implementSerializable = implementSerializable, addGeneratedAnnotation = addGeneratedAnnotation, + disableDatesInGeneratedAnnotation = disableDatesInGeneratedAnnotation, addDeprecatedAnnotation = addDeprecatedAnnotation, includeImports = includeImports, includeEnumImports = includeEnumImports,