Search in sources :

Example 1 with SwaggerContextService

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

the class SwaggerContextServiceTest method initializeAndGetScannerWithoutServletConfig.

@Test(description = "should add scanner to ScannerFactory")
public void initializeAndGetScannerWithoutServletConfig() {
    new SwaggerContextService().initScanner();
    new SwaggerContextService().initScanner();
    verify(servletConfig1, never()).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_DEFAULT), any(Scanner.class));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.1"), any(Scanner.class));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.2"), any(Scanner.class));
    verify(servletConfig2, never()).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_DEFAULT), any(Scanner.class));
    verify(servletContext2, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.2"), any(Scanner.class));
    verify(servletContext1, never()).setAttribute(eq(SCANNER_ID_PREFIX + "test.1"), any(Scanner.class));
    assertNotNull(ScannerFactory.getScanner());
    assertEquals(new SwaggerContextService().getScanner(), SwaggerScannerLocator.getInstance().getScanner(SCANNER_ID_DEFAULT));
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) DefaultJaxrsScanner(io.swagger.jaxrs.config.DefaultJaxrsScanner) Scanner(io.swagger.config.Scanner) Test(org.testng.annotations.Test)

Example 2 with SwaggerContextService

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

the class SwaggerContextServiceTest method getScannerWithInitParam.

@Test(description = "should call servletContext getAttribute with param SCANNER_ID_PREFIX + \"test.1/2\"")
public void getScannerWithInitParam() {
    stubWithInitParam();
    Scanner scanner1 = new SwaggerContextService().withServletConfig(servletConfig1).getScanner();
    Scanner scanner2 = new SwaggerContextService().withServletConfig(servletConfig2).getScanner();
    assertEquals(beanConfigScanner1, scanner1);
    assertEquals(beanConfigScanner2, scanner2);
    verify(servletConfig1, times(2)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext1, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletContext2, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.1"));
    verify(servletConfig2, times(2)).getInitParameter(eq(SCANNER_ID_KEY));
    verify(servletContext2, times(1)).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
    verify(servletContext1, never()).getAttribute(eq(SCANNER_ID_PREFIX + "test.2"));
}
Also used : DefaultJaxrsScanner(io.swagger.jaxrs.config.DefaultJaxrsScanner) Scanner(io.swagger.config.Scanner) SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Test(org.testng.annotations.Test)

Example 3 with SwaggerContextService

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

the class SwaggerContextServiceTest method initializeAndGetConfigWithoutServletConfig.

@Test(description = "should add SwaggerConfig to SwaggerConfigLocator map with key CONFIG_ID_DEFAULT")
public void initializeAndGetConfigWithoutServletConfig() {
    new SwaggerContextService().initConfig();
    new SwaggerContextService().initConfig();
    verify(servletConfig1, never()).getInitParameter(eq(CONFIG_ID_KEY));
    verify(servletConfig2, never()).getInitParameter(eq(CONFIG_ID_KEY));
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Test(org.testng.annotations.Test)

Example 4 with SwaggerContextService

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

the class BaseApiListingResource method process.

protected Swagger process(Application app, ServletContext servletContext, ServletConfig sc, HttpHeaders headers, UriInfo uriInfo) {
    SwaggerContextService ctxService = new SwaggerContextService().withServletConfig(sc).withBasePath(getBasePath(uriInfo));
    Swagger swagger = ctxService.getSwagger();
    synchronized (ApiListingResource.class) {
        if (SwaggerContextService.isScannerIdInitParamDefined(sc)) {
            if (!initializedScanner.containsKey(sc.getServletName() + "_" + SwaggerContextService.getScannerIdFromInitParam(sc))) {
                swagger = scan(app, servletContext, sc, uriInfo);
            }
        } else {
            if (SwaggerContextService.isConfigIdInitParamDefined(sc)) {
                if (!initializedConfig.containsKey(sc.getServletName() + "_" + SwaggerContextService.getConfigIdFromInitParam(sc))) {
                    swagger = scan(app, servletContext, sc, uriInfo);
                }
            } else if (SwaggerContextService.isUsePathBasedConfigInitParamDefined(sc)) {
                if (!initializedConfig.containsKey(sc.getServletName() + "_" + ctxService.getBasePath())) {
                    swagger = scan(app, servletContext, sc, uriInfo);
                }
            } else if (!initialized) {
                swagger = scan(app, servletContext, sc, uriInfo);
            }
        }
    }
    if (swagger != null) {
        SwaggerSpecFilter filterImpl = FilterFactory.getFilter();
        if (filterImpl != null) {
            SpecFilter f = new SpecFilter();
            swagger = f.filter(swagger, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers), getHeaders(headers));
        }
    }
    return swagger;
}
Also used : SwaggerContextService(io.swagger.jaxrs.config.SwaggerContextService) Swagger(io.swagger.models.Swagger) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SpecFilter(io.swagger.core.filter.SpecFilter)

Example 5 with SwaggerContextService

use of io.swagger.jaxrs.config.SwaggerContextService 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)

Aggregations

SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)10 Test (org.testng.annotations.Test)6 Scanner (io.swagger.config.Scanner)4 DefaultJaxrsScanner (io.swagger.jaxrs.config.DefaultJaxrsScanner)3 Swagger (io.swagger.models.Swagger)3 ServletContext (javax.servlet.ServletContext)2 SwaggerConfig (io.swagger.config.SwaggerConfig)1 SpecFilter (io.swagger.core.filter.SpecFilter)1 SwaggerSpecFilter (io.swagger.core.filter.SwaggerSpecFilter)1 Reader (io.swagger.jaxrs.Reader)1 JaxrsScanner (io.swagger.jaxrs.config.JaxrsScanner)1 WebXMLReader (io.swagger.jaxrs.config.WebXMLReader)1