Search in sources :

Example 71 with Yaml

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

the class ReaderTest method testExampleWithRef.

@Test(description = "Example with ref")
public void testExampleWithRef() {
    Components components = new Components();
    components.addExamples("Id", new Example().description("Id Example").summary("Id Example").value("1"));
    OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
    Reader reader = new Reader(oas);
    OpenAPI openAPI = reader.read(RefExamplesResource.class);
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  description: info\n" + "paths:\n" + "  /example:\n" + "    post:\n" + "      description: subscribes a client to updates relevant to the requestor's account\n" + "      operationId: subscribe\n" + "      parameters:\n" + "      - name: subscriptionId\n" + "        in: path\n" + "        required: true\n" + "        style: simple\n" + "        schema:\n" + "          type: string\n" + "          description: Schema\n" + "          example: Subscription example\n" + "        examples:\n" + "          subscriptionId_1:\n" + "            summary: Subscription number 12345\n" + "            description: subscriptionId_1\n" + "            value: 12345\n" + "            externalValue: Subscription external value 1\n" + "            $ref: '#/components/examples/Id'\n" + "        example: example\n" + "      requestBody:\n" + "        content:\n" + "          '*/*':\n" + "            schema:\n" + "              type: integer\n" + "              format: int32\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                $ref: '#/components/schemas/SubscriptionResponse'\n" + "components:\n" + "  schemas:\n" + "    SubscriptionResponse:\n" + "      type: object\n" + "      properties:\n" + "        subscriptionId:\n" + "          type: string\n" + "  examples:\n" + "    Id:\n" + "      summary: Id Example\n" + "      description: Id Example\n" + "      value: \"1\"\n";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : Components(io.swagger.v3.oas.models.Components) Example(io.swagger.v3.oas.models.examples.Example) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 72 with Yaml

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

the class ASwaggerMavenIntegrationTest method runTest.

SwaggerMojo runTest(File pom, Consumer<OpenAPI> validator) throws Exception {
    assertNotNull(pom);
    assertTrue(pom.exists());
    SwaggerMojo swaggerMojo = (SwaggerMojo) lookupConfiguredMojo(pom, "resolve");
    // set random context id to not mix states with multiple tests
    swaggerMojo.setContextId(RandomStringUtils.randomAscii(32));
    assertNotNull(swaggerMojo);
    swaggerMojo.execute();
    final PlexusConfiguration config = extractPluginConfiguration("swagger-maven-plugin", pom);
    String outputPath = swaggerMojo.getOutputPath();
    String outputFile = config.getChild("outputFileName").getValue();
    if (outputFile == null) {
        outputFile = "openapi";
    }
    String format = config.getChild("outputFormat").getValue();
    if (format.toLowerCase().equals("yaml") || format.toLowerCase().equals("jsonandyaml")) {
        Path path = Paths.get(outputPath, outputFile + ".yaml");
        File file = path.toFile();
        assertTrue(Files.isRegularFile(path));
        String content = FileUtils.readFileToString(file, "UTF-8");
        final OpenAPI openAPI = Yaml.mapper().readValue(content, OpenAPI.class);
        assertNotNull(openAPI);
        validator.accept(openAPI);
    }
    if (format.toLowerCase().equals("json") || format.toLowerCase().equals("jsonandyaml")) {
        Path path = Paths.get(outputPath, outputFile + ".json");
        File file = path.toFile();
        assertTrue(Files.isRegularFile(path));
        String content = FileUtils.readFileToString(file, "UTF-8");
        final OpenAPI openAPI = Json.mapper().readValue(content, OpenAPI.class);
        assertNotNull(openAPI);
        validator.accept(openAPI);
    }
    return swaggerMojo;
}
Also used : Path(java.nio.file.Path) PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) File(java.io.File) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

Test (org.testng.annotations.Test)63 OpenAPI (io.swagger.v3.oas.models.OpenAPI)55 Schema (io.swagger.v3.oas.models.media.Schema)15 Components (io.swagger.v3.oas.models.Components)13 Info (io.swagger.v3.oas.models.info.Info)13 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)9 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)8 SpecFilter (io.swagger.v3.core.filter.SpecFilter)8 StringSchema (io.swagger.v3.oas.models.media.StringSchema)8 AfterTest (org.testng.annotations.AfterTest)6 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)5 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)4 AbstractSpecFilter (io.swagger.v3.core.filter.AbstractSpecFilter)4 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)4 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 IOException (java.io.IOException)4 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)3 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)3