Commit 73771495 authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Draft: OperationIdCustomizer

- implement OperationCustomizer to avoid generating method names not by Java naming conventions when using `swagger-codegen-maven-plugin`
parent 0d8e8f67
......@@ -17,15 +17,23 @@ package org.gringlobal.application.config;
import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import io.swagger.v3.oas.models.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.SpringDocConfiguration;
import org.springdoc.core.SwaggerUiConfigParameters;
import org.springdoc.core.SwaggerUiConfigProperties;
import org.springdoc.core.SwaggerUiOAuthProperties;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration;
import org.springdoc.webmvc.core.SpringDocWebMvcConfiguration;
import org.springdoc.webmvc.ui.SwaggerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
......@@ -46,6 +54,7 @@ import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.method.HandlerMethod;
@Configuration
@Import({ SpringDocConfigProperties.class, SpringDocConfiguration.class, SpringDocWebMvcConfiguration.class,
......@@ -104,15 +113,54 @@ public class OpenAPIConfig {
protected static class GroupsConfiguration {
@Bean
public GroupedOpenApi apiV1() {
public OperationCustomizer operationCustomizer() {
return new OperationIdCustomizer();
}
/**
* Customize `operationId` property to avoid default generating such as `update_1`, `update_2` etc.
*
* `operationId` property is required to produce method names in the generated API client.
*/
private static class OperationIdCustomizer implements OperationCustomizer {
private final Map<String, String> namesToOverride = new HashMap<>();
public OperationIdCustomizer() {
namesToOverride.put("create", "create%s");
namesToOverride.put("update", "update%s");
namesToOverride.put("get", "get%s");
namesToOverride.put("remove", "remove%s");
namesToOverride.put("list", "list%s");
namesToOverride.put("filter", "filter%s");
namesToOverride.put("startActions", "start%ss");
namesToOverride.put("reopenActions", "reopen%ss");
namesToOverride.put("completeActions", "complete%ss");
namesToOverride.put("scheduleActions", "schedule%ss");
namesToOverride.put("listActions", "list%ss");
}
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
String methodName = handlerMethod.getMethod().getName();
if (namesToOverride.get(methodName) != null && (Objects.equals(operation.getOperationId(), methodName) || StringUtils.isBlank(operation.getOperationId()))) {
// extract entity name from controller name
String entityName = handlerMethod.getBeanType().getSimpleName().replace("Controller", "");
operation.setOperationId(String.format(namesToOverride.get(methodName), entityName));
}
return operation;
}
}
@Bean
public GroupedOpenApi apiV1(@Autowired OperationCustomizer customizer) {
String[] paths = { "/api/v1/**" };
return GroupedOpenApi.builder().group("V1").pathsToMatch(paths).build();
return GroupedOpenApi.builder().group("V1").pathsToMatch(paths).addOperationCustomizer(customizer).build();
}
@Bean
public GroupedOpenApi brapiV1() {
public GroupedOpenApi brapiV1(@Autowired OperationCustomizer customizer) {
String[] paths = { "/brapi/v1/**" };
return GroupedOpenApi.builder().group("BrAPI").pathsToMatch(paths).build();
return GroupedOpenApi.builder().group("BrAPI").pathsToMatch(paths).addOperationCustomizer(customizer).build();
}
// @Bean
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment