Skip to content

Commit

Permalink
OpenAPITools#14141 Add externalDocs to @operation to the JavaSpring g…
Browse files Browse the repository at this point in the history
…enerator
  • Loading branch information
arey committed Dec 5, 2022
1 parent 4e387ca commit 468e5da
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class CodegenOperation {
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, File> 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<String, Object> additionalProperties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 468e5da

Please sign in to comment.