From 468e5da71c0f21af9f353606421df551954c586d Mon Sep 17 00:00:00 2001 From: Antoine REY Date: Mon, 5 Dec 2022 10:28:38 +0100 Subject: [PATCH] #14141 Add externalDocs to @Operation to the JavaSpring generator --- .../codegen/CodegenOperation.java | 6 ++- .../openapitools/codegen/DefaultCodegen.java | 1 + .../main/resources/JavaSpring/api.mustache | 7 +++- .../java/spring/SpringCodegenTest.java | 41 +++++++++++++++++++ .../src/test/resources/3_0/petstore.yaml | 3 ++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java index 2672a1709a35..528d10d0a888 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenOperation.java @@ -24,7 +24,7 @@ public class CodegenOperation { public final List responseHeaders = new ArrayList(); - public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams, + public boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, hasRequiredParams, hasExternalDocs, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap, isArray, isMultipart, isResponseBinary = false, isResponseFile = false, isResponseOptional = false, hasReference = false, defaultReturnType = false, @@ -316,6 +316,7 @@ public String toString() { sb.append(", hasParams=").append(hasParams); sb.append(", hasOptionalParams=").append(hasOptionalParams); sb.append(", hasRequiredParams=").append(hasRequiredParams); + sb.append(", hasExternalDocs=").append(hasExternalDocs); sb.append(", returnTypeIsPrimitive=").append(returnTypeIsPrimitive); sb.append(", returnSimpleType=").append(returnSimpleType); sb.append(", subresourceOperation=").append(subresourceOperation); @@ -393,6 +394,7 @@ public boolean equals(Object o) { hasParams == that.hasParams && hasOptionalParams == that.hasOptionalParams && hasRequiredParams == that.hasRequiredParams && + hasExternalDocs == that.hasExternalDocs && returnTypeIsPrimitive == that.returnTypeIsPrimitive && returnSimpleType == that.returnSimpleType && subresourceOperation == that.subresourceOperation && @@ -462,7 +464,7 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(responseHeaders, hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams, - hasRequiredParams, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap, + hasRequiredParams, hasExternalDocs, returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMap, isArray, isMultipart, isResponseBinary, isResponseFile, isResponseOptional, hasReference, hasDefaultResponse, isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy, isRestful, isDeprecated, isCallbackRequest, uniqueItems, path, operationId, returnType, httpMethod, diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 0e580ae31d48..42b9cd82ad6a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4478,6 +4478,7 @@ else if (one.required) op.requiredParams = requiredParams; op.optionalParams = optionalParams; op.externalDocs = operation.getExternalDocs(); + op.hasExternalDocs = op.externalDocs != null; // legacy support op.nickname = op.operationId; diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache index 0ae5e6db62aa..34bd4624fb1c 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache @@ -16,6 +16,9 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; +{{#hasAuthMethods}} +import io.swagger.v3.oas.annotations.ExternalDocumentation; +{{/hasAuthMethods}} {{/swagger2AnnotationLibrary}} {{#swagger1AnnotationLibrary}} import io.swagger.annotations.*; @@ -170,7 +173,9 @@ public interface {{classname}} { {{#authMethods}} @SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes={ {{#scopes}}"{{scope}}"{{^-last}}, {{/-last}}{{/scopes}} }{{/isOAuth}}){{^-last}},{{/-last}} {{/authMethods}} - }{{/hasAuthMethods}} + }{{/hasAuthMethods}}{{#hasExternalDocs}}, + externalDocs = @ExternalDocumentation(description = "{{externalDocs.description}}", url = "{{externalDocs.url}}") + {{/hasExternalDocs}} ) {{/swagger2AnnotationLibrary}} {{#swagger1AnnotationLibrary}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index fb1f01b7e150..ee4e604f6819 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -1244,6 +1244,47 @@ public void shouldPurAdditionalModelTypesOverAllModels() throws IOException { } } + @Test + public void shouldGenerateExternalDocs() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI(); + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(SpringCodegen.USE_TAGS, "true"); + codegen.additionalProperties().put(BeanValidationFeatures.USE_BEANVALIDATION, "true"); + + ClientOptInput input = new ClientOptInput() + .openAPI(openAPI) + .config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "false"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false"); + generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false"); + + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert.assertThat(files.get("PetApi.java")) + .printFileContent() + .assertMethod("updatePet") + .assertMethodAnnotations() + .containsWithName("Operation") + .containsWithNameAndAttributes("Operation", + ImmutableMap.of( + "operationId", "\"updatePet\"", + //"security", "{ @SecurityRequirement(name = \"petstore_auth\", scopes = { \"write:pets\", \"read:pets\" }) }", + "externalDocs", "@ExternalDocumentation(description = \"API documentation for the updatePet operation\", url = \"http://petstore.swagger.io/v2/doc/updatePet\")" + ) + ); + } + @Test public void testHandleDefaultValue_issue8535() throws Exception { Map additionalProperties = new HashMap<>(); diff --git a/modules/openapi-generator/src/test/resources/3_0/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/petstore.yaml index 0971c554414a..a8f9809a1249 100644 --- a/modules/openapi-generator/src/test/resources/3_0/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/petstore.yaml @@ -49,6 +49,9 @@ paths: summary: Update an existing pet description: '' operationId: updatePet + externalDocs: + url: "http://petstore.swagger.io/v2/doc/updatePet" + description: "API documentation for the updatePet operation" responses: '200': description: successful operation