Skip to content

Commit

Permalink
Merge pull request #1823 from altro3/fix-swagger-annotations
Browse files Browse the repository at this point in the history
Fix generating swagger annotations
  • Loading branch information
altro3 authored Oct 21, 2024
2 parents a85fbf6 + 330951b commit e333412
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import io.micronaut.http.client.annotation.Client;
import {{invokerPackage}}.auth.Authorization;
{{/configureAuth}}
import io.micronaut.core.convert.format.Format;
{{#reactive}}
import reactor.core.publisher.Mono;
import reactor.core.publisher.Flux;
{{/reactive}}
{{#wrapInHttpResponse}}
import io.micronaut.http.HttpResponse;
{{/wrapInHttpResponse}}
Expand All @@ -33,6 +29,7 @@ import {{javaxPackage}}.validation.constraints.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -54,8 +51,8 @@ public interface {{classname}} {

{{#operation}}
{{#formatNoEmptyLines}}
{{>common/operationAnnotations}}{{!
}} @{{#lambda.pascalcase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.pascalcase}}("{{{path}}}")
{{>common/operationAnnotations}}
@{{#lambda.pascalcase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.pascalcase}}("{{{path}}}")
{{^vendorExtensions.onlyDefaultProduceOrEmpty}}
{{#produces}}{{#-first}}@Consumes({{#produces.1}}{{openbrace}}{{/produces.1}}{{/-first}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{#-last}}{{#produces.1}}{{closebrace}}{{/produces.1}}){{/-last}}{{/produces}}
{{/vendorExtensions.onlyDefaultProduceOrEmpty}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{{/responses.1}}{{#responses}}{{#responses.1}}
{{/responses.1}}@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = {{#produces.1}}{{openbrace}}
{{#produces}}
@Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema({{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class)){{#isArray}}){{/isArray}}){{^-last}},{{/-last}}
@Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema({{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class){{#isArray}}){{/isArray}}){{^-last}},{{/-last}}
{{/produces}}
{{closebrace}}{{/produces.1}}{{^produces.1}}{{#produces}}@Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = @ArraySchema({{/isArray}}schema = @Schema(implementation = {{{baseType}}}.class)){{#isArray}}){{/isArray}}{{/produces}}{{/produces.1}}{{/baseType}}){{^-last}},{{/-last}}{{#responses.1}}
{{/responses.1}}{{/responses}}{{#responses.1}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import io.micronaut.core.convert.format.Format;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
{{/useAuth}}
{{#reactive}}
import reactor.core.publisher.Mono;
import reactor.core.publisher.Flux;
{{/reactive}}
{{#wrapInHttpResponse}}
import io.micronaut.http.HttpResponse;
{{/wrapInHttpResponse}}
Expand All @@ -33,6 +29,7 @@ import {{javaxPackage}}.validation.constraints.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {{javaxPackage}}.validation.constraints.*
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.Parameters
import io.swagger.v3.oas.annotations.media.ArraySchema
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
@Generated({{^hideGenerationTimestamp}}value = {{/hideGenerationTimestamp}}"{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
{{/externalDocs}}
*/
{{#generateSwagger2Annotations}}
@Suppress("DEPRECATED_JAVA_ANNOTATION")
{{#isDeprecated}}
@Deprecated("{{#vendorExtensions.x-deprecated-message}}{{{.}}}{{/vendorExtensions.x-deprecated-message}}")
{{/isDeprecated}}
Expand All @@ -54,7 +53,7 @@
{{#responses}}
ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{#baseType}}, content = [
{{#produces}}
Content(mediaType = "{{{mediaType}}}", schema = Schema(implementation = {{{baseType}}}::class)){{^-last}},{{/-last}}
Content(mediaType = "{{{mediaType}}}", {{#isArray}}array = ArraySchema({{/isArray}}schema = Schema(implementation = {{{baseType}}}::class){{#isArray}}){{/isArray}}){{^-last}},{{/-last}}
{{/produces}}
]{{/baseType}}){{^-last}},{{/-last}}
{{/responses}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {{javaxPackage}}.validation.constraints.*
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.Parameters
import io.swagger.v3.oas.annotations.media.ArraySchema
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ void testEnumsExtensionsAndPrimitives() {
@JsonProperty("3")
THE_THREE(3),
"""
);
);

assertFileContains(modelPath + "LongEnum.java",
"""
Expand All @@ -974,36 +974,36 @@ void testPrimitives() {

assertFileContains(basePath + "api/ParametersApi.java",
"@QueryValue(\"name\") @NotNull String name",
"@QueryValue(\"byteType\") @NotNull Byte byteType",
"@QueryValue(\"byteType2\") @NotNull Byte byteType2",
"@QueryValue(\"shortType\") @NotNull Short shortType",
"@QueryValue(\"shortType2\") @NotNull Short shortType2",
"@QueryValue(\"intType\") @NotNull Integer intType",
"@QueryValue(\"longType\") @NotNull Long longType",
"@QueryValue(\"boolType\") @NotNull Boolean boolType",
"@QueryValue(\"decimalType\") @NotNull BigDecimal decimalType",
"@QueryValue(\"floatType\") @NotNull Float floatType",
"@QueryValue(\"doubleType\") @NotNull Double doubleType",
"@QueryValue(\"bytePrimitiveType\") @NotNull Byte bytePrimitiveType",
"@QueryValue(\"shortPrimitiveType\") @NotNull Short shortPrimitiveType",
"@QueryValue(\"intPrimitiveType\") @NotNull Integer intPrimitiveType",
"@QueryValue(\"longPrimitiveType\") @NotNull Long longPrimitiveType",
"@QueryValue(\"floatPrimitiveType\") @NotNull Float floatPrimitiveType",
"@QueryValue(\"doublePrimitiveType\") @NotNull Double doublePrimitiveType",
"@QueryValue(\"charPrimitiveType\") @NotNull Character charPrimitiveType",
"@QueryValue(\"bytePrimitiveTypes\") @NotNull List<Byte> bytePrimitiveTypes",
"@QueryValue(\"shortPrimitiveTypes\") @NotNull List<Short> shortPrimitiveTypes",
"@QueryValue(\"intPrimitiveTypes\") @NotNull List<Integer> intPrimitiveTypes",
"@QueryValue(\"longPrimitiveTypes\") @NotNull List<Long> longPrimitiveTypes",
"@QueryValue(\"floatPrimitiveTypes\") @NotNull List<Float> floatPrimitiveTypes",
"@QueryValue(\"doublePrimitiveTypes\") @NotNull List<Double> doublePrimitiveTypes",
"@QueryValue(\"charPrimitiveTypes\") @NotNull List<Character> charPrimitiveTypes",
"@QueryValue(\"byteTypes\") @NotNull List<@NotNull Byte> byteTypes",
"@QueryValue(\"byteTypes2\") @NotNull List<@NotNull Byte> byteTypes2",
"@QueryValue(\"shortTypes\") @NotNull List<@NotNull Short> shortTypes",
"@QueryValue(\"shortTypes2\") @NotNull List<@NotNull Short> shortTypes2",
"@QueryValue(\"intTypes\") @NotNull List<@NotNull Integer> intTypes",
"@QueryValue(\"longTypes\") @NotNull List<@NotNull Long> longTypes"
"@QueryValue(\"byteType\") @NotNull Byte byteType",
"@QueryValue(\"byteType2\") @NotNull Byte byteType2",
"@QueryValue(\"shortType\") @NotNull Short shortType",
"@QueryValue(\"shortType2\") @NotNull Short shortType2",
"@QueryValue(\"intType\") @NotNull Integer intType",
"@QueryValue(\"longType\") @NotNull Long longType",
"@QueryValue(\"boolType\") @NotNull Boolean boolType",
"@QueryValue(\"decimalType\") @NotNull BigDecimal decimalType",
"@QueryValue(\"floatType\") @NotNull Float floatType",
"@QueryValue(\"doubleType\") @NotNull Double doubleType",
"@QueryValue(\"bytePrimitiveType\") @NotNull Byte bytePrimitiveType",
"@QueryValue(\"shortPrimitiveType\") @NotNull Short shortPrimitiveType",
"@QueryValue(\"intPrimitiveType\") @NotNull Integer intPrimitiveType",
"@QueryValue(\"longPrimitiveType\") @NotNull Long longPrimitiveType",
"@QueryValue(\"floatPrimitiveType\") @NotNull Float floatPrimitiveType",
"@QueryValue(\"doublePrimitiveType\") @NotNull Double doublePrimitiveType",
"@QueryValue(\"charPrimitiveType\") @NotNull Character charPrimitiveType",
"@QueryValue(\"bytePrimitiveTypes\") @NotNull List<Byte> bytePrimitiveTypes",
"@QueryValue(\"shortPrimitiveTypes\") @NotNull List<Short> shortPrimitiveTypes",
"@QueryValue(\"intPrimitiveTypes\") @NotNull List<Integer> intPrimitiveTypes",
"@QueryValue(\"longPrimitiveTypes\") @NotNull List<Long> longPrimitiveTypes",
"@QueryValue(\"floatPrimitiveTypes\") @NotNull List<Float> floatPrimitiveTypes",
"@QueryValue(\"doublePrimitiveTypes\") @NotNull List<Double> doublePrimitiveTypes",
"@QueryValue(\"charPrimitiveTypes\") @NotNull List<Character> charPrimitiveTypes",
"@QueryValue(\"byteTypes\") @NotNull List<@NotNull Byte> byteTypes",
"@QueryValue(\"byteTypes2\") @NotNull List<@NotNull Byte> byteTypes2",
"@QueryValue(\"shortTypes\") @NotNull List<@NotNull Short> shortTypes",
"@QueryValue(\"shortTypes2\") @NotNull List<@NotNull Short> shortTypes2",
"@QueryValue(\"intTypes\") @NotNull List<@NotNull Integer> intTypes",
"@QueryValue(\"longTypes\") @NotNull List<@NotNull Long> longTypes"
);

assertFileContains(basePath + "model/Obj.java",
Expand Down Expand Up @@ -1320,14 +1320,47 @@ void testNoVars() {

assertFileContains(path + "model/Book.java",
"""
@Serdeable
@Generated("io.micronaut.openapi.generator.JavaMicronautClientCodegen")
@Serializable
public class Book {
@Override
""");
@Serdeable
@Generated("io.micronaut.openapi.generator.JavaMicronautClientCodegen")
@Serializable
public class Book {
@Override
""");

System.clearProperty("micronaut.test.no-vars");
}

@Test
void testSwaggerAnnotations() {

var codegen = new JavaMicronautClientCodegen();
codegen.setGenerateSwaggerAnnotations(true);
String outputPath = generateFiles(codegen, "src/test/resources/petstore.json", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/PetApi.java",
"""
@Operation(
operationId = "findPetsByStatus",
summary = "Finds Pets by status",
description = "Multiple status values can be provided with comma separated strings",
responses = {
@ApiResponse(responseCode = "200", description = "successful operation", content = {
@Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Pet.class))),
@Content(mediaType = "application/xml", array = @ArraySchema(schema = @Schema(implementation = Pet.class)))
}),
@ApiResponse(responseCode = "400", description = "Invalid status value")
},
parameters = @Parameter(name = "status", description = "Status values that need to be considered for filter", in = ParameterIn.QUERY)
,
security = @SecurityRequirement(name = "petstore_auth", scopes = {"write:pets", "read:pets"})
)
@Get("/pet/findByStatus")
@Consumes({"application/json", "application/xml"})
Mono<@NotNull List<@Valid Pet>> findPetsByStatus(
@QueryValue(value = "status", defaultValue = "[\\"available\\"]") @Nullable List<@NotNull String> status
);
""");
}
}
Loading

0 comments on commit e333412

Please sign in to comment.