Search in sources :

Example 1 with OpenAPISpecFilter

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

the class SpecFilter method filterResponse.

protected ApiResponse filterResponse(OpenAPISpecFilter filter, Operation operation, ApiResponse response, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (response != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<ApiResponse> filteredResponse = filter.filterResponse(response, operation, description, params, cookies, headers);
        if (filteredResponse.isPresent()) {
            return filteredResponse.get();
        }
    }
    return null;
}
Also used : ApiDescription(io.swagger.v3.core.model.ApiDescription) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 2 with OpenAPISpecFilter

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

the class SpecFilter method filterRequestBody.

protected RequestBody filterRequestBody(OpenAPISpecFilter filter, Operation operation, RequestBody requestBody, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (requestBody != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<RequestBody> filteredRequestBody = filter.filterRequestBody(requestBody, operation, description, params, cookies, headers);
        if (filteredRequestBody.isPresent()) {
            return filteredRequestBody.get();
        }
    }
    return null;
}
Also used : ApiDescription(io.swagger.v3.core.model.ApiDescription) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 3 with OpenAPISpecFilter

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

the class SpecFilter method filterParameter.

protected Parameter filterParameter(OpenAPISpecFilter filter, Operation operation, Parameter parameter, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    if (parameter != null) {
        ApiDescription description = new ApiDescription(resourcePath, key);
        Optional<Parameter> filteredParameter = filter.filterParameter(parameter, operation, description, params, cookies, headers);
        if (filteredParameter.isPresent()) {
            return filteredParameter.get();
        }
    }
    return null;
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) ApiDescription(io.swagger.v3.core.model.ApiDescription)

Example 4 with OpenAPISpecFilter

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

the class ReaderTest method testResponseWithFilter.

@Test(description = "Responses with filter")
public void testResponseWithFilter() {
    Components components = new Components();
    components.addResponses("invalidJWT", new ApiResponse().description("when JWT token invalid/expired"));
    OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
    Reader reader = new Reader(oas);
    OpenAPI openAPI = reader.read(SimpleResponsesResource.class);
    OpenAPISpecFilter filterImpl = new RefResponseFilter();
    SpecFilter f = new SpecFilter();
    openAPI = f.filter(openAPI, filterImpl, null, null, null);
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  description: info\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      summary: Simple get operation\n" + "      description: Defines a simple get operation with no inputs and a complex output\n" + "        object\n" + "      operationId: getWithPayloadResponse\n" + "      responses:\n" + "        \"200\":\n" + "          description: voila!\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/SampleResponseSchema'\n" + "        default:\n" + "          description: boo\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/GenericError'\n" + "        \"401\":\n" + "          $ref: '#/components/responses/invalidJWT'\n" + "      deprecated: true\n" + "components:\n" + "  schemas:\n" + "    GenericError:\n" + "      type: object\n" + "    SampleResponseSchema:\n" + "      type: object\n" + "  responses:\n" + "    invalidJWT:\n" + "      description: when JWT token invalid/expired";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : Components(io.swagger.v3.oas.models.Components) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) AbstractSpecFilter(io.swagger.v3.core.filter.AbstractSpecFilter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) SpecFilter(io.swagger.v3.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 5 with OpenAPISpecFilter

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

the class OpenApiServlet method doGet.

// TODO move to own servlet non jaxrs project and reference from there
// TODO cleanup and errors
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String ctxId = getContextIdFromServletConfig(getServletConfig());
    OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
    OpenAPI oas = ctx.read();
    if (oas != null) {
        if (ctx.getOpenApiConfiguration() != null && ctx.getOpenApiConfiguration().getFilterClass() != null) {
            try {
                OpenAPISpecFilter filterImpl = (OpenAPISpecFilter) Class.forName(ctx.getOpenApiConfiguration().getFilterClass()).newInstance();
                SpecFilter f = new SpecFilter();
                oas = f.filter(oas, filterImpl, ServletUtils.getQueryParams(req.getParameterMap()), ServletUtils.getCookies(req.getCookies()), ServletUtils.getHeaders(req));
            } catch (Exception e) {
                LOGGER.error("failed to load filter", e);
            }
        }
    }
    String type = "json";
    String acceptHeader = req.getHeader(ACCEPT_HEADER);
    if (!StringUtils.isBlank(acceptHeader) && acceptHeader.toLowerCase().contains(APPLICATION_YAML)) {
        type = "yaml";
    } else {
        // check URL:
        if (req.getRequestURL().toString().toLowerCase().endsWith("yaml")) {
            type = "yaml";
        }
    }
    boolean pretty = false;
    if (ctx.getOpenApiConfiguration() != null && Boolean.TRUE.equals(ctx.getOpenApiConfiguration().isPrettyPrint())) {
        pretty = true;
    }
    resp.setStatus(200);
    if (type.equalsIgnoreCase("yaml")) {
        resp.setContentType(APPLICATION_YAML);
        try (PrintWriter pw = resp.getWriter()) {
            pw.write(pretty ? ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputYamlMapper().writeValueAsString(oas));
        }
    } else {
        resp.setContentType(APPLICATION_JSON);
        try (PrintWriter pw = resp.getWriter()) {
            pw.write(pretty ? ctx.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputJsonMapper().writeValueAsString(oas));
        }
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SpecFilter(io.swagger.v3.core.filter.SpecFilter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext) PrintWriter(java.io.PrintWriter)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)9 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)8 SpecFilter (io.swagger.v3.core.filter.SpecFilter)8 ApiDescription (io.swagger.v3.core.model.ApiDescription)5 Components (io.swagger.v3.oas.models.Components)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)4 AbstractSpecFilter (io.swagger.v3.core.filter.AbstractSpecFilter)4 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)4 Info (io.swagger.v3.oas.models.info.Info)4 Test (org.testng.annotations.Test)4 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)3 Parameter (io.swagger.v3.oas.models.parameters.Parameter)3 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3 IOException (java.io.IOException)3 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)2 Operation (io.swagger.v3.oas.models.Operation)2 PathItem (io.swagger.v3.oas.models.PathItem)2