Skip to content

Commit

Permalink
Add deprecated annotation to generation query classes. (Netflix#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasankavitha authored Apr 15, 2024
1 parent d33cbe2 commit 95f27dc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down

0 comments on commit 95f27dc

Please sign in to comment.