Search in sources :

Example 1 with Swagger

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));
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) GenericsResolver(cz.habarta.typescript.generator.util.GenericsResolver) SwaggerResponse(cz.habarta.typescript.generator.parser.SwaggerResponse) SpringApplication(org.springframework.boot.SpringApplication) Map(java.util.Map) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Pageable(org.springframework.data.domain.Pageable) RestMethodModel(cz.habarta.typescript.generator.parser.RestMethodModel) Method(java.lang.reflect.Method) JaxrsApplicationParser(cz.habarta.typescript.generator.parser.JaxrsApplicationParser) RestApplicationModel(cz.habarta.typescript.generator.parser.RestApplicationModel) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) IntrospectionException(java.beans.IntrospectionException) TypeScriptGenerator(cz.habarta.typescript.generator.TypeScriptGenerator) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Utils.getInheritanceChain(cz.habarta.typescript.generator.util.Utils.getInheritanceChain) Type(java.lang.reflect.Type) PropertyDescriptor(java.beans.PropertyDescriptor) RestApplicationType(cz.habarta.typescript.generator.parser.RestApplicationType) SourceType(cz.habarta.typescript.generator.parser.SourceType) RestApplicationParser(cz.habarta.typescript.generator.parser.RestApplicationParser) SwaggerOperation(cz.habarta.typescript.generator.parser.SwaggerOperation) AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) BridgeMethodResolver(org.springframework.core.BridgeMethodResolver) JTypeWithNullability(cz.habarta.typescript.generator.type.JTypeWithNullability) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RequestBody(org.springframework.web.bind.annotation.RequestBody) Introspector(java.beans.Introspector) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Parameter(java.lang.reflect.Parameter) BeanInfo(java.beans.BeanInfo) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) TypeProcessor(cz.habarta.typescript.generator.TypeProcessor) MultiValueMap(org.springframework.util.MultiValueMap) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) Settings(cz.habarta.typescript.generator.Settings) Component(org.springframework.stereotype.Component) ParameterizedType(java.lang.reflect.ParameterizedType) Pair(cz.habarta.typescript.generator.util.Pair) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) Swagger(cz.habarta.typescript.generator.parser.Swagger) ResponseEntity(org.springframework.http.ResponseEntity) TsType(cz.habarta.typescript.generator.TsType) Utils(cz.habarta.typescript.generator.util.Utils) ValueConstants(org.springframework.web.bind.annotation.ValueConstants) RequestParam(org.springframework.web.bind.annotation.RequestParam) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) PathTemplate(cz.habarta.typescript.generator.parser.PathTemplate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) LinkedHashMap(java.util.LinkedHashMap) SwaggerOperation(cz.habarta.typescript.generator.parser.SwaggerOperation) RestQueryParam(cz.habarta.typescript.generator.parser.RestQueryParam) PropertyDescriptor(java.beans.PropertyDescriptor) SwaggerResponse(cz.habarta.typescript.generator.parser.SwaggerResponse) MethodParameterModel(cz.habarta.typescript.generator.parser.MethodParameterModel) Method(java.lang.reflect.Method) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Type(java.lang.reflect.Type) RestApplicationType(cz.habarta.typescript.generator.parser.RestApplicationType) SourceType(cz.habarta.typescript.generator.parser.SourceType) ParameterizedType(java.lang.reflect.ParameterizedType) TsType(cz.habarta.typescript.generator.TsType) RestMethodModel(cz.habarta.typescript.generator.parser.RestMethodModel) Parameter(java.lang.reflect.Parameter) PathVariable(org.springframework.web.bind.annotation.PathVariable)

Aggregations

Settings (cz.habarta.typescript.generator.Settings)1 TsType (cz.habarta.typescript.generator.TsType)1 TypeProcessor (cz.habarta.typescript.generator.TypeProcessor)1 TypeScriptGenerator (cz.habarta.typescript.generator.TypeScriptGenerator)1 JaxrsApplicationParser (cz.habarta.typescript.generator.parser.JaxrsApplicationParser)1 MethodParameterModel (cz.habarta.typescript.generator.parser.MethodParameterModel)1 PathTemplate (cz.habarta.typescript.generator.parser.PathTemplate)1 RestApplicationModel (cz.habarta.typescript.generator.parser.RestApplicationModel)1 RestApplicationParser (cz.habarta.typescript.generator.parser.RestApplicationParser)1 RestApplicationType (cz.habarta.typescript.generator.parser.RestApplicationType)1 RestMethodModel (cz.habarta.typescript.generator.parser.RestMethodModel)1 RestQueryParam (cz.habarta.typescript.generator.parser.RestQueryParam)1 SourceType (cz.habarta.typescript.generator.parser.SourceType)1 Swagger (cz.habarta.typescript.generator.parser.Swagger)1 SwaggerOperation (cz.habarta.typescript.generator.parser.SwaggerOperation)1 SwaggerResponse (cz.habarta.typescript.generator.parser.SwaggerResponse)1 JTypeWithNullability (cz.habarta.typescript.generator.type.JTypeWithNullability)1 GenericsResolver (cz.habarta.typescript.generator.util.GenericsResolver)1 Pair (cz.habarta.typescript.generator.util.Pair)1 Utils (cz.habarta.typescript.generator.util.Utils)1