use of org.springdoc.core.fn.RouterOperation in project springdoc-openapi by springdoc.
the class SpringCloudFunctionProvider method buildRouterOperation.
/**
* Build router operation router operation.
*
* @param name the name
* @param type the type
* @param requestMethod the request method
* @param routerOperationList the router operation list
* @return the router operation
*/
private RouterOperation buildRouterOperation(String name, String type, RequestMethod requestMethod, List<RouterOperation> routerOperationList) {
Operation operation = new Operation().operationId(name + "_" + requestMethod);
RouterOperation routerOperation = new RouterOperation();
routerOperation.setConsumes(defaultMediaTypes);
routerOperation.setProduces(defaultMediaTypes);
routerOperation.setMethods(new RequestMethod[] { requestMethod });
operation.description(name + type);
routerOperation.setOperationModel(operation);
routerOperationList.add(routerOperation);
return routerOperation;
}
use of org.springdoc.core.fn.RouterOperation in project springdoc-openapi by springdoc.
the class SpringCloudFunctionProvider method getRouterOperations.
@Override
public List<RouterOperation> getRouterOperations(OpenAPI openAPI) {
List<RouterOperation> routerOperationList = new ArrayList<>();
functionCatalogOptional.ifPresent(functionCatalog -> {
Set<String> names = functionCatalog.getNames(null);
for (String name : names) {
if (!RoutingFunction.FUNCTION_NAME.equals(name)) {
FunctionInvocationWrapper function = functionCatalog.lookup(name);
if (function.isFunction()) {
for (RequestMethod requestMethod : functionRequestMethods) {
RouterOperation routerOperation = buildRouterOperation(name, " function", requestMethod, routerOperationList);
buildRequest(openAPI, name, function, requestMethod, routerOperation);
ApiResponses apiResponses = buildResponses(openAPI, function, defaultMediaTypes);
routerOperation.getOperationModel().responses(apiResponses);
if (StringUtils.isEmpty(prefix)) {
if (GET.equals(requestMethod))
routerOperation.setPath(AntPathMatcher.DEFAULT_PATH_SEPARATOR + name + AntPathMatcher.DEFAULT_PATH_SEPARATOR + "{" + name + "}");
else
routerOperation.setPath(AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
} else {
if (GET.equals(requestMethod))
routerOperation.setPath(prefix + AntPathMatcher.DEFAULT_PATH_SEPARATOR + name + AntPathMatcher.DEFAULT_PATH_SEPARATOR + "{" + name + "}");
else
routerOperation.setPath(prefix + AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
}
RouterOperation userRouterOperation = this.getRouterFunctionPaths(name, requestMethod);
if (userRouterOperation != null)
mergeRouterOperation(routerOperation, userRouterOperation);
}
} else if (function.isConsumer()) {
for (RequestMethod requestMethod : consumerRequestMethods) {
RouterOperation routerOperation = buildRouterOperation(name, " consumer", requestMethod, routerOperationList);
buildRequest(openAPI, name, function, requestMethod, routerOperation);
ApiResponses apiResponses = new ApiResponses();
ApiResponse apiResponse = new ApiResponse();
apiResponse.setContent(new Content());
apiResponses.put(String.valueOf(HttpStatus.ACCEPTED.value()), apiResponse.description(HttpStatus.ACCEPTED.getReasonPhrase()));
routerOperation.getOperationModel().responses(apiResponses);
if (StringUtils.isEmpty(prefix))
routerOperation.setPath(AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
else
routerOperation.setPath(prefix + AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
RouterOperation userRouterOperation = this.getRouterFunctionPaths(name, requestMethod);
if (userRouterOperation != null)
mergeRouterOperation(routerOperation, userRouterOperation);
}
} else if (function.isSupplier()) {
for (RequestMethod requestMethod : supplierRequestMethods) {
RouterOperation routerOperation = buildRouterOperation(name, " supplier", requestMethod, routerOperationList);
ApiResponses apiResponses = buildResponses(openAPI, function, new String[] { springDocConfigProperties.getDefaultProducesMediaType() });
routerOperation.getOperationModel().responses(apiResponses);
if (StringUtils.isEmpty(prefix))
routerOperation.setPath(AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
else
routerOperation.setPath(prefix + AntPathMatcher.DEFAULT_PATH_SEPARATOR + name);
RouterOperation userRouterOperation = this.getRouterFunctionPaths(name, requestMethod);
if (userRouterOperation != null)
mergeRouterOperation(routerOperation, userRouterOperation);
}
}
}
}
});
return routerOperationList;
}
use of org.springdoc.core.fn.RouterOperation in project springdoc-openapi by springdoc.
the class AbstractOpenApiResourceTest method calculatePathFromRouterOperation.
@Test
void calculatePathFromRouterOperation() {
resource = new EmptyPathsOpenApiResource(GROUP_NAME, openAPIBuilderObjectFactory, requestBuilder, responseBuilder, operationParser, Optional.empty(), Optional.empty(), Optional.empty(), new SpringDocConfigProperties(), springDocProviders);
final Parameter refParameter = new Parameter().$ref(PARAMETER_REFERENCE);
final Parameter numberParameterInPath = new Parameter().name(PARAMETER_WITH_NUMBER_SCHEMA_NAME).in(ParameterIn.PATH.toString()).schema(new NumberSchema());
final Parameter parameterWithoutSchema = new Parameter().name(PARAMETER_WITHOUT_SCHEMA_NAME);
final Operation operation = new Operation();
operation.setParameters(asList(refParameter, numberParameterInPath, parameterWithoutSchema));
final RouterOperation routerOperation = new RouterOperation();
routerOperation.setMethods(new RequestMethod[] { GET });
routerOperation.setOperationModel(operation);
routerOperation.setPath(PATH);
resource.calculatePath(routerOperation, Locale.getDefault());
final List<Parameter> parameters = resource.getOpenApi(Locale.getDefault()).getPaths().get(PATH).getGet().getParameters();
assertThat(parameters.size(), is(3));
assertThat(parameters, containsInAnyOrder(refParameter, numberParameterInPath, parameterWithoutSchema));
assertThat(refParameter.getName(), nullValue());
assertThat(refParameter.get$ref(), is(PARAMETER_REFERENCE));
assertThat(refParameter.getSchema(), nullValue());
assertThat(refParameter.getIn(), nullValue());
assertThat(numberParameterInPath.getName(), is(PARAMETER_WITH_NUMBER_SCHEMA_NAME));
assertThat(numberParameterInPath.get$ref(), nullValue());
assertThat(numberParameterInPath.getSchema(), is(new NumberSchema()));
assertThat(numberParameterInPath.getIn(), is(ParameterIn.PATH.toString()));
assertThat(parameterWithoutSchema.getName(), is(PARAMETER_WITHOUT_SCHEMA_NAME));
assertThat(parameterWithoutSchema.get$ref(), nullValue());
assertThat(parameterWithoutSchema.getSchema(), is(new StringSchema()));
assertThat(parameterWithoutSchema.getIn(), is(ParameterIn.QUERY.toString()));
}
use of org.springdoc.core.fn.RouterOperation in project springdoc-openapi by springdoc.
the class AbstractOpenApiResource method getRouterFunctionPaths.
/**
* Gets router function paths.
*
* @param beanName the bean name
* @param routerFunctionVisitor the router function visitor
* @param locale the locale
*/
protected void getRouterFunctionPaths(String beanName, AbstractRouterFunctionVisitor routerFunctionVisitor, Locale locale) {
boolean withRouterOperation = routerFunctionVisitor.getRouterFunctionDatas().stream().anyMatch(routerFunctionData -> routerFunctionData.getAttributes().containsKey(OPERATION_ATTRIBUTE));
if (withRouterOperation) {
List<RouterOperation> operationList = routerFunctionVisitor.getRouterFunctionDatas().stream().map(RouterOperation::new).collect(Collectors.toList());
calculatePath(operationList, locale);
} else {
List<org.springdoc.core.annotations.RouterOperation> routerOperationList = new ArrayList<>();
ApplicationContext applicationContext = openAPIService.getContext();
RouterOperations routerOperations = applicationContext.findAnnotationOnBean(beanName, RouterOperations.class);
if (routerOperations == null) {
org.springdoc.core.annotations.RouterOperation routerOperation = applicationContext.findAnnotationOnBean(beanName, org.springdoc.core.annotations.RouterOperation.class);
if (routerOperation != null)
routerOperationList.add(routerOperation);
} else
routerOperationList.addAll(Arrays.asList(routerOperations.value()));
if (routerOperationList.size() == 1)
calculatePath(routerOperationList.stream().map(routerOperation -> new RouterOperation(routerOperation, routerFunctionVisitor.getRouterFunctionDatas().get(0))).collect(Collectors.toList()), locale);
else {
List<RouterOperation> operationList = routerOperationList.stream().map(RouterOperation::new).collect(Collectors.toList());
mergeRouters(routerFunctionVisitor.getRouterFunctionDatas(), operationList);
calculatePath(operationList, locale);
}
}
}
use of org.springdoc.core.fn.RouterOperation 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);
}
}
Aggregations