From 25bb2a6c83f08d7d6ef706a8abe56dbe75089b8f Mon Sep 17 00:00:00 2001 From: altro3 Date: Wed, 4 Sep 2024 05:21:48 +0700 Subject: [PATCH] Add support implicitHeaders and implicitHeadersRegex (#1730) Fixed #1729 --- .../openapi/annotation/OpenAPIGroup.java | 6 +- .../openapi/annotation/OpenAPIGroupInfo.java | 4 +- .../AbstractMicronautJavaCodegen.java | 25 ++++++- .../AbstractMicronautKotlinCodegen.java | 71 ++++++++++++++++++- .../MicronautCodeGeneratorEntryPoint.java | 50 +++++++++++-- .../MicronautCodeGeneratorOptionsBuilder.java | 28 +++++++- .../java-micronaut/client/api.mustache | 1 + .../common/operationAnnotations.mustache | 30 ++------ .../server/controller-interface.mustache | 1 + .../kotlin-micronaut/client/api.mustache | 1 + .../common/operationAnnotations.mustache | 15 +--- .../server/controller-interface.mustache | 1 + .../JavaMicronautClientCodegenTest.java | 26 +++++++ .../KotlinMicronautClientCodegenTest.java | 26 +++++++ 14 files changed, 230 insertions(+), 55 deletions(-) diff --git a/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroup.java b/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroup.java index 2a5d660938..dd5c6f137b 100644 --- a/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroup.java +++ b/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroup.java @@ -40,19 +40,19 @@ public @interface OpenAPIGroup { /** - * @return The names of the OpenAPi groups. + * @return The names of the OpenAPI groups. */ @AliasFor(member = "names") String[] value() default {}; /** - * @return The names of the OpenAPi groups. + * @return The names of the OpenAPI groups. */ @AliasFor(member = "value") String[] names() default {}; /** - * @return The names of the OpenAPi groups to exclude endpoints from. + * @return The names of the OpenAPI groups to exclude endpoints from. */ String[] exclude() default {}; diff --git a/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroupInfo.java b/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroupInfo.java index 82dcc1df1f..41ce1fa78b 100644 --- a/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroupInfo.java +++ b/openapi-annotations/src/main/java/io/micronaut/openapi/annotation/OpenAPIGroupInfo.java @@ -41,13 +41,13 @@ public @interface OpenAPIGroupInfo { /** - * @return The names of the OpenAPi groups. + * @return The names of the OpenAPI groups. */ @AliasFor(member = "names") String[] value() default {}; /** - * @return The names of the OpenAPi groups. + * @return The names of the OpenAPI groups. */ @AliasFor(member = "value") String[] names() default {}; diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index 87a4f05e93..64449c23c5 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -1248,6 +1248,10 @@ public CodegenModel fromModel(String name, Schema model) { return codegenModel; } + private boolean shouldBeImplicitHeader(CodegenParameter parameter) { + return StringUtils.isNotBlank(implicitHeadersRegex) && parameter.baseName.matches(implicitHeadersRegex); + } + @Override public String toEnumValue(String value, String datatype) { if ("Integer".equals(datatype) || "Double".equals(datatype)) { @@ -1279,19 +1283,27 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation op.imports.add(op.returnType); } - op.vendorExtensions.put("originalParams", new ArrayList<>(op.allParams)); + var paramsWithoutImplicitHeaders = new ArrayList(); + var swaggerParams = new ArrayList(); var hasMultipleParams = false; var notBodyParamsSize = 0; for (var param : op.allParams) { - if (param.isBodyParam) { + if (!param.isHeaderParam || (!implicitHeaders && !shouldBeImplicitHeader(param))) { + paramsWithoutImplicitHeaders.add(param); + } + if (param.isBodyParam || param.isFormParam) { continue; } + swaggerParams.add(param); notBodyParamsSize++; if (notBodyParamsSize > 1) { hasMultipleParams = true; - break; } } + op.vendorExtensions.put("swaggerParams", swaggerParams); + op.vendorExtensions.put("originalParams", paramsWithoutImplicitHeaders); + op.vendorExtensions.put("hasNotBodyParam", notBodyParamsSize > 0); + op.vendorExtensions.put("hasMultipleParams", hasMultipleParams); for (var param : op.allParams) { param.vendorExtensions.put("hasNotBodyParam", notBodyParamsSize > 0); param.vendorExtensions.put("hasMultipleParams", hasMultipleParams); @@ -1889,6 +1901,13 @@ public boolean getUseInlineModelResolver() { return false; } + public void setGenerateSwaggerAnnotations(boolean generateSwaggerAnnotations) { + this.generateSwaggerAnnotations = Boolean.toString(generateSwaggerAnnotations); + if (generateSwaggerAnnotations) { + additionalProperties.put("generateSwagger2Annotations", true); + } + } + @Override public void postProcess() { // disable output donation suggestion 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 310df52bda..fde06a78fe 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 @@ -121,6 +121,8 @@ public abstract class AbstractMicronautKotlinCodegen operationList = operations.getOperation(); for (CodegenOperation op : operationList) { + + handleImplicitHeaders(op); + handleConstantParams(op); + // Set whether body is supported in request op.vendorExtensions.put("methodAllowsBody", op.httpMethod.equals("PUT") || op.httpMethod.equals("POST") @@ -947,6 +968,34 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List copy = new ArrayList<>(operation.allParams); + operation.allParams.clear(); + + for (CodegenParameter p : copy) { + if (p.isHeaderParam && (implicitHeaders || shouldBeImplicitHeader(p))) { + operation.implicitHeadersParams.add(p); + operation.headerParams.removeIf(header -> header.baseName.equals(p.baseName)); + once(log).info("Update operation [{}]. Remove header [{}] because it's marked to be implicit", operation.operationId, p.baseName); + } else { + operation.allParams.add(p); + } + } + operation.hasParams = !operation.allParams.isEmpty(); + } + + private boolean shouldBeImplicitHeader(CodegenParameter parameter) { + return StringUtils.isNotBlank(implicitHeadersRegex) && parameter.baseName.matches(implicitHeadersRegex); + } + @Override public String toEnumValue(String value, String datatype) { if ("Integer".equals(datatype) @@ -1168,19 +1217,28 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation op.imports.add(op.returnType); } - op.vendorExtensions.put("originalParams", new ArrayList<>(op.allParams)); + var paramsWithoutImplicitHeaders = new ArrayList(); + var swaggerParams = new ArrayList(); var hasMultipleParams = false; var notBodyParamsSize = 0; for (var param : op.allParams) { - if (param.isBodyParam) { + if (!param.isHeaderParam || (!implicitHeaders && !shouldBeImplicitHeader(param))) { + param.vendorExtensions.computeIfAbsent("realName", k -> param.paramName); + paramsWithoutImplicitHeaders.add(param); + } + if (param.isBodyParam || param.isFormParam) { continue; } + swaggerParams.add(param); notBodyParamsSize++; if (notBodyParamsSize > 1) { hasMultipleParams = true; - break; } } + op.vendorExtensions.put("swaggerParams", swaggerParams); + op.vendorExtensions.put("originalParams", paramsWithoutImplicitHeaders); + op.vendorExtensions.put("hasNotBodyParam", notBodyParamsSize > 0); + op.vendorExtensions.put("hasMultipleParams", hasMultipleParams); for (var param : op.allParams) { param.vendorExtensions.put("hasNotBodyParam", notBodyParamsSize > 0); param.vendorExtensions.put("hasMultipleParams", hasMultipleParams); @@ -2001,6 +2059,13 @@ public boolean getUseInlineModelResolver() { return false; } + public void setGenerateSwaggerAnnotations(boolean generateSwaggerAnnotations) { + this.generateSwaggerAnnotations = Boolean.toString(generateSwaggerAnnotations); + if (generateSwaggerAnnotations) { + additionalProperties.put("generateSwagger2Annotations", true); + } + } + @Override public void postProcess() { // disable output donation suggestion diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java index a0991a8497..45a463df7e 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorEntryPoint.java @@ -180,16 +180,21 @@ private void configureOptions() { if (options.modelNameSuffix != null && !options.modelNameSuffix.isBlank()) { javaCodeGen.setModelNameSuffix(options.modelNameSuffix); } + javaCodeGen.setImplicitHeaders(options.implicitHeaders); + if (options.implicitHeadersRegex != null && !options.implicitHeadersRegex.isBlank()) { + javaCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); + } - javaCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces()); + javaCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); javaCodeGen.setReactive(options.reactive); javaCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); javaCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired); javaCodeGen.setUseOptional(options.optional); javaCodeGen.setUseBeanValidation(options.beanValidation); javaCodeGen.setTestTool(options.testFramework.value); - javaCodeGen.setSerializationLibrary(options.serializationLibraryKind().name()); - javaCodeGen.setDateTimeLibrary(options.dateTimeFormat().name()); + javaCodeGen.setSerializationLibrary(options.serializationLibraryKind.name()); + javaCodeGen.setGenerateSwaggerAnnotations(options.generateSwaggerAnnotations); + javaCodeGen.setDateTimeLibrary(options.dateTimeFormat.name()); configureJavaServerOptions(); configureJavaClientOptions(); } else if (options.lang == GeneratorLanguage.KOTLIN && codeGenerator instanceof AbstractMicronautKotlinCodegen kotlinCodeGen) { @@ -245,14 +250,19 @@ private void configureOptions() { if (options.modelNameSuffix != null && !options.modelNameSuffix.isBlank()) { kotlinCodeGen.setModelNameSuffix(options.modelNameSuffix); } + kotlinCodeGen.setImplicitHeaders(options.implicitHeaders); + if (options.implicitHeadersRegex != null && !options.implicitHeadersRegex.isBlank()) { + kotlinCodeGen.setImplicitHeadersRegex(options.implicitHeadersRegex); + } kotlinCodeGen.setUseOneOfInterfaces(options.useOneOfInterfaces); kotlinCodeGen.setReactive(options.reactive); kotlinCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways); kotlinCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired); + kotlinCodeGen.setGenerateSwaggerAnnotations(options.generateSwaggerAnnotations); kotlinCodeGen.setUseBeanValidation(options.beanValidation); kotlinCodeGen.setTestTool(options.testFramework.value); - kotlinCodeGen.setSerializationLibrary(options.serializationLibraryKind().name()); - kotlinCodeGen.setDateTimeLibrary(options.dateTimeFormat().name()); + kotlinCodeGen.setSerializationLibrary(options.serializationLibraryKind.name()); + kotlinCodeGen.setDateTimeLibrary(options.dateTimeFormat.name()); configureKotlinServerOptions(); configureKotlinClientOptions(); } @@ -511,11 +521,15 @@ private static class DefaultOptionsBuilder implements MicronautCodeGeneratorOpti private String modelNamePrefix; private String modelNameSuffix; + private boolean implicitHeaders; + private String implicitHeadersRegex; + private boolean optional; private boolean reactive = true; private boolean useOneOfInterfaces = true; private boolean generateHttpResponseAlways; private boolean generateHttpResponseWhereRequired = true; + private boolean generateSwaggerAnnotations; private TestFramework testFramework = TestFramework.JUNIT5; private SerializationLibraryKind serializationLibraryKind = SerializationLibraryKind.MICRONAUT_SERDE_JACKSON; private DateTimeFormat dateTimeFormat = DateTimeFormat.ZONED_DATETIME; @@ -641,6 +655,18 @@ public MicronautCodeGeneratorOptionsBuilder withModelNameSuffix(String modelName return this; } + @Override + public MicronautCodeGeneratorOptionsBuilder withImplicitHeaders(boolean implicitHeaders) { + this.implicitHeaders = implicitHeaders; + return this; + } + + @Override + public MicronautCodeGeneratorOptionsBuilder withImplicitHeadersRegex(String implicitHeadersRegex) { + this.implicitHeadersRegex = implicitHeadersRegex; + return this; + } + @Override public MicronautCodeGeneratorOptionsBuilder withReactive(boolean reactive) { this.reactive = reactive; @@ -659,6 +685,12 @@ public MicronautCodeGeneratorOptionsBuilder withGenerateHttpResponseWhereRequire return this; } + @Override + public MicronautCodeGeneratorOptionsBuilder withGenerateSwaggerAnnotations(boolean generateSwaggerAnnotations) { + this.generateSwaggerAnnotations = generateSwaggerAnnotations; + return this; + } + @Override public MicronautCodeGeneratorOptionsBuilder withBeanValidation(boolean beanValidation) { this.beanValidation = beanValidation; @@ -719,12 +751,16 @@ private Options build() { modelNamePrefix, modelNameSuffix, + implicitHeaders, + implicitHeadersRegex, + beanValidation, optional, reactive, useOneOfInterfaces, generateHttpResponseAlways, generateHttpResponseWhereRequired, + generateSwaggerAnnotations, testFramework, serializationLibraryKind, dateTimeFormat); @@ -771,12 +807,16 @@ private record Options( String modelNamePrefix, String modelNameSuffix, + boolean implicitHeaders, + String implicitHeadersRegex, + boolean beanValidation, boolean optional, boolean reactive, boolean useOneOfInterfaces, boolean generateHttpResponseAlways, boolean generateHttpResponseWhereRequired, + boolean generateSwaggerAnnotations, TestFramework testFramework, SerializationLibraryKind serializationLibraryKind, MicronautCodeGeneratorOptionsBuilder.DateTimeFormat dateTimeFormat diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java index d6431f85d1..72cee45e3f 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautCodeGeneratorOptionsBuilder.java @@ -145,9 +145,9 @@ public interface MicronautCodeGeneratorOptionsBuilder { MicronautCodeGeneratorOptionsBuilder withInlineSchemaOption(Map inlineSchemaOption); /** - * Add the OpenAPi normalizer options. + * Add the OpenAPI normalizer options. * - * @param openapiNormalizer the OpenAPi normalizer options + * @param openapiNormalizer the OpenAPI normalizer options * @return this builder */ MicronautCodeGeneratorOptionsBuilder withOpenapiNormalizer(Map openapiNormalizer); @@ -184,6 +184,22 @@ public interface MicronautCodeGeneratorOptionsBuilder { */ MicronautCodeGeneratorOptionsBuilder withModelNameSuffix(String modelNameSuffix); + /** + * Set the implicit headers flag. + * + * @param implicitHeaders implicit headers + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withImplicitHeaders(boolean implicitHeaders); + + /** + * Set the implicit headers regex. + * + * @param implicitHeadersRegex implicit headers regex + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withImplicitHeadersRegex(String implicitHeadersRegex); + /** * If set to true, the generator will use reactive types. * @@ -208,6 +224,14 @@ public interface MicronautCodeGeneratorOptionsBuilder { */ MicronautCodeGeneratorOptionsBuilder withGenerateHttpResponseWhereRequired(boolean generateHttpResponseWhereRequired); + /** + * If set to true, controller and client method will be generated with openAPI annotations. + * + * @param generateSwaggerAnnotations the generate swagger annotations flag + * @return this builder + */ + MicronautCodeGeneratorOptionsBuilder withGenerateSwaggerAnnotations(boolean generateSwaggerAnnotations); + /** * If set to true, the generated code will use bean validation. * diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache index 92e2676621..988cc3372e 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/api.mustache @@ -39,6 +39,7 @@ import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.security.SecurityRequirement; {{/generateSwagger2Annotations}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/operationAnnotations.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/operationAnnotations.mustache index 37a169fb93..edeeed4acf 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/operationAnnotations.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/operationAnnotations.mustache @@ -81,11 +81,11 @@ {{closebrace}}{{/produces.1}}{{^produces.1}}{{#produces}}@Content(mediaType = "{{{mediaType}}}", schema = @Schema(implementation = {{{baseType}}}.class)){{/produces}}{{/produces.1}}{{/baseType}}){{^-last}},{{/-last}}{{#responses.1}} {{/responses.1}}{{/responses}}{{#responses.1}} {{closebrace}}{{/responses.1}}{{#vendorExtensions.hasNotBodyParam}}, - parameters = {{#vendorExtensions.originalParams.1}}{{openbrace}} - {{/vendorExtensions.originalParams.1}}{{#vendorExtensions.originalParams}}{{#vendorExtensions.originalParams.1}} - {{/vendorExtensions.originalParams.1}}{{^isBodyParam}}@Parameter(name = "{{paramName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}){{^-last}},{{/-last}}{{/isBodyParam}}{{#vendorExtensions.hasMultipleParams}} - {{/vendorExtensions.hasMultipleParams}}{{/vendorExtensions.originalParams}}{{#vendorExtensions.originalParams.1}} - {{closebrace}}{{/vendorExtensions.originalParams.1}}{{/vendorExtensions.hasNotBodyParam}}{{#hasAuthMethods}}, + parameters = {{#vendorExtensions.swaggerParams.1}}{{openbrace}} + {{/vendorExtensions.swaggerParams.1}}{{#vendorExtensions.swaggerParams}}{{#vendorExtensions.swaggerParams.1}} + {{/vendorExtensions.swaggerParams.1}}@Parameter(name = "{{baseName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}, in = ParameterIn.{{#isCookieParam}}COOKIE{{/isCookieParam}}{{#isHeaderParam}}HEADER{{/isHeaderParam}}{{#isQueryParam}}QUERY{{/isQueryParam}}{{#isPathParam}}PATH{{/isPathParam}}){{^-last}},{{/-last}}{{#vendorExtensions.hasMultipleParams}} + {{/vendorExtensions.hasMultipleParams}}{{/vendorExtensions.swaggerParams}}{{#vendorExtensions.swaggerParams.1}} + {{closebrace}}{{/vendorExtensions.swaggerParams.1}}{{/vendorExtensions.hasNotBodyParam}}{{#hasAuthMethods}}, security = {{#authMethods.1}}{{openbrace}} {{/authMethods.1}}{{#authMethods}}{{#authMethods.1}} {{/authMethods.1}}@SecurityRequirement(name = "{{name}}"{{#isOAuth}}{{#scopes.1}}, scopes = {{openbrace}}{{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}}{{closebrace}}{{/scopes.1}}{{/isOAuth}}){{^-last}},{{/-last}} @@ -93,23 +93,3 @@ {{closebrace}}{{/authMethods.1}}{{/hasAuthMethods}} ) {{/generateSwagger2Annotations}} - {{#implicitHeadersParams.0}} - {{#generateSwagger2Annotations}} - {{#implicitHeadersParams.1}} - @Parameters({{openbrace}} - {{/implicitHeadersParams.1}} - {{#implicitHeadersParams}} - @Parameter(name = "{{paramName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}){{^-last}},{{/-last}} - {{/implicitHeadersParams}} - {{#implicitHeadersParams.1}} - {{closebrace}}) - {{/implicitHeadersParams.1}} - {{/generateSwagger2Annotations}} - {{#generateSwagger1Annotations}} - @ApiImplicitParams({{openbrace}} - {{#implicitHeadersParams}} - @ApiImplicitParam(name = "{{{baseName}}}", value = "{{{description}}}", {{#required}}required = true,{{/required}} dataType = "{{{dataType}}}", paramType = "header"){{^-last}},{{/-last}} - {{/implicitHeadersParams}} - {{closebrace}}) - {{/generateSwagger1Annotations}} - {{/implicitHeadersParams.0}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache index 55d3f358d1..0e28635342 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/controller-interface.mustache @@ -39,6 +39,7 @@ import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.security.SecurityRequirement; {{/generateSwagger2Annotations}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/api.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/api.mustache index 81368787f0..830e3a7e2e 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/api.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/client/api.mustache @@ -31,6 +31,7 @@ import io.swagger.v3.oas.annotations.Parameters import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.enums.ParameterIn import io.swagger.v3.oas.annotations.tags.Tag import io.swagger.v3.oas.annotations.security.SecurityRequirement {{/generateSwagger2Annotations}} 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 15ee45cf7c..c19500e8ed 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 @@ -51,11 +51,9 @@ {{/responses}} ]{{#vendorExtensions.hasNotBodyParam}}, parameters = [ - {{#vendorExtensions.originalParams}} - {{^isBodyParam}} - Parameter(name = "{{vendorExtensions.realName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}){{^-last}},{{/-last}} - {{/isBodyParam}} - {{/vendorExtensions.originalParams}} + {{#vendorExtensions.swaggerParams}} + Parameter(name = "{{baseName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}, `in` = ParameterIn.{{#isCookieParam}}COOKIE{{/isCookieParam}}{{#isHeaderParam}}HEADER{{/isHeaderParam}}{{#isQueryParam}}QUERY{{/isQueryParam}}{{#isPathParam}}PATH{{/isPathParam}}){{^-last}},{{/-last}} + {{/vendorExtensions.swaggerParams}} ]{{/vendorExtensions.hasNotBodyParam}}{{#hasAuthMethods}}, security = [ {{#authMethods}} @@ -64,10 +62,3 @@ ]{{/hasAuthMethods}} ) {{/generateSwagger2Annotations}} - {{#implicitHeadersParams.0}} - {{#generateSwagger2Annotations}} - {{#implicitHeadersParams}} - @Parameter(name = "{{vendorExtensions.realName}}"{{#description}}, description = "{{{description}}}"{{/description}}{{#required}}, required = true{{/required}}) - {{/implicitHeadersParams}} - {{/generateSwagger2Annotations}} - {{/implicitHeadersParams.0}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/controller-interface.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/controller-interface.mustache index 07203a0578..ba9ee798d8 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/controller-interface.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/server/controller-interface.mustache @@ -28,6 +28,7 @@ import io.swagger.v3.oas.annotations.Parameters import io.swagger.v3.oas.annotations.media.Content import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.enums.ParameterIn import io.swagger.v3.oas.annotations.tags.Tag import io.swagger.v3.oas.annotations.security.SecurityRequirement {{/generateSwagger2Annotations}} diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index 308124e068..de1d4fb485 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -587,4 +587,30 @@ void testLombok() { "@AllArgsConstructor", "@Data"); } + + @Test + void testImplicitHeaders() { + + var codegen = new JavaMicronautClientCodegen(); + codegen.setImplicitHeaders(true); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/params-with-default-value.yml", CodegenConstants.APIS); + String path = outputPath + "src/main/java/org/openapitools/"; + + assertFileNotContains(path + "api/DefaultApi.java", "@Header(\"X-Favor-Token\") @Nullable String xFavorToken", + "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable String contentType" + ); + } + + @Test + void testImplicitHeadersRegex() { + + var codegen = new JavaMicronautClientCodegen(); + codegen.setImplicitHeadersRegex(".*"); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/params-with-default-value.yml", CodegenConstants.APIS); + String path = outputPath + "src/main/java/org/openapitools/"; + + assertFileNotContains(path + "api/DefaultApi.java", "@Header(\"X-Favor-Token\") @Nullable String xFavorToken", + "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable String contentType" + ); + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index a4547124c1..232e738f4c 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -625,4 +625,30 @@ void testSingleProduceContentType() { assertFileContains(path + "api/FilesApi.kt", "@Produces(\"application/octet-stream\")"); } + + @Test + void testImplicitHeaders() { + + var codegen = new KotlinMicronautClientCodegen(); + codegen.setImplicitHeaders(true); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/params-with-default-value.yml", CodegenConstants.APIS); + String path = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileNotContains(path + "api/DefaultApi.kt", "@Header(\"X-Favor-Token\") @Nullable xFavorToken: String? = null,", + "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable contentType: String? = \"application/json\"" + ); + } + + @Test + void testImplicitHeadersRegex() { + + var codegen = new KotlinMicronautClientCodegen(); + codegen.setImplicitHeadersRegex(".*"); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/params-with-default-value.yml", CodegenConstants.APIS); + String path = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileNotContains(path + "api/DefaultApi.kt", "@Header(\"X-Favor-Token\") @Nullable xFavorToken: String? = null,", + "@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable contentType: String? = \"application/json\"" + ); + } }