Search in sources :

Example 1 with SpecFilter

use of io.swagger.core.filter.SpecFilter 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 2 with SpecFilter

use of io.swagger.core.filter.SpecFilter in project swagger-core by swagger-api.

the class SpecFilterTest method cloneRetainDeperecatedFlags.

@Test(description = "Clone should retain any 'deperecated' flags present on operations")
public void cloneRetainDeperecatedFlags() throws IOException {
    final Swagger swagger = getSwagger("specFiles/deprecatedoperationmodel.json");
    final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
    final Swagger filtered = new SpecFilter().filter(swagger, remover, null, null, null);
    Operation operation = filtered.getPath("/test").getOperations().get(0);
    Boolean deprectedFlag = operation.isDeprecated();
    assertNotNull(deprectedFlag);
    assertEquals(deprectedFlag, Boolean.TRUE);
}
Also used : Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 3 with SpecFilter

use of io.swagger.core.filter.SpecFilter in project swagger-core by swagger-api.

the class SpecFilterTest method filterAwayGetOperations.

@Test(description = "it should filter away get operations in a resource")
public void filterAwayGetOperations() throws IOException {
    final Swagger swagger = getSwagger("specFiles/petstore.json");
    final NoGetOperationsFilter filter = new NoGetOperationsFilter();
    final Swagger filtered = new SpecFilter().filter(swagger, filter, null, null, null);
    if (filtered.getPaths() != null) {
        for (Map.Entry<String, Path> entry : filtered.getPaths().entrySet()) {
            assertNull(entry.getValue().getGet());
        }
    } else {
        fail("paths should not be null");
    }
}
Also used : Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 4 with SpecFilter

use of io.swagger.core.filter.SpecFilter in project swagger-core by swagger-api.

the class SpecFilterTest method removeUnreferencedDefinitionsOfRecuriveModels.

@Test(description = "recursive models, e.g. A-> A or A-> B and B -> A should not result in stack overflow")
public void removeUnreferencedDefinitionsOfRecuriveModels() throws IOException {
    final Swagger swagger = getSwagger("specFiles/recursivemodels.json");
    final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
    final Swagger filtered = new SpecFilter().filter(swagger, remover, null, null, null);
    assertNotNull(filtered.getDefinitions().get("SelfReferencingModel"));
    assertNotNull(filtered.getDefinitions().get("IndirectRecursiveModelA"));
    assertNotNull(filtered.getDefinitions().get("IndirectRecursiveModelB"));
}
Also used : Swagger(io.swagger.models.Swagger) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 5 with SpecFilter

use of io.swagger.core.filter.SpecFilter in project swagger-core by swagger-api.

the class SpecFilterTest method filterNoPropertiesModels.

@Test(description = "it should filter models where some fields have no properties")
public void filterNoPropertiesModels() throws IOException {
    final String modelName = "Array";
    final ModelImpl model = new ModelImpl().type(ModelImpl.OBJECT).name(modelName);
    final Swagger swagger = new Swagger();
    swagger.addDefinition(modelName, model);
    final Map<String, Model> filtered = new SpecFilter().filterDefinitions(new NoOpOperationsFilter(), swagger.getDefinitions(), null, null, null);
    if (filtered.size() != 1) {
        fail("ModelImpl with no properties failed to filter");
    }
}
Also used : Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) ModelImpl(io.swagger.models.ModelImpl) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Aggregations

SpecFilter (io.swagger.core.filter.SpecFilter)21 Swagger (io.swagger.models.Swagger)21 Test (org.testng.annotations.Test)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Map (java.util.Map)4 SwaggerSpecFilter (io.swagger.core.filter.SwaggerSpecFilter)3 Path (io.swagger.models.Path)3 ApiOperation (io.swagger.annotations.ApiOperation)2 Model (io.swagger.models.Model)2 Operation (io.swagger.models.Operation)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 SwaggerContextService (io.swagger.jaxrs.config.SwaggerContextService)1 ModelImpl (io.swagger.models.ModelImpl)1 Parameter (io.swagger.models.parameters.Parameter)1 IOException (java.io.IOException)1