Search in sources :

Example 11 with SpecFilter

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

the class SwaggerJSONResource method getListingYaml.

@GET
@Produces("application/yaml")
@Path(OrcidApiConstants.SWAGGER_FILE_YAML)
@ApiOperation(value = "The swagger definition in YAML", hidden = true)
public Response getListingYaml(@Context Application app, @Context HttpHeaders headers, @Context UriInfo uriInfo) {
    Swagger swagger = (Swagger) context.getAttribute("swagger");
    if (!initialized) {
        swagger = scan(app);
    }
    try {
        if (swagger != null) {
            SwaggerSpecFilter filterImpl = FilterFactory.getFilter();
            LOGGER.debug("using filter " + filterImpl);
            if (filterImpl != null) {
                SpecFilter f = new SpecFilter();
                swagger = f.filter(swagger, filterImpl, getQueryParams(uriInfo.getQueryParameters()), getCookies(headers), getHeaders(headers));
            }
            String yaml = Yaml.mapper().writeValueAsString(swagger);
            String[] parts = yaml.split("\n");
            StringBuilder b = new StringBuilder();
            for (String part : parts) {
                //int pos = part.indexOf("!<");
                //int endPos = part.indexOf(">");
                b.append(part);
                b.append("\n");
            }
            return Response.ok().entity(b.toString()).type("application/yaml").build();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Response.status(404).build();
}
Also used : Swagger(io.swagger.models.Swagger) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SpecFilter(io.swagger.core.filter.SpecFilter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 12 with SpecFilter

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

the class SwaggerJSONResource method getListingJson.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path(OrcidApiConstants.SWAGGER_FILE)
@ApiOperation(value = "The swagger definition in JSON", hidden = true)
public Response getListingJson(@Context Application app, @Context HttpHeaders headers, @Context UriInfo uriInfo) {
    Swagger swagger = (Swagger) context.getAttribute("swagger");
    if (!initialized) {
        swagger = scan(app);
    }
    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 Response.ok().entity(swagger).build();
    } else {
        return Response.status(404).build();
    }
}
Also used : Swagger(io.swagger.models.Swagger) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SwaggerSpecFilter(io.swagger.core.filter.SwaggerSpecFilter) SpecFilter(io.swagger.core.filter.SpecFilter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 13 with SpecFilter

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

the class SpecFilterTest method filterAwayBrokenReferenceModelProperties.

@Test(description = "it should filter away broken reference model properties")
public void filterAwayBrokenReferenceModelProperties() throws IOException {
    final Swagger swagger = getSwagger("specFiles/paramAndResponseRef.json");
    assertNotNull(swagger.getDefinitions().get("Order"));
    assertNotNull(swagger.getDefinitions().get("NoPropertiesModel"));
    assertNotNull(swagger.getDefinitions().get("OrderTag"));
    assertNotNull(swagger.getDefinitions().get("Tag"));
    final NoOpOperationsFilter noOpfilter = new NoOpOperationsFilter();
    Swagger filtered = new SpecFilter().filter(swagger, noOpfilter, null, null, null);
    assertNotNull(filtered.getDefinitions().get("Order"));
    assertNotNull(filtered.getDefinitions().get("NoPropertiesModel"));
    assertNotNull(filtered.getDefinitions().get("OrderTag"));
    assertNotNull(filtered.getDefinitions().get("Tag"));
    final RemoveUnreferencedDefinitionsFilter refFilter = new RemoveUnreferencedDefinitionsFilter();
    filtered = new SpecFilter().filter(swagger, refFilter, null, null, null);
    assertNull(filtered.getDefinitions().get("Order"));
    assertNull(filtered.getDefinitions().get("NoPropertiesModel"));
    assertNull(filtered.getDefinitions().get("OrderTag"));
    assertNotNull(filtered.getDefinitions().get("Tag"));
}
Also used : Swagger(io.swagger.models.Swagger) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 14 with SpecFilter

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

the class SpecFilterTest method cloneEverything.

@Test(description = "it should clone everything")
public void cloneEverything() throws IOException {
    final Swagger swagger = getSwagger("specFiles/petstore.json");
    final Swagger filtered = new SpecFilter().filter(swagger, new NoOpOperationsFilter(), null, null, null);
    assertEquals(Json.pretty(swagger), Json.pretty(filtered));
}
Also used : Swagger(io.swagger.models.Swagger) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 15 with SpecFilter

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

the class SpecFilterTest method cloneWithoutModels.

@Test(description = "it should clone everything from JSON without models")
public void cloneWithoutModels() throws IOException {
    final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/noModels.json");
    final Swagger swagger = Json.mapper().readValue(json, Swagger.class);
    final Swagger filtered = new SpecFilter().filter(swagger, new NoOpOperationsFilter(), null, null, null);
    SerializationMatchers.assertEqualsToJson(filtered, json);
}
Also used : Swagger(io.swagger.models.Swagger) 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