Skip to content

Commit

Permalink
Add config option for OpenAPI Auto Operation
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Nov 4, 2024
1 parent 0fbfc31 commit 761c5a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private OASFilter getOperationFilter(OpenApiFilteredIndexViewBuildItem indexView
Map<String, List<String>> rolesAllowedMethods = Collections.emptyMap();
List<String> authenticatedMethods = Collections.emptyList();

if (config.autoAddTags) {
if (config.autoAddTags || config.autoAddOperationSummary) {
classNamesMethods = getClassNamesMethodReferences(indexViewBuildItem);
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ public class OperationFilter implements OASFilter {

public static final String EXT_METHOD_REF = "x-quarkus-openapi-method-ref";

private Map<String, ClassAndMethod> classNameMap;
private Map<String, List<String>> rolesAllowedMethodReferences;
private List<String> authenticatedMethodReferences;
private String defaultSecuritySchemeName;
private final Map<String, ClassAndMethod> classNameMap;
private final Map<String, List<String>> rolesAllowedMethodReferences;
private final List<String> authenticatedMethodReferences;
private final String defaultSecuritySchemeName;
private final boolean doAutoTag;
private final boolean doAutoOperation;

public OperationFilter(Map<String, ClassAndMethod> classNameMap,
Map<String, List<String>> rolesAllowedMethodReferences,
List<String> 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
Expand All @@ -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);
}
Expand All @@ -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()));
}
}
Expand Down

0 comments on commit 761c5a3

Please sign in to comment.