Skip to content

Fixed AbstractRequestService to modify parameters by the return value #1372

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -292,7 +292,7 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
applyBeanValidatorAnnotations(requestBodyInfo.getRequestBody(), parameterAnnotations, methodParameter.isOptional());
}

customiseParameter(parameter, parameterInfo);
GenericParameterService.replaceParameter(operationParameters, parameter, customiseParameter(parameter, parameterInfo));
}
}

Expand Down Expand Up @@ -366,9 +366,16 @@ public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes
* @param parameterInfo the parameter info
* @return the parameter
*/
protected Parameter customiseParameter(Parameter parameter, ParameterInfo parameterInfo) {
parameterCustomizers.ifPresent(customizers -> customizers.forEach(customizer -> customizer.customize(parameter, parameterInfo.getMethodParameter())));
return parameter;
protected Parameter customiseParameter(final Parameter parameter, final ParameterInfo parameterInfo) {
return parameterCustomizers
.map(customizers -> {
Parameter customizedParameter = parameter;
for (final ParameterCustomizer customizer : customizers) {
customizedParameter = customizer.customize(customizedParameter, parameterInfo.getMethodParameter());
}
return customizedParameter;
})
.orElse(parameter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ public static boolean isFile(Class type) {
return FILE_TYPES.stream().anyMatch(clazz -> clazz.isAssignableFrom(type));
}

/**
* Replace existingParamDoc. The argument named asis will be replaced to the argument named tobe.
* @param existingParamDoc the existing param doc
* @param asis the parameter. If null, the do nothing. otherwise, replace to tobe.
* @param tobe the parameter. If null, just remove asis.
*/
public static void replaceParameter(List<Parameter> existingParamDoc, Parameter asis, Parameter tobe) {
int index = -1;
if (asis != null) {
index = existingParamDoc.stream()
.filter(param -> asis.getName().equals(param.getName()))
.findFirst()
.map(existingParamDoc::indexOf)
.orElse(-1);
}
if (index == -1) {
return;
}

if (tobe == null) {
existingParamDoc.remove(index);
} else {
existingParamDoc.set(index, tobe);
}
}

/**
* Merge parameter parameter.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test.org.springdoc.api.app172;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import test.org.springdoc.api.app172.annotation.MyIdPathVariable;
import test.org.springdoc.api.app172.model.MyObj;

@RestController
public class HelloController {
@GetMapping("/test/{objId}")
String test(@MyIdPathVariable MyObj obj) {
return obj.getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test.org.springdoc.api.app172;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfiguration implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new MyObjArgumentResolver());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test.org.springdoc.api.app172;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import test.org.springdoc.api.app172.annotation.MyIdPathVariable;
import test.org.springdoc.api.app172.model.MyObj;

public class MyObjArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(MyIdPathVariable.class) &&
MyObj.class.isAssignableFrom(parameter.getParameterType());
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
return new MyObj("id", "content");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.org.springdoc.api.app172;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import test.org.springdoc.api.AbstractSpringDocTest;

public class SpringDocApp172Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test.org.springdoc.api.app172.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface MyIdPathVariable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.org.springdoc.api.app172.customizer;

import org.springdoc.core.customizers.ParameterCustomizer;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;

import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.PathParameter;
import test.org.springdoc.api.app172.annotation.MyIdPathVariable;

@Component
public class MyPathParameterCustomizer implements ParameterCustomizer {
@Override
public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
if (methodParameter.hasParameterAnnotation(MyIdPathVariable.class)) {
Parameter alternativeParameter = new PathParameter();
alternativeParameter.setName("objId");
alternativeParameter.setSchema(new StringSchema());
return alternativeParameter;
}
return parameterModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test.org.springdoc.api.app172.model;

public class MyObj {
private final String id;
private final String content;

public MyObj(String id, String content) {
this.id = id;
this.content = content;
}

public String getId() {
return id;
}

public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/test/{objId}": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "test",
"parameters": [
{
"name": "objId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {}
}
}