Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paths with params condition option #2761

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> getActivePatterns(Object requestMapping) {
Set<String> 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<String> expression) {
return expression.getName() + "=" + (expression.getValue() != null ? expression.getValue() : "");
}
}