Skip to content

Commit

Permalink
Dates in @generated are now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbakker committed Jul 19, 2024
1 parent 5aa6000 commit adb0c93
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 14 deletions.
54 changes: 54 additions & 0 deletions graphql-dgs-codegen-core/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fun String.toTypeName(isGenericParam: Boolean = false): TypeName {
}
}

private fun generatedAnnotation(packageName: String): List<AnnotationSpec> {
private fun generatedAnnotation(packageName: String, generateDate: Boolean): List<AnnotationSpec> {
val graphqlGenerated = AnnotationSpec
.builder(ClassName.get(packageName, "Generated"))
.build()
Expand All @@ -166,19 +166,21 @@ private fun generatedAnnotation(packageName: String): List<AnnotationSpec> {
} 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) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ fun List<FileSpec>.assertKotlinGeneratedAnnotation() = onEach {
.forEach { typeSpec -> typeSpec.assertKotlinGeneratedAnnotation(it) }
}

fun List<JavaFile>.assertJavaGeneratedAnnotation() = onEach {
it.typeSpec.assertJavaGeneratedAnnotation()
fun List<JavaFile>.assertJavaGeneratedAnnotation(shouldHaveDate: Boolean) = onEach {
it.typeSpec.assertJavaGeneratedAnnotation(shouldHaveDate)
}

fun KTypeSpec.assertKotlinGeneratedAnnotation(fileSpec: FileSpec) {
Expand All @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions graphql-dgs-codegen-gradle/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ open class GenerateJavaTask @Inject constructor(
@Input
var addGeneratedAnnotation = false

@Input
var disableDatesInGeneratedAnnotation = false

@Input
var addDeprecatedAnnotation = false

Expand Down Expand Up @@ -208,6 +211,7 @@ open class GenerateJavaTask @Inject constructor(
snakeCaseConstantNames = snakeCaseConstantNames,
implementSerializable = implementSerializable,
addGeneratedAnnotation = addGeneratedAnnotation,
disableDatesInGeneratedAnnotation = disableDatesInGeneratedAnnotation,
addDeprecatedAnnotation = addDeprecatedAnnotation,
includeImports = includeImports,
includeEnumImports = includeEnumImports,
Expand Down

0 comments on commit adb0c93

Please sign in to comment.