Search in sources :

Example 1 with OpenApiLocaleCustomizer

use of org.springdoc.core.customizers.OpenApiLocaleCustomizer in project springdoc-openapi by springdoc.

the class AbstractOpenApiResource method getOpenApi.

/**
 * Gets open api.
 * @param locale the locale
 * @return the open api
 */
protected synchronized OpenAPI getOpenApi(Locale locale) {
    OpenAPI openApi;
    final Locale finalLocale = locale == null ? Locale.getDefault() : locale;
    if (openAPIService.getCachedOpenAPI(finalLocale) == null || springDocConfigProperties.isCacheDisabled()) {
        Instant start = Instant.now();
        openAPIService.build(finalLocale);
        Map<String, Object> mappingsMap = openAPIService.getMappingsMap().entrySet().stream().filter(controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(), Hidden.class) == null)).filter(controller -> !AbstractOpenApiResource.isHiddenRestControllers(controller.getValue().getClass())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
        Map<String, Object> findControllerAdvice = openAPIService.getControllerAdviceMap();
        // calculate generic responses
        openApi = openAPIService.getCalculatedOpenAPI();
        if (springDocConfigProperties.isOverrideWithGenericResponse()) {
            if (!CollectionUtils.isEmpty(mappingsMap))
                findControllerAdvice.putAll(mappingsMap);
            responseBuilder.buildGenericResponse(openApi.getComponents(), findControllerAdvice, finalLocale);
        }
        getPaths(mappingsMap, finalLocale);
        Optional<CloudFunctionProvider> cloudFunctionProviderOptional = springDocProviders.getSpringCloudFunctionProvider();
        cloudFunctionProviderOptional.ifPresent(cloudFunctionProvider -> {
            List<RouterOperation> routerOperationList = cloudFunctionProvider.getRouterOperations(openApi);
            if (!CollectionUtils.isEmpty(routerOperationList))
                this.calculatePath(routerOperationList, locale);
        });
        if (!CollectionUtils.isEmpty(openApi.getServers()))
            openAPIService.setServersPresent(true);
        openAPIService.updateServers(openApi);
        if (springDocConfigProperties.isRemoveBrokenReferenceDefinitions())
            this.removeBrokenReferenceDefinitions(openApi);
        // run the optional customisers
        List<Server> servers = openApi.getServers();
        List<Server> serversCopy = null;
        try {
            serversCopy = Json.mapper().readValue(Json.mapper().writeValueAsString(servers), new TypeReference<List<Server>>() {
            });
        } catch (JsonProcessingException e) {
            LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
        }
        openApiLocaleCustomizers.values().forEach(openApiLocaleCustomizer -> openApiLocaleCustomizer.customise(openApi, finalLocale));
        openApiCustomisers.ifPresent(apiCustomisers -> apiCustomisers.forEach(openApiCustomiser -> openApiCustomiser.customise(openApi)));
        if (!CollectionUtils.isEmpty(openApi.getServers()) && !openApi.getServers().equals(serversCopy))
            openAPIService.setServersPresent(true);
        openAPIService.setCachedOpenAPI(openApi, finalLocale);
        openAPIService.resetCalculatedOpenAPI();
        LOGGER.info("Init duration for springdoc-openapi is: {} ms", Duration.between(start, Instant.now()).toMillis());
    } else {
        LOGGER.debug("Fetching openApi document from cache");
        openApi = openAPIService.updateServers(openAPIService.getCachedOpenAPI(finalLocale));
    }
    return openApi;
}
Also used : Locale(java.util.Locale) RouterOperations(org.springdoc.core.annotations.RouterOperations) Arrays(java.util.Arrays) JsonView(com.fasterxml.jackson.annotation.JsonView) URLDecoder(java.net.URLDecoder) AopUtils(org.springframework.aop.support.AopUtils) Yaml(io.swagger.v3.core.util.Yaml) URISyntaxException(java.net.URISyntaxException) Parameter(io.swagger.v3.oas.models.parameters.Parameter) LoggerFactory(org.slf4j.LoggerFactory) Operation(io.swagger.v3.oas.models.Operation) DOT(org.springdoc.core.Constants.DOT) StringUtils(org.apache.commons.lang3.StringUtils) OpenApiLocaleCustomizer(org.springdoc.core.customizers.OpenApiLocaleCustomizer) HandlerMethod(org.springframework.web.method.HandlerMethod) JavadocProvider(org.springdoc.core.providers.JavadocProvider) OpenApiMethodFilter(org.springdoc.core.filters.OpenApiMethodFilter) SchemaPropertyDeprecatingConverter.isDeprecated(org.springdoc.core.converters.SchemaPropertyDeprecatingConverter.isDeprecated) Locale(java.util.Locale) Duration(java.time.Duration) Map(java.util.Map) OperationService(org.springdoc.core.OperationService) CloudFunctionProvider(org.springdoc.core.providers.CloudFunctionProvider) URI(java.net.URI) RouterOperation(org.springdoc.core.fn.RouterOperation) AntPathMatcher(org.springframework.util.AntPathMatcher) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ACTUATOR_DEFAULT_GROUP(org.springdoc.core.Constants.ACTUATOR_DEFAULT_GROUP) Method(java.lang.reflect.Method) SortedOpenAPIMixin(org.springdoc.api.mixins.SortedOpenAPIMixin) MethodAttributes(org.springdoc.core.MethodAttributes) Hidden(io.swagger.v3.oas.annotations.Hidden) Collection(java.util.Collection) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Paths(io.swagger.v3.oas.models.Paths) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) DEFAULT_PATH_SEPARATOR(org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.swagger.v3.oas.models.PathItem.HttpMethod) Server(io.swagger.v3.oas.models.servers.Server) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Environment(org.springframework.core.env.Environment) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Feature(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature) OperationCustomizer(org.springdoc.core.customizers.OperationCustomizer) AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) ActuatorProvider(org.springdoc.core.providers.ActuatorProvider) Json(io.swagger.v3.core.util.Json) GenericResponseService(org.springdoc.core.GenericResponseService) AbstractRouterFunctionVisitor(org.springdoc.core.fn.AbstractRouterFunctionVisitor) OPERATION_ATTRIBUTE(org.springdoc.core.Constants.OPERATION_ATTRIBUTE) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) SpecFilter(io.swagger.v3.core.filter.SpecFilter) ParameterIn(io.swagger.v3.oas.annotations.enums.ParameterIn) ArrayList(java.util.ArrayList) LINKS_SCHEMA_CUSTOMISER(org.springdoc.core.Constants.LINKS_SCHEMA_CUSTOMISER) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) RequestBody(org.springframework.web.bind.annotation.RequestBody) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SortedSchemaMixin(org.springdoc.api.mixins.SortedSchemaMixin) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) OpenAPIService(org.springdoc.core.OpenAPIService) AbstractRequestService(org.springdoc.core.AbstractRequestService) Schema(io.swagger.v3.oas.models.media.Schema) SpringDocProviders(org.springdoc.core.SpringDocProviders) OpenApiCustomiser(org.springdoc.core.customizers.OpenApiCustomiser) GenericParameterService(org.springdoc.core.GenericParameterService) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SpringDocConfigProperties(org.springdoc.core.SpringDocConfigProperties) Logger(org.slf4j.Logger) RouterFunctionData(org.springdoc.core.fn.RouterFunctionData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PathItem(io.swagger.v3.oas.models.PathItem) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Callback(io.swagger.v3.oas.annotations.callbacks.Callback) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ApplicationContext(org.springframework.context.ApplicationContext) GroupConfig(org.springdoc.core.SpringDocConfigProperties.GroupConfig) ReflectionUtils(io.swagger.v3.core.util.ReflectionUtils) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Components(io.swagger.v3.oas.models.Components) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) SPRING_MVC_SERVLET_PATH(org.springdoc.core.Constants.SPRING_MVC_SERVLET_PATH) Collections(java.util.Collections) Server(io.swagger.v3.oas.models.servers.Server) Instant(java.time.Instant) RouterOperation(org.springdoc.core.fn.RouterOperation) Hidden(io.swagger.v3.oas.annotations.Hidden) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CloudFunctionProvider(org.springdoc.core.providers.CloudFunctionProvider) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonView (com.fasterxml.jackson.annotation.JsonView)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Feature (com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature)1 SpecFilter (io.swagger.v3.core.filter.SpecFilter)1 Json (io.swagger.v3.core.util.Json)1 ReflectionUtils (io.swagger.v3.core.util.ReflectionUtils)1 Yaml (io.swagger.v3.core.util.Yaml)1 Hidden (io.swagger.v3.oas.annotations.Hidden)1 Callback (io.swagger.v3.oas.annotations.callbacks.Callback)1 ParameterIn (io.swagger.v3.oas.annotations.enums.ParameterIn)1 Components (io.swagger.v3.oas.models.Components)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Operation (io.swagger.v3.oas.models.Operation)1 PathItem (io.swagger.v3.oas.models.PathItem)1 HttpMethod (io.swagger.v3.oas.models.PathItem.HttpMethod)1