use of cz.habarta.typescript.generator.parser.Swagger in project typescript-generator by vojtechhabarta.
the class SpringApplicationParser method parseControllerMethod.
// https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-methods
private void parseControllerMethod(JaxrsApplicationParser.Result result, JaxrsApplicationParser.ResourceContext context, Class<?> controllerClass, Method method) {
final RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (requestMapping != null) {
// swagger
final SwaggerOperation swaggerOperation = settings.ignoreSwaggerAnnotations ? new SwaggerOperation() : Swagger.parseSwaggerAnnotations(method);
if (swaggerOperation.possibleResponses != null) {
for (SwaggerResponse response : swaggerOperation.possibleResponses) {
if (response.responseType != null) {
foundType(result, response.responseType, controllerClass, method.getName());
}
}
}
if (swaggerOperation.hidden) {
return;
}
// subContext
context = context.subPath(requestMapping.path().length == 0 ? "" : requestMapping.path()[0]);
final Map<String, Type> pathParamTypes = new LinkedHashMap<>();
for (Parameter parameter : method.getParameters()) {
final PathVariable pathVariableAnnotation = AnnotationUtils.findAnnotation(parameter, PathVariable.class);
if (pathVariableAnnotation != null) {
String pathVariableName = pathVariableAnnotation.value();
// Can be empty if the URI template variable matches the method argument
if (pathVariableName.isEmpty()) {
pathVariableName = parameter.getName();
}
pathParamTypes.put(pathVariableName, parameter.getParameterizedType());
}
}
context = context.subPathParamTypes(pathParamTypes);
final RequestMethod httpMethod = requestMapping.method().length == 0 ? RequestMethod.GET : requestMapping.method()[0];
// path parameters
final PathTemplate pathTemplate = PathTemplate.parse(context.path);
final Map<String, Type> contextPathParamTypes = context.pathParamTypes;
final List<MethodParameterModel> pathParams = pathTemplate.getParts().stream().filter(PathTemplate.Parameter.class::isInstance).map(PathTemplate.Parameter.class::cast).map(parameter -> {
final Type type = contextPathParamTypes.get(parameter.getOriginalName());
final Type paramType = type != null ? type : String.class;
foundType(result, paramType, controllerClass, method.getName());
return new MethodParameterModel(parameter.getValidName(), paramType);
}).collect(Collectors.toList());
// query parameters
final List<RestQueryParam> queryParams = new ArrayList<>();
for (Parameter parameter : method.getParameters()) {
if (parameter.getType() == Pageable.class) {
queryParams.add(new RestQueryParam.Single(new MethodParameterModel("page", Long.class), false));
queryParams.add(new RestQueryParam.Single(new MethodParameterModel("size", Long.class), false));
queryParams.add(new RestQueryParam.Single(new MethodParameterModel("sort", String.class), false));
} else {
final RequestParam requestParamAnnotation = AnnotationUtils.findAnnotation(parameter, RequestParam.class);
if (requestParamAnnotation != null) {
if (parameter.getType() == MultiValueMap.class) {
queryParams.add(new RestQueryParam.Map(false));
} else {
final boolean isRequired = requestParamAnnotation.required() && requestParamAnnotation.defaultValue().equals(ValueConstants.DEFAULT_NONE);
queryParams.add(new RestQueryParam.Single(new MethodParameterModel(firstOf(requestParamAnnotation.value(), parameter.getName()), parameter.getParameterizedType()), isRequired));
foundType(result, parameter.getParameterizedType(), controllerClass, method.getName());
}
}
final ModelAttribute modelAttributeAnnotation = AnnotationUtils.findAnnotation(parameter, ModelAttribute.class);
if (modelAttributeAnnotation != null) {
try {
final BeanInfo beanInfo = Introspector.getBeanInfo(parameter.getType());
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
final Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null) {
queryParams.add(new RestQueryParam.Single(new MethodParameterModel(propertyDescriptor.getName(), propertyDescriptor.getPropertyType()), false));
foundType(result, propertyDescriptor.getPropertyType(), controllerClass, method.getName());
}
}
} catch (IntrospectionException e) {
TypeScriptGenerator.getLogger().warning(String.format("Cannot introspect '%s' class: " + e.getMessage(), parameter.getAnnotatedType()));
}
}
}
}
// entity parameter
final MethodParameterModel entityParameter = getEntityParameter(controllerClass, method);
if (entityParameter != null) {
foundType(result, entityParameter.getType(), controllerClass, method.getName());
}
final Type modelReturnType = parseReturnType(controllerClass, method);
foundType(result, modelReturnType, controllerClass, method.getName());
model.getMethods().add(new RestMethodModel(controllerClass, method.getName(), modelReturnType, method, controllerClass, httpMethod.name(), context.path, pathParams, queryParams, entityParameter, null));
}
}
Aggregations