Search in sources :

Example 1 with SwaggerConfig

use of io.swagger.config.SwaggerConfig in project swagger-core by swagger-api.

the class SwaggerConfigLocatorTest method putConfigFirstTime.

@Test(description = "should add given config to map ")
public void putConfigFirstTime() {
    SwaggerConfig config = new SwaggerConfig() {

        @Override
        public Swagger configure(Swagger swagger) {
            return swagger;
        }

        @Override
        public String getFilterClass() {
            return null;
        }
    };
    SwaggerConfigLocator.getInstance().putConfig(id, config);
    assertEquals(SwaggerConfigLocator.getInstance().getConfig(id), config);
}
Also used : SwaggerConfig(io.swagger.config.SwaggerConfig) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 2 with SwaggerConfig

use of io.swagger.config.SwaggerConfig in project swagger-core by swagger-api.

the class SwaggerConfigLocatorTest method putConfigSecondTime.

@Test(description = "shouldn't add given config to map because already set")
public void putConfigSecondTime() {
    putConfigFirstTime();
    SwaggerConfig config = new SwaggerConfig() {

        @Override
        public Swagger configure(Swagger swagger) {
            return swagger;
        }

        @Override
        public String getFilterClass() {
            return null;
        }
    };
    SwaggerConfigLocator.getInstance().putConfig(id, config);
    assertNotEquals(SwaggerConfigLocator.getInstance().getConfig(id), config);
}
Also used : SwaggerConfig(io.swagger.config.SwaggerConfig) Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 3 with SwaggerConfig

use of io.swagger.config.SwaggerConfig in project swagger-core by swagger-api.

the class SwaggerContextService method initConfig.

public SwaggerContextService initConfig(Swagger swagger) {
    String configIdKey;
    if (configId != null) {
        configIdKey = CONFIG_ID_PREFIX + configId;
    } else if (contextId != null) {
        configIdKey = CONFIG_ID_PREFIX + contextId;
    } else {
        if (isServletConfigAvailable(sc)) {
            configIdKey = (sc.getInitParameter(CONFIG_ID_KEY) != null) ? CONFIG_ID_PREFIX + sc.getInitParameter(CONFIG_ID_KEY) : null;
            if (configIdKey == null) {
                boolean usePathBasedConfig = Boolean.valueOf(sc.getInitParameter(USE_PATH_BASED_CONFIG));
                if (usePathBasedConfig && StringUtils.isNotBlank(basePath)) {
                    configIdKey = CONFIG_ID_PREFIX + basePath;
                } else {
                    configIdKey = (sc.getInitParameter(CONTEXT_ID_KEY) != null) ? CONFIG_ID_PREFIX + sc.getInitParameter(CONTEXT_ID_KEY) : CONFIG_ID_DEFAULT;
                }
            }
        } else {
            if (isUsePathBasedConfig() && StringUtils.isNotBlank(basePath)) {
                configIdKey = CONFIG_ID_PREFIX + basePath;
            } else {
                configIdKey = CONFIG_ID_DEFAULT;
            }
        }
    }
    SwaggerConfig value = (swaggerConfig != null) ? swaggerConfig : null;
    if (value == null && isServletConfigAvailable(sc)) {
        value = new WebXMLReader(sc);
    }
    if (value != null) {
        SwaggerConfigLocator.getInstance().putConfig(configIdKey, value);
    }
    if (swagger != null) {
        SwaggerConfigLocator.getInstance().putSwagger(configIdKey, swagger);
    }
    return this;
}
Also used : SwaggerConfig(io.swagger.config.SwaggerConfig)

Example 4 with SwaggerConfig

use of io.swagger.config.SwaggerConfig in project swagger-core by swagger-api.

the class BaseApiListingResource method scan.

private static synchronized Swagger scan(Application app, ServletContext context, ServletConfig sc, UriInfo uriInfo) {
    Swagger swagger = null;
    SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo));
    Scanner scanner = ctxService.getScanner();
    if (scanner != null) {
        SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
        swagger = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).getSwagger();
        Set<Class<?>> classes;
        if (scanner instanceof JaxrsScanner) {
            JaxrsScanner jaxrsScanner = (JaxrsScanner) scanner;
            classes = jaxrsScanner.classesFromContext(app, sc);
        } else {
            classes = scanner.classes();
        }
        if (classes != null) {
            Reader reader = new Reader(swagger, ReaderConfigUtils.getReaderConfig(context));
            swagger = reader.read(classes);
            if (scanner instanceof SwaggerConfig) {
                swagger = ((SwaggerConfig) scanner).configure(swagger);
            } else {
                SwaggerConfig swaggerConfig = ctxService.getConfig();
                if (swaggerConfig != null) {
                    LOGGER.debug("configuring swagger with " + swaggerConfig);
                    swaggerConfig.configure(swagger);
                } else {
                    LOGGER.debug("no configurator");
                }
            }
            new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo)).updateSwagger(swagger);
        }
    }
    if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
        initializedScanner.put(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc), true);
    } else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
        initializedConfig.put(sc.getServletName() + "_" + ctxService.getBasePath(), true);
    } else {
        initialized = true;
    }
    return swagger;
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Scanner(io.swagger.config.Scanner) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) SwaggerConfig(io.swagger.config.SwaggerConfig) JaxrsScanner(io.swagger.jaxrs.config.JaxrsScanner) Swagger(io.swagger.models.Swagger) Reader(io.swagger.jaxrs.Reader)

Example 5 with SwaggerConfig

use of io.swagger.config.SwaggerConfig in project swagger-core by swagger-api.

the class ApiListingJSON method scan.

protected synchronized void scan(Application app) {
    Scanner scanner = ScannerFactory.getScanner();
    LOGGER.debug("using scanner " + scanner);
    if (scanner != null) {
        SwaggerSerializers.setPrettyPrint(scanner.getPrettyPrint());
        Set<Class<?>> classes = null;
        classes = scanner.classes();
        if (classes != null) {
            Reader reader = new Reader(swagger);
            swagger = reader.read(classes);
            if (scanner instanceof SwaggerConfig) {
                swagger = ((SwaggerConfig) scanner).configure(swagger);
            }
        }
    }
    initialized = true;
}
Also used : Scanner(io.swagger.config.Scanner) SwaggerConfig(io.swagger.config.SwaggerConfig) Reader(io.swagger.jaxrs.Reader)

Aggregations

SwaggerConfig (io.swagger.config.SwaggerConfig)7 Swagger (io.swagger.models.Swagger)5 Scanner (io.swagger.config.Scanner)4 Reader (io.swagger.jaxrs.Reader)3 Test (org.testng.annotations.Test)2 JaxrsScanner (io.swagger.jaxrs.config.JaxrsScanner)1 SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)1 ServletContext (javax.servlet.ServletContext)1