use of org.springdoc.core.MethodAttributes in project springdoc-openapi by springdoc.
the class AbstractOpenApiResource method calculatePath.
/**
* Calculate path.
*
* @param handlerMethod the handler method
* @param routerOperation the router operation
* @param locale the locale
*/
protected void calculatePath(HandlerMethod handlerMethod, RouterOperation routerOperation, Locale locale) {
String operationPath = routerOperation.getPath();
Set<RequestMethod> requestMethods = new HashSet<>(Arrays.asList(routerOperation.getMethods()));
io.swagger.v3.oas.annotations.Operation apiOperation = routerOperation.getOperation();
String[] methodConsumes = routerOperation.getConsumes();
String[] methodProduces = routerOperation.getProduces();
String[] headers = routerOperation.getHeaders();
Map<String, String> queryParams = routerOperation.getQueryParams();
OpenAPI openAPI = openAPIService.getCalculatedOpenAPI();
Components components = openAPI.getComponents();
Paths paths = openAPI.getPaths();
Map<HttpMethod, Operation> operationMap = null;
if (paths.containsKey(operationPath)) {
PathItem pathItem = paths.get(operationPath);
operationMap = pathItem.readOperationsMap();
}
JavadocProvider javadocProvider = operationParser.getJavadocProvider();
for (RequestMethod requestMethod : requestMethods) {
Operation existingOperation = getExistingOperation(operationMap, requestMethod);
Method method = handlerMethod.getMethod();
// skip hidden operations
if (operationParser.isHidden(method))
continue;
RequestMapping reqMappingClass = AnnotatedElementUtils.findMergedAnnotation(handlerMethod.getBeanType(), RequestMapping.class);
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), methodConsumes, methodProduces, headers, locale);
methodAttributes.setMethodOverloaded(existingOperation != null);
// Use the javadoc return if present
if (javadocProvider != null) {
methodAttributes.setJavadocReturn(javadocProvider.getMethodJavadocReturn(handlerMethod.getMethod()));
}
if (reqMappingClass != null) {
methodAttributes.setClassConsumes(reqMappingClass.consumes());
methodAttributes.setClassProduces(reqMappingClass.produces());
}
methodAttributes.calculateHeadersForClass(method.getDeclaringClass());
methodAttributes.calculateConsumesProduces(method);
Operation operation = (existingOperation != null) ? existingOperation : new Operation();
if (isDeprecated(method))
operation.setDeprecated(true);
// Add documentation from operation annotation
if (apiOperation == null || StringUtils.isBlank(apiOperation.operationId()))
apiOperation = AnnotatedElementUtils.findMergedAnnotation(method, io.swagger.v3.oas.annotations.Operation.class);
calculateJsonView(apiOperation, methodAttributes, method);
if (apiOperation != null)
openAPI = operationParser.parse(apiOperation, operation, openAPI, methodAttributes);
fillParametersList(operation, queryParams, methodAttributes);
// compute tags
operation = openAPIService.buildTags(handlerMethod, operation, openAPI, locale);
io.swagger.v3.oas.annotations.parameters.RequestBody requestBodyDoc = AnnotatedElementUtils.findMergedAnnotation(method, io.swagger.v3.oas.annotations.parameters.RequestBody.class);
// RequestBody in Operation
requestBuilder.getRequestBodyBuilder().buildRequestBodyFromDoc(requestBodyDoc, methodAttributes, components, methodAttributes.getJsonViewAnnotationForRequestBody()).ifPresent(operation::setRequestBody);
// requests
operation = requestBuilder.build(handlerMethod, requestMethod, operation, methodAttributes, openAPI);
// responses
ApiResponses apiResponses = responseBuilder.build(components, handlerMethod, operation, methodAttributes);
operation.setResponses(apiResponses);
// get javadoc method description
if (javadocProvider != null) {
String description = javadocProvider.getMethodJavadocDescription(handlerMethod.getMethod());
if (!StringUtils.isEmpty(description) && StringUtils.isEmpty(operation.getDescription()))
operation.setDescription(description);
}
Set<io.swagger.v3.oas.annotations.callbacks.Callback> apiCallbacks = AnnotatedElementUtils.findMergedRepeatableAnnotations(method, io.swagger.v3.oas.annotations.callbacks.Callback.class);
// callbacks
buildCallbacks(openAPI, methodAttributes, operation, apiCallbacks);
// allow for customisation
operation = customiseOperation(operation, handlerMethod);
PathItem pathItemObject = buildPathItem(requestMethod, operation, operationPath, paths);
paths.addPathItem(operationPath, pathItemObject);
}
}
use of org.springdoc.core.MethodAttributes in project springdoc-openapi by springdoc.
the class DataRestRouterOperationService method buildRouterOperation.
/**
* Build router operation.
*
* @param routerOperationList the router operation list
* @param dataRestRepository the repository data rest
* @param openAPI the open api
* @param methodResourceMapping the method resource mapping
* @param handlerMethod the handler method
* @param requestMethod the request method
* @param resourceMetadata the resource metadata
* @param operationPath the operation path
* @param controllerType the controller type
*/
private void buildRouterOperation(List<RouterOperation> routerOperationList, DataRestRepository dataRestRepository, OpenAPI openAPI, MethodResourceMapping methodResourceMapping, HandlerMethod handlerMethod, RequestMethod requestMethod, ResourceMetadata resourceMetadata, String operationPath, ControllerType controllerType) {
RouterOperation routerOperation = new RouterOperation(operationPath, new RequestMethod[] { requestMethod });
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), dataRestRepository.getLocale());
methodAttributes.calculateConsumesProduces(handlerMethod.getMethod());
routerOperation.setConsumes(methodAttributes.getMethodConsumes());
routerOperation.setProduces(methodAttributes.getMethodProduces());
Operation operation = dataRestOperationService.buildOperation(handlerMethod, dataRestRepository, openAPI, requestMethod, operationPath, methodAttributes, resourceMetadata, methodResourceMapping, controllerType);
routerOperation.setOperationModel(operation);
routerOperationList.add(routerOperation);
}
use of org.springdoc.core.MethodAttributes in project springdoc-openapi by springdoc.
the class DataRestRouterOperationService method isSearchControllerPresent.
/**
* Is search controller present boolean.
*
* @param requestMappingInfo the request mapping info
* @param handlerMethod the handler method
* @param requestMethod the request method
* @param locale the locale
* @return the boolean
*/
private boolean isSearchControllerPresent(RequestMappingInfo requestMappingInfo, HandlerMethod handlerMethod, RequestMethod requestMethod, Locale locale) {
if (!UNDOCUMENTED_REQUEST_METHODS.contains(requestMethod)) {
Set<String> patterns = getActivePatterns(requestMappingInfo);
if (!CollectionUtils.isEmpty(patterns)) {
Map<String, String> regexMap = new LinkedHashMap<>();
String operationPath;
for (String pattern : patterns) {
operationPath = PathUtils.parsePath(pattern, regexMap);
if (andCheck(operationPath.contains(REPOSITORY_PATH), operationPath.contains(SEARCH_PATH))) {
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), locale);
methodAttributes.calculateConsumesProduces(handlerMethod.getMethod());
if (springDocConfigProperties.getDefaultProducesMediaType().equals(methodAttributes.getMethodProduces()[0]))
return true;
}
}
}
}
return false;
}
use of org.springdoc.core.MethodAttributes in project springdoc-openapi by springdoc.
the class AbstractOpenApiResource method calculatePath.
/**
* Calculate path.
*
* @param routerOperation the router operation
* @param locale the locale
*/
protected void calculatePath(RouterOperation routerOperation, Locale locale) {
String operationPath = routerOperation.getPath();
io.swagger.v3.oas.annotations.Operation apiOperation = routerOperation.getOperation();
String[] methodConsumes = routerOperation.getConsumes();
String[] methodProduces = routerOperation.getProduces();
String[] headers = routerOperation.getHeaders();
Map<String, String> queryParams = routerOperation.getQueryParams();
OpenAPI openAPI = openAPIService.getCalculatedOpenAPI();
Paths paths = openAPI.getPaths();
Map<HttpMethod, Operation> operationMap = null;
if (paths.containsKey(operationPath)) {
PathItem pathItem = paths.get(operationPath);
operationMap = pathItem.readOperationsMap();
}
for (RequestMethod requestMethod : routerOperation.getMethods()) {
Operation existingOperation = getExistingOperation(operationMap, requestMethod);
MethodAttributes methodAttributes = new MethodAttributes(springDocConfigProperties.getDefaultConsumesMediaType(), springDocConfigProperties.getDefaultProducesMediaType(), methodConsumes, methodProduces, headers, locale);
methodAttributes.setMethodOverloaded(existingOperation != null);
Operation operation = getOperation(routerOperation, existingOperation);
if (apiOperation != null)
openAPI = operationParser.parse(apiOperation, operation, openAPI, methodAttributes);
String operationId = operationParser.getOperationId(operation.getOperationId(), openAPI);
operation.setOperationId(operationId);
fillParametersList(operation, queryParams, methodAttributes);
if (!CollectionUtils.isEmpty(operation.getParameters()))
operation.getParameters().stream().filter(parameter -> StringUtils.isEmpty(parameter.get$ref())).forEach(parameter -> {
if (parameter.getSchema() == null)
parameter.setSchema(new StringSchema());
if (parameter.getIn() == null)
parameter.setIn(ParameterIn.QUERY.toString());
});
PathItem pathItemObject = buildPathItem(requestMethod, operation, operationPath, paths);
paths.addPathItem(operationPath, pathItemObject);
}
}
use of org.springdoc.core.MethodAttributes in project springdoc-openapi by springdoc.
the class AbstractOpenApiResource method calculateJsonView.
/**
* Calculate json view.
*
* @param apiOperation the api operation
* @param methodAttributes the method attributes
* @param method the method
*/
private void calculateJsonView(io.swagger.v3.oas.annotations.Operation apiOperation, MethodAttributes methodAttributes, Method method) {
JsonView jsonViewAnnotation;
JsonView jsonViewAnnotationForRequestBody;
if (apiOperation != null && apiOperation.ignoreJsonView()) {
jsonViewAnnotation = null;
jsonViewAnnotationForRequestBody = null;
} else {
jsonViewAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, JsonView.class);
/*
* If one and only one exists, use the @JsonView annotation from the method
* parameter annotated with @RequestBody. Otherwise fall back to the @JsonView
* annotation for the method itself.
*/
jsonViewAnnotationForRequestBody = (JsonView) Arrays.stream(ReflectionUtils.getParameterAnnotations(method)).filter(arr -> Arrays.stream(arr).anyMatch(annotation -> (annotation.annotationType().equals(io.swagger.v3.oas.annotations.parameters.RequestBody.class) || annotation.annotationType().equals(RequestBody.class)))).flatMap(Arrays::stream).filter(annotation -> annotation.annotationType().equals(JsonView.class)).reduce((a, b) -> null).orElse(jsonViewAnnotation);
}
methodAttributes.setJsonViewAnnotation(jsonViewAnnotation);
methodAttributes.setJsonViewAnnotationForRequestBody(jsonViewAnnotationForRequestBody);
}
Aggregations