From ec837fd57c87f767a14a8f3716895367464967a1 Mon Sep 17 00:00:00 2001 From: altro3 Date: Fri, 24 Nov 2023 14:48:22 +0700 Subject: [PATCH] Fix kotlin generator with security scopes --- gradle/libs.versions.toml | 2 +- .../generator/AbstractMicronautKotlinCodegen.java | 3 +-- .../common/operationAnnotations.mustache | 2 +- .../generator/KotlinMicronautServerCodegenTest.java | 10 ++++++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ccf4f738cd..baf3ce9b72 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -7,7 +7,7 @@ managed-parboiled = "1.4.1" managed-freemarker = "2.3.32" managed-pegdown = "1.6.0" -kotlin = "1.9.20" +kotlin = "1.9.21" ksp = "1.9.20-1.0.14" jspecify = "0.3.0" jdt-annotation = "2.2.700" diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java index 45d6517d2a..535accedb1 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java @@ -80,7 +80,6 @@ public abstract class AbstractMicronautKotlinCodegen + case OPT_TEST_JUNIT -> testTool = (String) additionalProperties.get(OPT_TEST); default -> throw new RuntimeException("Test tool \"" + additionalProperties.get(OPT_TEST) + "\" is not supported or misspelled."); diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/operationAnnotations.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/operationAnnotations.mustache index 9ceaad04c0..b0661b773b 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/operationAnnotations.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/operationAnnotations.mustache @@ -57,7 +57,7 @@ ]{{/hasParams}}{{#hasAuthMethods}}, security = [ {{#authMethods}} - SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = {{openbrace}}{{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}}{{closebrace}}{{/isOAuth}}){{^-last}},{{/-last}} + SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = [{{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}}]{{/isOAuth}}){{^-last}},{{/-last}} {{/authMethods}} ]{{/hasAuthMethods}} ) diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java index d28a3eb661..8749a997ff 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java @@ -205,6 +205,16 @@ void generateAuthRolesWithExtension() { assertFileContainsRegex(apiPath + "UsersApi.kt", "IS_AUTHENTICATED[^;]{0,100}updateProfile"); } + @Test + void generateAuth() { + var codegen = new KotlinMicronautServerCodegen(); + codegen.additionalProperties().put(KotlinMicronautServerCodegen.OPT_USE_AUTH, true); + String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.MODELS, CodegenConstants.APIS); + + String apiPath = outputPath + "src/main/kotlin/org/openapitools/api/"; + assertFileContains(apiPath + "PetApi.kt", "SecurityRequirement(name = \"petstore_auth\", scopes = [\"write:pets\", \"read:pets\"])"); + } + @Test void doGenerateMonoWrapHttpResponse() { var codegen = new KotlinMicronautServerCodegen();