Search in sources :

Example 86 with Yaml

use of io.swagger.v3.core.util.Yaml 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 87 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class ReaderTest method testTicket2340.

@Test(description = "Responses with array schema")
public void testTicket2340() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Ticket2340Resource.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /test/test:\n" + "    post:\n" + "      operationId: getAnimal\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/Animal'\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                type: string\n" + "components:\n" + "  schemas:\n" + "    Animal:\n" + "      required:\n" + "      - type\n" + "      type: object\n" + "      properties:\n" + "        type:\n" + "          type: string\n" + "      discriminator:\n" + "        propertyName: type\n" + "    Cat:\n" + "      type: object\n" + "      allOf:\n" + "      - $ref: '#/components/schemas/Animal'\n" + "      - type: object\n" + "        properties:\n" + "          lives:\n" + "            type: integer\n" + "            format: int32\n" + "    Dog:\n" + "      type: object\n" + "      allOf:\n" + "      - $ref: '#/components/schemas/Animal'\n" + "      - type: object\n" + "        properties:\n" + "          barkVolume:\n" + "            type: number\n" + "            format: double\n";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 88 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class ReaderTest method testTicket3587.

@Test(description = "Parameter examples ordering")
public void testTicket3587() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Ticket3587Resource.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /test/test:\n" + "    get:\n" + "      operationId: parameterExamplesOrderingTest\n" + "      parameters:\n" + "      - in: query\n" + "        schema:\n" + "          type: string\n" + "        examples:\n" + "          Example One:\n" + "            description: Example One\n" + "          Example Two:\n" + "            description: Example Two\n" + "          Example Three:\n" + "            description: Example Three\n" + "      - in: query\n" + "        schema:\n" + "          type: string\n" + "        examples:\n" + "          Example Three:\n" + "            description: Example Three\n" + "          Example Two:\n" + "            description: Example Two\n" + "          Example One:\n" + "            description: Example One\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*': {}";
    SerializationMatchers.assertEqualsToYamlExact(openAPI, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 89 with Yaml

use of io.swagger.v3.core.util.Yaml 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)

Example 90 with Yaml

use of io.swagger.v3.core.util.Yaml in project swagger-core by swagger-api.

the class ReaderTest method testTicket3731.

@Test(description = "Constraints annotations in models")
public void testTicket3731() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Ticket3731Resource.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /test/cart:\n" + "    get:\n" + "      summary: Get cart items\n" + "      description: Paging follows RFC 5005.\n" + "      operationId: getCart\n" + "      parameters:\n" + "      - name: pageSize\n" + "        in: query\n" + "        description: \"Number of items per page. Range[1, 200]\"\n" + "        schema:\n" + "          maximum: 200\n" + "          minimum: 1\n" + "          type: integer\n" + "          format: int32\n" + "          default: 50\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: array\n" + "                items:\n" + "                  type: string\n";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
    reader = new Reader(new OpenAPI());
    openAPI = reader.read(Ticket3731BisResource.class);
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : Ticket3731BisResource(io.swagger.v3.jaxrs2.resources.Ticket3731BisResource) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)188 OpenAPI (io.swagger.v3.oas.models.OpenAPI)151 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)121 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)94 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)62 Schema (io.swagger.v3.oas.models.media.Schema)58 StringSchema (io.swagger.v3.oas.models.media.StringSchema)49 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)47 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)43 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)42 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)38 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)35 MapSchema (io.swagger.v3.oas.models.media.MapSchema)33 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)25 DateSchema (io.swagger.v3.oas.models.media.DateSchema)25 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)25 Parameter (io.swagger.v3.oas.models.parameters.Parameter)18 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)18 Components (io.swagger.v3.oas.models.Components)15 Info (io.swagger.v3.oas.models.info.Info)14