From 761c5a30c57bacd353b4b2044f6cd0a24a7ac091 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Mon, 4 Nov 2024 12:33:32 +1100 Subject: [PATCH] Add config option for OpenAPI Auto Operation Signed-off-by: Phillip Kruger --- .../deployment/SmallRyeOpenApiConfig.java | 6 +++++ .../deployment/SmallRyeOpenApiProcessor.java | 5 ++-- .../deployment/filter/OperationFilter.java | 27 +++++++++++-------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/extensions/smallrye-openapi-common/deployment/src/main/java/io/quarkus/smallrye/openapi/common/deployment/SmallRyeOpenApiConfig.java b/extensions/smallrye-openapi-common/deployment/src/main/java/io/quarkus/smallrye/openapi/common/deployment/SmallRyeOpenApiConfig.java index 7e47a5188ae54..a1e4eec66c6d3 100644 --- a/extensions/smallrye-openapi-common/deployment/src/main/java/io/quarkus/smallrye/openapi/common/deployment/SmallRyeOpenApiConfig.java +++ b/extensions/smallrye-openapi-common/deployment/src/main/java/io/quarkus/smallrye/openapi/common/deployment/SmallRyeOpenApiConfig.java @@ -89,6 +89,12 @@ public final class SmallRyeOpenApiConfig { @ConfigItem(defaultValue = "true") public boolean autoAddTags; + /** + * This will automatically add a summary to operations based on the Java method name. + */ + @ConfigItem(defaultValue = "true") + public boolean autoAddOperationSummary; + /** * Setting it to `true` will automatically add a default server to the schema if none is provided, * using the current running server host and port. diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index c8f0178cb4218..bdb53b6849f8b 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -544,7 +544,7 @@ private OASFilter getOperationFilter(OpenApiFilteredIndexViewBuildItem indexView Map> rolesAllowedMethods = Collections.emptyMap(); List authenticatedMethods = Collections.emptyList(); - if (config.autoAddTags) { + if (config.autoAddTags || config.autoAddOperationSummary) { classNamesMethods = getClassNamesMethodReferences(indexViewBuildItem); } @@ -559,7 +559,8 @@ private OASFilter getOperationFilter(OpenApiFilteredIndexViewBuildItem indexView } if (!classNamesMethods.isEmpty() || !rolesAllowedMethods.isEmpty() || !authenticatedMethods.isEmpty()) { - return new OperationFilter(classNamesMethods, rolesAllowedMethods, authenticatedMethods, config.securitySchemeName); + return new OperationFilter(classNamesMethods, rolesAllowedMethods, authenticatedMethods, config.securitySchemeName, + config.autoAddTags, config.autoAddOperationSummary); } return null; diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/filter/OperationFilter.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/filter/OperationFilter.java index 82c7483621c6d..f33c0e56460bd 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/filter/OperationFilter.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/filter/OperationFilter.java @@ -36,20 +36,25 @@ public class OperationFilter implements OASFilter { public static final String EXT_METHOD_REF = "x-quarkus-openapi-method-ref"; - private Map classNameMap; - private Map> rolesAllowedMethodReferences; - private List authenticatedMethodReferences; - private String defaultSecuritySchemeName; + private final Map classNameMap; + private final Map> rolesAllowedMethodReferences; + private final List authenticatedMethodReferences; + private final String defaultSecuritySchemeName; + private final boolean doAutoTag; + private final boolean doAutoOperation; public OperationFilter(Map classNameMap, Map> rolesAllowedMethodReferences, List authenticatedMethodReferences, - String defaultSecuritySchemeName) { + String defaultSecuritySchemeName, + boolean doAutoTag, boolean doAutoOperation) { this.classNameMap = Objects.requireNonNull(classNameMap); this.rolesAllowedMethodReferences = Objects.requireNonNull(rolesAllowedMethodReferences); this.authenticatedMethodReferences = Objects.requireNonNull(authenticatedMethodReferences); this.defaultSecuritySchemeName = Objects.requireNonNull(defaultSecuritySchemeName); + this.doAutoTag = doAutoTag; + this.doAutoOperation = doAutoOperation; } @Override @@ -76,7 +81,7 @@ public void filterOpenAPI(OpenAPI openAPI) { final String methodRef = methodRef(operation); if (methodRef != null) { - maybeSetDescriptionAndTag(operation, methodRef); + maybeSetSummaryAndTag(operation, methodRef); maybeAddSecurityRequirement(operation, methodRef, schemeName, scopesValidForScheme, defaultSecurityErrors); } @@ -90,19 +95,19 @@ private String methodRef(Operation operation) { return (String) (extensions != null ? extensions.get(EXT_METHOD_REF) : null); } - private void maybeSetDescriptionAndTag(Operation operation, String methodRef) { + private void maybeSetSummaryAndTag(Operation operation, String methodRef) { if (!classNameMap.containsKey(methodRef)) { return; } ClassAndMethod classMethod = classNameMap.get(methodRef); - if (operation.getDescription() == null || operation.getDescription().isBlank()) { - // Auto add a description - operation.setDescription(capitalizeFirstLetter(splitCamelCase(classMethod.methodName()))); + if (doAutoOperation && operation.getSummary() == null) { + // Auto add a summary + operation.setSummary(capitalizeFirstLetter(splitCamelCase(classMethod.methodName()))); } - if (operation.getTags() == null || operation.getTags().isEmpty()) { + if (doAutoTag && (operation.getTags() == null || operation.getTags().isEmpty())) { operation.addTag(splitCamelCase(classMethod.className())); } }