Skip to content

Commit

Permalink
do not warn in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Nov 17, 2023
1 parent 15407de commit 1154110
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.apollographql.apollo3.compiler.applyIf
import com.apollographql.apollo3.compiler.codegen.Identifier
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.ir.IrModel
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
Expand Down Expand Up @@ -128,15 +128,17 @@ internal class ImplementationAdapterBuilder(
.addParameter(
ParameterSpec.builder(Identifier.adapterContext, KotlinSymbols.CompositeAdapterContext)
.applyIf(addTypenameArgument) {
addAnnotation(AnnotationSpec.builder(KotlinSymbols.Suppress).addMember("%S", "UNUSED_PARAMETER").build())
addSuppressions(unusedParameter = true)
}
.build()
)
.addCode(writeToResponseCodeBlock(model, context))
.apply {
if (model.properties.any { it.info.deprecationReason != null }) {
addAnnotation(suppressDeprecationAnnotationSpec)
}
addSuppressions(
deprecation = model.properties.any { it.info.deprecationReason != null },
optInUsage = model.properties.any { it.info.optInFeature != null }
)

if (!addTypenameArgument) {
addModifiers(KModifier.OVERRIDE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import com.apollographql.apollo3.compiler.codegen.Identifier.writer
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.NamedType
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.requiresOptInAnnotation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.ir.isOptional
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
Expand All @@ -33,9 +33,9 @@ internal fun List<NamedType>.inputAdapterTypeSpec(
.addFunction(notImplementedFromResponseFunSpec(adaptedTypeName))
.addFunction(writeToResponseFunSpec(context, adaptedTypeName))
.apply {
if (this@inputAdapterTypeSpec.any { it.deprecationReason != null }) {
addAnnotation(suppressDeprecationAnnotationSpec)
}
addSuppressions(
deprecation = this@inputAdapterTypeSpec.any { it.deprecationReason != null }
)
if (any { it.optInFeature != null }) {
val requiresOptInAnnotation = context.resolver.resolveRequiresOptInAnnotation()
if (requiresOptInAnnotation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.apollographql.apollo3.compiler.codegen.Identifier.writer
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.NamedType
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.requiresOptInAnnotation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.ir.IrBooleanValue
import com.apollographql.apollo3.compiler.ir.isOptional
import com.squareup.kotlinpoet.AnnotationSpec
Expand All @@ -26,9 +26,7 @@ internal fun List<NamedType>.variablesAdapterTypeSpec(
return TypeSpec.objectBuilder(adapterName)
.addFunction(serializeVariablesFunSpec(context, adaptedTypeName))
.apply {
if (this@variablesAdapterTypeSpec.any { it.deprecationReason != null }) {
addAnnotation(suppressDeprecationAnnotationSpec)
}
addSuppressions(this@variablesAdapterTypeSpec.any { it.deprecationReason != null })
if (any { it.optInFeature != null }) {
val requiresOptInAnnotation = context.resolver.resolveRequiresOptInAnnotation()
if (requiresOptInAnnotation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.deprecatedAnnotation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddDeprecation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddDescription
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddOptIn
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddRequiresOptIn
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeSuppressDeprecation
import com.apollographql.apollo3.compiler.ir.IrEnum
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
Expand Down Expand Up @@ -97,7 +97,7 @@ internal class EnumAsEnumBuilder(
private fun IrEnum.knownEntriesPropertySpec(): PropertySpec {
return PropertySpec.builder(Identifier.knownEntries, KotlinSymbols.List.parameterizedBy(selfClassName))
.addKdoc("All [%T] known at compile time", selfClassName)
.maybeSuppressDeprecation(enum.values)
.addSuppressions(enum.values.any { it.deprecationReason != null })
.maybeAddOptIn(context.resolver, enum.values)
.getter(
FunSpec.getterBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddDeprecation
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddDescription
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddOptIn
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeAddRequiresOptIn
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.maybeSuppressDeprecation
import com.apollographql.apollo3.compiler.ir.IrEnum
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
Expand Down Expand Up @@ -121,7 +121,7 @@ internal class EnumAsSealedBuilder(
private fun IrEnum.safeValueOfFunSpec(): FunSpec {
return FunSpec.builder(safeValueOf)
.addKdoc("Returns the [%T] that represents the specified [rawValue].\n", selfClassName)
.maybeSuppressDeprecation(enum.values)
.addSuppressions(enum.values.any { it.deprecationReason != null })
.maybeAddOptIn(context.resolver, enum.values)
.addParameter("rawValue", KotlinSymbols.String)
.returns(selfClassName)
Expand All @@ -139,7 +139,7 @@ internal class EnumAsSealedBuilder(
private fun IrEnum.knownValuesFunSpec(): FunSpec {
return FunSpec.builder(knownValues)
.addKdoc("Returns all [%T] known at compile time", selfClassName)
.maybeSuppressDeprecation(enum.values)
.addSuppressions(enum.values.any { it.deprecationReason != null })
.maybeAddOptIn(context.resolver, enum.values)
.returns(KotlinSymbols.Array.parameterizedBy(selfClassName))
.addCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.suppressDeprecationAnnotationSpec
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.addSuppressions
import com.apollographql.apollo3.compiler.ir.IrExecutionContextTargetArgument
import com.apollographql.apollo3.compiler.ir.IrGraphqlTargetArgument
import com.apollographql.apollo3.compiler.ir.IrOptionalType
Expand Down Expand Up @@ -52,7 +52,7 @@ internal class MainResolverBuilder(
.addProperty(resolversPropertySpec())
.addFunction(typenameFunSpec())
.addFunction(resolveFunSpec())
.addAnnotation(suppressDeprecationAnnotationSpec)
.addSuppressions(deprecation = true)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ internal fun FunSpec.Builder.maybeAddRequiresOptIn(resolver: KotlinResolver, opt
return addAnnotation(AnnotationSpec.builder(annotation).build())
}

internal val suppressDeprecationAnnotationSpec = AnnotationSpec.builder(KotlinSymbols.Suppress)
.addMember("%S", "DEPRECATION")
.build()

internal fun <T: Annotatable.Builder<*>> T.maybeSuppressDeprecation(enumValues: List<IrEnum.Value>): T = applyIf(enumValues.any { !it.deprecationReason.isNullOrBlank() }) {
addAnnotation(suppressDeprecationAnnotationSpec)
}

internal fun requiresOptInAnnotation(annotation: ClassName): AnnotationSpec {
return AnnotationSpec.builder(KotlinSymbols.OptIn)
.addMember(CodeBlock.of("%T::class", annotation))
Expand All @@ -135,3 +127,33 @@ internal fun <T: Annotatable.Builder<*>> T.maybeAddOptIn(
val annotation = resolver.resolveRequiresOptInAnnotation() ?: return@applyIf
addAnnotation(requiresOptInAnnotation(annotation))
}

/**
* Add suppressions for generated code.
* This is code the user has no control over and it should not generate warnings
*/
internal fun <T: Annotatable.Builder<*>> T.addSuppressions(
deprecation: Boolean = false,
optInUsage: Boolean = false,
unusedParameter: Boolean = false
): T = apply {
if (!deprecation && !optInUsage && !unusedParameter) {
return@apply
}

addAnnotation(
AnnotationSpec.builder(KotlinSymbols.Suppress)
.apply {
if (deprecation) {
addMember("%S", "DEPRECATION")
}
if (optInUsage) {
addMember("%S", "OPT_IN_USAGE")
}
if (unusedParameter) {
addMember("%S", "UNUSED_PARAMETER")
}
}
.build()
)
}
7 changes: 0 additions & 7 deletions tests/deprecated-requires-opt-in/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ dependencies {
testImplementation(libs.junit)
}

/**
* Because of:
*
* w: GetNewFieldQuery_ResponseAdapter.kt:50:82 Apollo: This symbol requires opt-in
*/
allWarningsAsErrors(false)

apollo {
service("default") {
srcDir("graphql")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example

@RequiresOptIn(
level = RequiresOptIn.Level.WARNING,
message = "Apollo: This symbol requires opt-in"
message = "MyRequiresOptIn: This symbol requires opt-in"
)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
annotation class MyRequiresOptIn
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import default.type.SomeInput as SomeInputDefault
import none.GetNewFieldQuery as GetNewFieldQueryNone
import none.type.Direction as DirectionNone
import none.type.SomeInput as SomeInputNone
import com.example.MyRequiresOptIn

@Suppress("DEPRECATION")
class RequiresOptInTest {
/**
* Visual test: this allows to see how the annotation is used but doesn't actually test anything
*/
@Test
@OptIn(ApolloRequiresOptIn::class)
fun visualTest() {
SomeInputNone(
newInputField = Optional.Present(9),
Expand All @@ -31,6 +31,7 @@ class RequiresOptInTest {
newInputField
}

@OptIn(ApolloRequiresOptIn::class)
SomeInputDefault(
newInputField = Optional.Present(9),
oldInputField = Optional.Present(0),
Expand All @@ -39,6 +40,7 @@ class RequiresOptInTest {
newInputField
}

@OptIn(MyRequiresOptIn::class)
SomeInputCustom(
newInputField = Optional.Present(9),
oldInputField = Optional.Present(0),
Expand All @@ -54,13 +56,17 @@ class RequiresOptInTest {
oldField
newField
}

@OptIn(ApolloRequiresOptIn::class)
GetNewFieldQueryDefault.Data(
newField = DirectionDefault.NORTH,
oldField = DirectionDefault.SOUTH
).apply {
oldField
newField
}

@OptIn(MyRequiresOptIn::class)
GetNewFieldQueryCustom.Data(
newField = DirectionCustom.NORTH,
oldField = DirectionCustom.SOUTH
Expand Down Expand Up @@ -90,6 +96,7 @@ class RequiresOptInTest {
assertTrue(customInputAnnotationsMethod?.declaredAnnotations?.any { it.annotationClass.simpleName == "MyRequiresOptIn"} == true)

assertFalse(DirectionDefault.NORTH.javaClass.getField("NORTH").declaredAnnotations.any { it.annotationClass.simpleName == "ApolloRequiresOptIn"})
@OptIn(MyRequiresOptIn::class)
assertTrue(DirectionCustom.NORTH.javaClass.getField("NORTH").declaredAnnotations.any { it.annotationClass.simpleName == "MyRequiresOptIn"})

val noneClass = GetNewFieldQueryNone.Data::class.java
Expand Down

0 comments on commit 1154110

Please sign in to comment.