Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @deprecated directives without a reason attribute #711

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun applyDirectivesKotlin(directives: List<Directive>, config: CodeGenConfig): M
if (argumentMap.containsKey(ParserConstants.REASON)) {
annotations.add(deprecatedAnnotation((argumentMap[ParserConstants.REASON] as StringValue).value))
} else {
throw IllegalArgumentException("Deprecated requires an argument `${ParserConstants.REASON}`")
annotations.add(deprecatedAnnotation("Deprecated in the GraphQL schema."))
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ fun applyDirectivesJava(directives: List<Directive>, config: CodeGenConfig): Pai
commentFormat = "@deprecated ${reason.substringBefore(ParserConstants.REPLACE_WITH_STR)}. Replaced by $replace"
}
} else {
throw IllegalArgumentException("Deprecated requires an argument `${ParserConstants.REASON}`")
commentFormat = "Deprecated in the GraphQL schema."
}
}
annotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4208,25 +4208,38 @@ It takes a title and such.
}

@Test
fun deprecateAnnotationWithNoMesssage() {
fun deprecateAnnotationWithNoReason() {
val schema = """
input Person @deprecated {
name: String
name: String @deprecated
}
""".trimIndent()

assertThrows<IllegalArgumentException> {
CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName,
includeImports = mapOf(Pair("validator", "com.test.validator")),
includeEnumImports = mapOf("ValidPerson" to mapOf("types" to "com.enums")),
generateCustomAnnotations = true,
addDeprecatedAnnotation = true
)
).generate()
}
val (dataTypes) = CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName,
includeImports = mapOf(Pair("validator", "com.test.validator")),
includeEnumImports = mapOf("ValidPerson" to mapOf("types" to "com.enums")),
generateCustomAnnotations = true,
addDeprecatedAnnotation = true
)

).generate()

assertThat(dataTypes.size).isEqualTo(1)
val person = dataTypes.single().typeSpec
assertThat(person.name).isEqualTo("Person")
assertThat(person.annotations).hasSize(1)
assertThat(((person.annotations[0] as AnnotationSpec).type as ClassName).simpleName()).isEqualTo("Deprecated")
assertThat(((person.annotations[0] as AnnotationSpec).type as ClassName).canonicalName()).isEqualTo("java.lang.Deprecated")
assertThat(person.javadoc.toString()).isEqualTo("Deprecated in the GraphQL schema.")
val fields = person.fieldSpecs
assertThat(fields).hasSize(1)
assertThat(fields[0].annotations).hasSize(1)
assertThat(((fields[0].annotations[0] as AnnotationSpec).type as ClassName).simpleName()).isEqualTo("Deprecated")
assertThat(((fields[0].annotations[0] as AnnotationSpec).type as ClassName).canonicalName()).isEqualTo("java.lang.Deprecated")
assertThat(fields[0].javadoc.toString()).isEqualTo("Deprecated in the GraphQL schema.")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2780,23 +2780,37 @@ class KotlinCodeGenTest {
}

@Test
fun deprecateAnnotationWithNoMesssage() {
fun deprecateAnnotationWithNoReason() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since we're changing it anyway, can we change it to be correct (there is a reason in this test case).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make it correct by removing the reason attribute so that its missing from both deprecated directives. And for consistency, I will also update the corresponding test in CodeGenTest to test for the same scenario.

val schema = """
input Person @deprecated(message: "This is going bye bye") {
input Person @deprecated {
name: String @deprecated
}
""".trimIndent()

assertThrows<IllegalArgumentException> {
CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName,
language = Language.KOTLIN,
addDeprecatedAnnotation = true
)
).generate()
}
val dataTypes = CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = basePackageName,
language = Language.KOTLIN,
addDeprecatedAnnotation = true
)
).generate().kotlinDataTypes

assertThat(dataTypes).hasSize(1)
assertThat(dataTypes[0].name).isEqualTo("Person")

val annotationSpec = (dataTypes[0].members[0] as TypeSpec).annotations[0]
assertThat((annotationSpec.typeName as ClassName).canonicalName).isEqualTo("kotlin.Deprecated")
assertThat(annotationSpec.members).hasSize(1)
assertThat(annotationSpec.members[0]).extracting("formatParts", "args").asList().contains(listOf("message = ", "%S"), listOf("Deprecated in the GraphQL schema."))

val parameterSpec = (((dataTypes[0].members)[0] as TypeSpec).primaryConstructor as FunSpec).parameters[0]
assertThat(parameterSpec.name).isEqualTo("name")
assertThat(parameterSpec.annotations).hasSize(2)
assertThat((parameterSpec.annotations[0].typeName as ClassName).canonicalName).isEqualTo("com.fasterxml.jackson.annotation.JsonProperty")
assertThat((parameterSpec.annotations[1].typeName as ClassName).canonicalName).isEqualTo("kotlin.Deprecated")
assertThat(parameterSpec.annotations[1].members).hasSize(1)
assertThat(parameterSpec.annotations[1].members[0]).extracting("formatParts", "args").asList().contains(listOf("message = ", "%S"), listOf("Deprecated in the GraphQL schema."))
}

@Test
Expand Down
Loading