Search in sources :

Example 6 with OpenApiConfigurationException

use of io.swagger.v3.oas.integration.OpenApiConfigurationException in project swagger-core by swagger-api.

the class SwaggerServletInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
    if (classes != null && !classes.isEmpty()) {
        Set<Class<?>> resources = new LinkedHashSet();
        classes.stream().filter(c -> ignored.stream().noneMatch(i -> c.getName().startsWith(i))).forEach(resources::add);
        if (!resources.isEmpty()) {
            // init context
            try {
                SwaggerConfiguration oasConfig = new SwaggerConfiguration().resourceClasses(resources.stream().map(Class::getName).collect(Collectors.toSet()));
                new JaxrsOpenApiContextBuilder().openApiConfiguration(oasConfig).buildContext(true);
            } catch (OpenApiConfigurationException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ServletException(javax.servlet.ServletException) HandlesTypes(javax.servlet.annotation.HandlesTypes) Path(javax.ws.rs.Path) ApplicationPath(javax.ws.rs.ApplicationPath) Set(java.util.Set) Collectors(java.util.stream.Collectors) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) HashSet(java.util.HashSet) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) OpenAPIDefinition(io.swagger.v3.oas.annotations.OpenAPIDefinition) JaxrsOpenApiContextBuilder(io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) ServletContext(javax.servlet.ServletContext) IgnoredPackages(io.swagger.v3.oas.integration.IgnoredPackages) LinkedHashSet(java.util.LinkedHashSet) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) JaxrsOpenApiContextBuilder(io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration)

Example 7 with OpenApiConfigurationException

use of io.swagger.v3.oas.integration.OpenApiConfigurationException in project swagger-core by swagger-api.

the class GenericOpenApiContext method init.

@Override
public T init() throws OpenApiConfigurationException {
    if (openApiConfiguration == null) {
        openApiConfiguration = loadConfiguration();
    }
    if (openApiConfiguration == null) {
        openApiConfiguration = new SwaggerConfiguration().resourcePackages(resourcePackages).resourceClasses(resourceClasses);
        ((SwaggerConfiguration) openApiConfiguration).setId(id);
        ((SwaggerConfiguration) openApiConfiguration).setOpenAPI31(openAPI31);
    }
    openApiConfiguration = mergeParentConfiguration(openApiConfiguration, parent);
    try {
        if (openApiReader == null) {
            openApiReader = buildReader(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (openApiScanner == null) {
            openApiScanner = buildScanner(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (objectMapperProcessor == null) {
            objectMapperProcessor = buildObjectMapperProcessor(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (modelConverters == null || modelConverters.isEmpty()) {
            modelConverters = buildModelConverters(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (outputJsonMapper == null) {
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputJsonMapper = Json31.mapper().copy();
            } else {
                outputJsonMapper = Json.mapper().copy();
            }
        }
        if (outputYamlMapper == null) {
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputYamlMapper = Yaml31.mapper().copy();
            } else {
                outputYamlMapper = Yaml.mapper().copy();
            }
        }
        if (openApiConfiguration.isSortOutput() != null && openApiConfiguration.isSortOutput()) {
            outputJsonMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
            outputJsonMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
            outputYamlMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
            outputYamlMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
                outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
                outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
                outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
            } else {
                outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
                outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
                outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
                outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error initializing context: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error initializing context: " + e.getMessage(), e);
    }
    try {
        if (objectMapperProcessor != null) {
            ObjectMapper mapper = IntegrationObjectMapperFactory.createJson();
            objectMapperProcessor.processJsonObjectMapper(mapper);
            ModelConverters.getInstance().addConverter(new ModelResolver(mapper));
            objectMapperProcessor.processOutputJsonObjectMapper(outputJsonMapper);
            objectMapperProcessor.processOutputYamlObjectMapper(outputYamlMapper);
        }
    } catch (Exception e) {
        LOGGER.error("error configuring objectMapper: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error configuring objectMapper: " + e.getMessage(), e);
    }
    try {
        if (modelConverters != null && !modelConverters.isEmpty()) {
            for (ModelConverter converter : modelConverters) {
                ModelConverters.getInstance().addConverter(converter);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error configuring model converters: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error configuring model converters: " + e.getMessage(), e);
    }
    // set cache TTL if present in configuration
    if (openApiConfiguration.getCacheTTL() != null) {
        this.cacheTTL = openApiConfiguration.getCacheTTL();
    }
    register();
    return (T) this;
}
Also used : IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelResolver(io.swagger.v3.core.jackson.ModelResolver)

Example 8 with OpenApiConfigurationException

use of io.swagger.v3.oas.integration.OpenApiConfigurationException in project swagger-core by swagger-api.

the class ServletOpenApiContextBuilder method buildContext.

@Override
public OpenApiContext buildContext(boolean init) throws OpenApiConfigurationException {
    if (StringUtils.isBlank(ctxId)) {
        ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT;
    }
    OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
    if (ctx == null) {
        OpenApiContext rootCtx = OpenApiContextLocator.getInstance().getOpenApiContext(OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT);
        ctx = new XmlWebOpenApiContext().servletConfig(servletConfig).openApiConfiguration(openApiConfiguration).id(ctxId).parent(rootCtx);
        if (ctx.getConfigLocation() == null && configLocation != null) {
            ((XmlWebOpenApiContext) ctx).configLocation(configLocation);
        }
        if (((XmlWebOpenApiContext) ctx).getResourcePackages() == null && resourcePackages != null) {
            ((XmlWebOpenApiContext) ctx).resourcePackages(resourcePackages);
        }
        if (((XmlWebOpenApiContext) ctx).getResourceClasses() == null && resourceClasses != null) {
            ((XmlWebOpenApiContext) ctx).resourceClasses(resourceClasses);
        }
        if (init) {
            // includes registering itself with OpenApiContextLocator
            ctx.init();
        }
    }
    return ctx;
}
Also used : OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Example 9 with OpenApiConfigurationException

use of io.swagger.v3.oas.integration.OpenApiConfigurationException in project swagger-core by swagger-api.

the class GenericOpenApiContextBuilder method buildContext.

@Override
public OpenApiContext buildContext(boolean init) throws OpenApiConfigurationException {
    if (StringUtils.isBlank(ctxId)) {
        ctxId = OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT;
    }
    OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
    if (ctx == null) {
        OpenApiContext rootCtx = OpenApiContextLocator.getInstance().getOpenApiContext(OpenApiContext.OPENAPI_CONTEXT_ID_DEFAULT);
        ctx = new GenericOpenApiContext().openApiConfiguration(openApiConfiguration).id(ctxId).parent(rootCtx);
        if (ctx.getConfigLocation() == null && configLocation != null) {
            ((GenericOpenApiContext) ctx).configLocation(configLocation);
        }
        if (((GenericOpenApiContext) ctx).getResourcePackages() == null && resourcePackages != null) {
            ((GenericOpenApiContext) ctx).resourcePackages(resourcePackages);
        }
        if (((GenericOpenApiContext) ctx).getResourceClasses() == null && resourceClasses != null) {
            ((GenericOpenApiContext) ctx).resourceClasses(resourceClasses);
        }
        if (init) {
            // includes registering itself with OpenApiContextLocator
            ctx.init();
        }
    }
    return ctx;
}
Also used : OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Aggregations

OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)6 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)5 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)5 OpenAPI (io.swagger.v3.oas.models.OpenAPI)3 HashSet (java.util.HashSet)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)2 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)2 SpecFilter (io.swagger.v3.core.filter.SpecFilter)2 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)2 OpenAPIDefinition (io.swagger.v3.oas.annotations.OpenAPIDefinition)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2 IgnoredPackages (io.swagger.v3.oas.integration.IgnoredPackages)2 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 HandlesTypes (javax.servlet.annotation.HandlesTypes)2