use of cz.habarta.typescript.generator.parser.RestMethodModel in project typescript-generator by vojtechhabarta.
the class ModelCompiler method processRestMethodGroup.
private List<TsMethodModel> processRestMethodGroup(TsModel tsModel, SymbolTable symbolTable, List<Pair<RestApplicationModel, RestMethodModel>> methods, Symbol responseSymbol, TsType optionsType, boolean implement) {
final List<TsMethodModel> resultMethods = new ArrayList<>();
final Map<String, Long> methodNamesCount = groupingByMethodName(methods);
for (Pair<RestApplicationModel, RestMethodModel> pair : methods) {
final RestApplicationModel restApplication = pair.getValue1();
final RestMethodModel method = pair.getValue2();
final boolean createLongName = methodNamesCount.get(method.getName()) > 1;
resultMethods.add(processRestMethod(tsModel, symbolTable, restApplication.getApplicationPath(), responseSymbol, method, createLongName, optionsType, implement));
}
return resultMethods;
}
use of cz.habarta.typescript.generator.parser.RestMethodModel 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