From a48c7f8be91fb5dfd520a2c398d3e8730164ba1d Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Sun, 24 Jan 2021 10:27:40 +0100 Subject: [PATCH] Change behavior for deprecated parameters in API operations to generate a secondary function The @Deprecated annotation is not supported by Kotlin (although IntelliJ worked fine with it!). The new behavior will generate one function with the deprecated members removed and another one with them added. The latter will have a "Deprecated" suffix in the name and a @Deprecated annotation on the function. --- .../openapi/builder/api/ApiBuilder.kt | 23 ++++++++++++++++++- .../openapi/builder/api/OperationBuilder.kt | 3 --- .../builder/api/OperationUrlBuilder.kt | 5 +--- .../org/jellyfin/openapi/constants/Strings.kt | 10 ++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/ApiBuilder.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/ApiBuilder.kt index a42ab5a2..95097e9d 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/ApiBuilder.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/ApiBuilder.kt @@ -4,6 +4,7 @@ import com.squareup.kotlinpoet.* import org.jellyfin.openapi.builder.Builder import org.jellyfin.openapi.constants.Classes import org.jellyfin.openapi.constants.Packages +import org.jellyfin.openapi.constants.Strings import org.jellyfin.openapi.hooks.OperationUrlHook import org.jellyfin.openapi.model.ApiService import org.jellyfin.openapi.model.JellyFile @@ -19,8 +20,28 @@ class ApiBuilder( addProperty(PropertySpec.builder("api", apiClientType, KModifier.PRIVATE).initializer("api").build()) primaryConstructor(FunSpec.constructorBuilder().addParameter("api", apiClientType).build()) + // Handle deprecated members + val operations = data.operations.map { namedOperation -> + // Check if any member is deprecated + if (namedOperation.queryParameters.any { it.deprecated }) { + // Return 2 operations, one with and one without deprecated members + listOf( + // Remove deprecated parameters from normal function + namedOperation.copy( + queryParameters = namedOperation.queryParameters.filterNot { it.deprecated } + ), + // Add new "deprecated" function with old parameters + namedOperation.copy( + name = namedOperation.name + Strings.DEPRECATED_OPERATION_SUFFIX, + // Mark the operation as deprecated + deprecated = true + ) + ) + } else listOf(namedOperation) + }.flatten() + // Add operations - data.operations.forEach { namedOperation -> + operations.forEach { namedOperation -> addFunction(operationBuilder.build(namedOperation)) if (operationUrlHooks.any { it.shouldOperationBuildUrlFun(data, namedOperation) }) diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationBuilder.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationBuilder.kt index e845652b..84f4e31a 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationBuilder.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationBuilder.kt @@ -50,9 +50,6 @@ class OperationBuilder( // Add description data.description?.let { addKdoc("%L", it) } - - // Add deprecated annotation - if (data.deprecated) addAnnotation(deprecatedAnnotationSpecBuilder.build(Strings.DEPRECATED_MEMBER)) }.build() diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationUrlBuilder.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationUrlBuilder.kt index 6283df3e..460dc57e 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationUrlBuilder.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/api/OperationUrlBuilder.kt @@ -11,7 +11,7 @@ import org.jellyfin.openapi.model.ApiServiceOperationParameter class OperationUrlBuilder( private val deprecatedAnnotationSpecBuilder: DeprecatedAnnotationSpecBuilder ) : Builder { - private fun buildFunctionShell(data: ApiServiceOperation) = FunSpec.builder(data.name + "Url").apply { + private fun buildFunctionShell(data: ApiServiceOperation) = FunSpec.builder(data.name + Strings.URL_OPERATION_SUFFIX).apply { // Add description data.description?.let { addKdoc("%L", it) } @@ -28,9 +28,6 @@ class OperationUrlBuilder( // Add description data.description?.let { addKdoc("%L", it) } - - // Add deprecated annotation - if (data.deprecated) addAnnotation(deprecatedAnnotationSpecBuilder.build(Strings.DEPRECATED_MEMBER)) }.build() diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Strings.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Strings.kt index 148c0e58..5307820e 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Strings.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Strings.kt @@ -31,4 +31,14 @@ object Strings { * The description used for the "includeCredentials" parameter in API URL functions */ const val INCLUDE_CREDENTIALS_DESCRIPTION = "Add the access token to the url to make an authenticated request." + + /** + * The suffix added to the name of a deprecated operation. + */ + const val DEPRECATED_OPERATION_SUFFIX = "Deprecated" + + /** + * The suffix added to the name of a URL operation. Added after [URL_OPERATION_SUFFIX]. + */ + const val URL_OPERATION_SUFFIX = "Url" }