diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java index 117f61227..a5337d74f 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java @@ -414,6 +414,11 @@ public final class Constants { */ public static final String SPRINGDOC_NULLABLE_REQUEST_PARAMETER_ENABLED = "springdoc.nullable-request-parameter-enabled"; + /** + * The constant SPRINGDOC_PATHS_WITH_PARAMS_CONDITIONS. + */ + public static final String SPRINGDOC_PATHS_WITH_PARAMS_CONDITIONS = "springdoc.paths-with-params-condition"; + /** * Instantiates a new Constants. */ diff --git a/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/configuration/SpringDocWebMvcConfiguration.java b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/configuration/SpringDocWebMvcConfiguration.java index 98e41d328..2b9354f2d 100644 --- a/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/configuration/SpringDocWebMvcConfiguration.java +++ b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/configuration/SpringDocWebMvcConfiguration.java @@ -49,6 +49,7 @@ import org.springdoc.webmvc.core.providers.ActuatorWebMvcProvider; import org.springdoc.webmvc.core.providers.RouterFunctionWebMvcProvider; import org.springdoc.webmvc.core.providers.SpringWebMvcProvider; +import org.springdoc.webmvc.core.providers.SpringWebMvcWithParamsConditionProvider; import org.springdoc.webmvc.core.service.RequestService; import org.springframework.beans.factory.ObjectFactory; @@ -73,6 +74,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import static org.springdoc.core.utils.Constants.SPRINGDOC_ENABLED; +import static org.springdoc.core.utils.Constants.SPRINGDOC_PATHS_WITH_PARAMS_CONDITIONS; import static org.springdoc.core.utils.SpringDocUtils.getConfig; /** @@ -142,11 +144,26 @@ RequestService requestBuilder(GenericParameterService parameterBuilder, RequestB */ @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(name = SPRINGDOC_PATHS_WITH_PARAMS_CONDITIONS, havingValue = "false", matchIfMissing = true) @Lazy(false) SpringWebProvider springWebProvider() { return new SpringWebMvcProvider(); } + /** + * Spring web provider. + * ActivePatterns will be specifically classified according to the params condition. + * + * @return the spring web provider according to the params condition. + */ + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty(name = SPRINGDOC_PATHS_WITH_PARAMS_CONDITIONS, havingValue = "true") + @Lazy(false) + SpringWebProvider springWebMvcWithParamsConditionProvider() { + return new SpringWebMvcWithParamsConditionProvider(); + } + /** * Response builder generic response builder. * diff --git a/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcWithParamsConditionProvider.java b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcWithParamsConditionProvider.java new file mode 100644 index 000000000..43979aef7 --- /dev/null +++ b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcWithParamsConditionProvider.java @@ -0,0 +1,42 @@ +package org.springdoc.webmvc.core.providers; + +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.web.servlet.mvc.condition.NameValueExpression; +import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; + +/** + * The type Spring web mvc provider with params condition. + * + * @author Jelicho + */ +public class SpringWebMvcWithParamsConditionProvider extends SpringWebMvcProvider { + @Override + public Set getActivePatterns(Object requestMapping) { + Set activePatterns = super.getActivePatterns(requestMapping); + RequestMappingInfo requestMappingInfo = (RequestMappingInfo) requestMapping; + String queryStringByParamsCondition = getQueryStringByParamsCondition(requestMappingInfo.getParamsCondition()); + + if (queryStringByParamsCondition.isEmpty()) { + return activePatterns; + } + + return activePatterns.stream() + .map(activePattern -> activePattern + "?" + queryStringByParamsCondition) + .collect(Collectors.toSet()); + } + + private String getQueryStringByParamsCondition(ParamsRequestCondition paramsRequestCondition) { + return paramsRequestCondition + .getExpressions() + .stream() + .map(this::mapExpression) + .collect(Collectors.joining("&")); + } + + private String mapExpression(NameValueExpression expression) { + return expression.getName() + "=" + (expression.getValue() != null ? expression.getValue() : ""); + } +}