From 280ce75590a8f485face55e3291200f05f1191a7 Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Tue, 8 Dec 2020 20:26:48 +0100 Subject: [PATCH] Set default value for requireAuthentication (#155) * Set the default value of requireAuthentication based on the security requirements * Improve requireAuthentication code readability --- .editorconfig | 1 + .../jellyfin/openapi/builder/api/OperationUrlBuilder.kt | 2 +- .../openapi/builder/openapi/OpenApiApiServicesBuilder.kt | 8 ++++++++ .../kotlin/org/jellyfin/openapi/constants/Security.kt | 6 ++++++ .../org/jellyfin/openapi/model/ApiServiceOperation.kt | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Security.kt diff --git a/.editorconfig b/.editorconfig index 3d79f124..b2944ad7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,6 +2,7 @@ root = true [*] insert_final_newline = true +end_of_line = lf [{*.kts, *.kt}] charset = utf-8 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 46261a46..6283df3e 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 @@ -54,7 +54,7 @@ class OperationUrlBuilder( .forEach { parameter -> addParameter(buildParameter(parameter)) } ParameterSpec.builder("includeCredentials", Boolean::class).apply { - defaultValue("%L", "false") + defaultValue("%L", data.requireAuthentication) addKdoc("%L", Strings.INCLUDE_CREDENTIALS_DESCRIPTION) }.build().let(::addParameter) diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/openapi/OpenApiApiServicesBuilder.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/openapi/OpenApiApiServicesBuilder.kt index 6c04ed03..1040db15 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/openapi/OpenApiApiServicesBuilder.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/builder/openapi/OpenApiApiServicesBuilder.kt @@ -8,6 +8,7 @@ import net.pearx.kasechange.toCamelCase import org.jellyfin.openapi.builder.Builder import org.jellyfin.openapi.builder.api.ApiNameBuilder import org.jellyfin.openapi.constants.MimeType +import org.jellyfin.openapi.constants.Security import org.jellyfin.openapi.constants.Strings import org.jellyfin.openapi.hooks.ApiTypePath import org.jellyfin.openapi.model.ApiService @@ -73,12 +74,19 @@ class OpenApiApiServicesBuilder( if (returnType == Unit::class.asTypeName() && "200" in operation.responses) println("Missing return-type for operation $operationName (status-codes: ${operation.responses.keys})") + val requireAuthentication = operation.security + ?.firstOrNull { requirement -> requirement.containsKey(Security.SECURITY_SCHEME) } + ?.get(Security.SECURITY_SCHEME) + ?.any(Security.AUTHENTICATION_POLICIES::contains) + ?: false + operations[serviceName]!! += ApiServiceOperation( name = operationName, description = operation.description ?: operation.summary, deprecated = operation.deprecated == true, pathTemplate = path, method = method, + requireAuthentication = requireAuthentication, returnType = returnType, pathParameters = pathParameters, queryParameters = queryParameters, diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Security.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Security.kt new file mode 100644 index 00000000..2a95afa8 --- /dev/null +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/constants/Security.kt @@ -0,0 +1,6 @@ +package org.jellyfin.openapi.constants + +object Security { + const val SECURITY_SCHEME = "CustomAuthentication" + val AUTHENTICATION_POLICIES = arrayOf("DefaultAuthorization", "RequiresElevation") +} diff --git a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/model/ApiServiceOperation.kt b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/model/ApiServiceOperation.kt index 3a48b99a..83f4dacc 100644 --- a/openapi-generator/src/main/kotlin/org/jellyfin/openapi/model/ApiServiceOperation.kt +++ b/openapi-generator/src/main/kotlin/org/jellyfin/openapi/model/ApiServiceOperation.kt @@ -8,6 +8,7 @@ data class ApiServiceOperation( val deprecated: Boolean, val pathTemplate: String, val method: HttpMethod, + val requireAuthentication: Boolean, val returnType: TypeName, val pathParameters: Collection = emptyList(), val queryParameters: Collection = emptyList(),