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));
}
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"));
}
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));
}
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;
}
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;
}
Aggregations