Created
November 23, 2017 13:23
-
-
Save andrea-sdl/ef03c3753460e1438134ce8becacabaf to your computer and use it in GitHub Desktop.
ParameterTypeReader fix for RequestBody ModelAttribute springfox issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Copyright 2015 the original author or authors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
* | |
*/ | |
package springfox.documentation.spring.web.readers.parameter; | |
import com.fasterxml.classmate.ResolvedType; | |
import org.springframework.core.Ordered; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.MediaType; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.bind.annotation.ModelAttribute; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestHeader; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RequestPart; | |
import org.springframework.web.multipart.MultipartFile; | |
import springfox.documentation.service.ResolvedMethodParameter; | |
import springfox.documentation.spi.DocumentationType; | |
import springfox.documentation.spi.service.ParameterBuilderPlugin; | |
import springfox.documentation.spi.service.contexts.OperationContext; | |
import springfox.documentation.spi.service.contexts.ParameterContext; | |
@Component | |
@Order(Ordered.HIGHEST_PRECEDENCE) | |
public class ParameterTypeReader implements ParameterBuilderPlugin { | |
@Override | |
public void apply(ParameterContext context) { | |
context.parameterBuilder().parameterType(findParameterType(context)); | |
} | |
@Override | |
public boolean supports(DocumentationType delimiter) { | |
return true; | |
} | |
public static String findParameterType(ParameterContext parameterContext) { | |
ResolvedMethodParameter resolvedMethodParameter = parameterContext.resolvedMethodParameter(); | |
ResolvedType parameterType = resolvedMethodParameter.getParameterType(); | |
parameterType = parameterContext.alternateFor(parameterType); | |
//Multi-part file trumps any other annotations | |
if (MultipartFile.class.isAssignableFrom(parameterType.getErasedType())) { | |
return "form"; | |
} | |
if (resolvedMethodParameter.hasParameterAnnotation(PathVariable.class)) { | |
return "path"; | |
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestBody.class)) { | |
return "body"; | |
} else if (resolvedMethodParameter.hasParameterAnnotation(ModelAttribute.class)) { | |
return queryOrForm(parameterContext.getOperationContext()); | |
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestParam.class)) { | |
return queryOrForm(parameterContext.getOperationContext()); | |
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestHeader.class)) { | |
return "header"; | |
} else if (resolvedMethodParameter.hasParameterAnnotation(RequestPart.class)) { | |
return "form"; | |
} | |
if (!resolvedMethodParameter.hasParameterAnnotations()) { | |
return queryOrForm(parameterContext.getOperationContext()); | |
} | |
return "body"; | |
} | |
private static String queryOrForm(OperationContext context) { | |
if (context.consumes().contains(MediaType.APPLICATION_FORM_URLENCODED) && context.httpMethod() == HttpMethod | |
.POST) { | |
return "form"; | |
} | |
return "query"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment