Search in sources :

Example 16 with Example

use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.

the class ReaderTest method testTicket3624.

@Test(description = "Optional handling")
public void testTicket3624() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Service.class);
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /example/model:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve models for display to the user'\n" + "      operationId: getModels\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Response'\n" + "  /example/model/by/ids:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve models by their ids'\n" + "      operationId: getModelsById\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/ByIdResponse'\n" + "  /example/containerized/model:\n" + "    get:\n" + "      tags:\n" + "      - ExampleService\n" + "      summary: ' Retrieve review insights for a specific product'\n" + "      operationId: getContainerizedModels\n" + "      responses:\n" + "        default:\n" + "          description: default response\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/ContainerizedResponse'\n" + "components:\n" + "  schemas:\n" + "    Model:\n" + "      type: object\n" + "      properties:\n" + "        text:\n" + "          type: string\n" + "        title:\n" + "          type: string\n" + "        active:\n" + "          type: boolean\n" + "        schemaParent:\n" + "          $ref: '#/components/schemas/Model'\n" + "        optionalString:\n" + "          type: string\n" + "        parent:\n" + "          $ref: '#/components/schemas/Model'\n" + "        id:\n" + "          type: integer\n" + "          format: int32\n" + "    Response:\n" + "      type: object\n" + "      properties:\n" + "        count:\n" + "          type: integer\n" + "          format: int32\n" + "        models:\n" + "          type: array\n" + "          items:\n" + "            $ref: '#/components/schemas/Model'\n" + "    ByIdResponse:\n" + "      type: object\n" + "      properties:\n" + "        modelsById:\n" + "          type: object\n" + "          additionalProperties:\n" + "            $ref: '#/components/schemas/Model'\n" + "    ContainerizedResponse:\n" + "      type: object\n" + "      properties:\n" + "        totalCount:\n" + "          type: integer\n" + "          format: int32\n" + "        containerizedModels:\n" + "          type: array\n" + "          items:\n" + "            $ref: '#/components/schemas/ModelContainer'\n" + "    ModelContainer:\n" + "      type: object\n" + "      properties:\n" + "        text:\n" + "          type: string\n" + "        model:\n" + "          $ref: '#/components/schemas/Model'\n" + "        id:\n" + "          type: integer\n" + "          format: int32";
    SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 17 with Example

use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.

the class SimpleBuilderTest method testBuilder.

@Test
public void testBuilder() throws Exception {
    // basic metadata
    OpenAPI oai = new OpenAPI().info(new Info().contact(new Contact().email("tony@eatbacon.org").name("Tony the Tam").url("https://foo.bar"))).externalDocs(new ExternalDocumentation().description("read more here").url("http://swagger.io")).addTagsItem(new Tag().name("funky dunky").description("all about neat things")).extensions(new HashMap<String, Object>() {

        {
            put("x-fancy-extension", "something");
        }
    });
    Map<String, Schema> schemas = new HashMap<>();
    schemas.put("StringSchema", new StringSchema().description("simple string schema").minLength(3).maxLength(100).example("it works"));
    schemas.put("IntegerSchema", new IntegerSchema().description("simple integer schema").multipleOf(new BigDecimal(3)).minimum(new BigDecimal(6)));
    oai.components(new Components().schemas(schemas));
    schemas.put("Address", new Schema().description("address object").addProperties("street", new StringSchema().description("the street number")).addProperties("city", new StringSchema().description("city")).addProperties("state", new StringSchema().description("state").minLength(2).maxLength(2)).addProperties("zip", new StringSchema().description("zip code").pattern("^\\d{5}(?:[-\\s]\\d{4})?$").minLength(2).maxLength(2)).addProperties("country", new StringSchema()._enum(new ArrayList<String>() {

        {
            this.add("US");
        }
    })).description("2-digit country code").minLength(2).maxLength(2));
    oai.paths(new Paths().addPathItem("/foo", new PathItem().description("the foo path").get(new Operation().addParametersItem(new QueryParameter().description("Records to skip").required(false).schema(new IntegerSchema())).responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it worked").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("#/components/schemas/Address")))).addLink("funky", new Link().operationId("getFunky")))))));
    System.out.println(writeJson(oai));
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Info(io.swagger.v3.oas.models.info.Info) BigDecimal(java.math.BigDecimal) Contact(io.swagger.v3.oas.models.info.Contact) Content(io.swagger.v3.oas.models.media.Content) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) Test(org.testng.annotations.Test)

Example 18 with Example

use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.

the class JsonDeserializationTest method deserializeDateExample.

@Test
public void deserializeDateExample() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/swos-126.yaml");
    final OpenAPI swagger = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(swagger);
    Map<String, Schema> props = swagger.getComponents().getSchemas().get("MyModel").getProperties();
    assertTrue(Yaml.pretty().writeValueAsString(props.get("date")).contains("example: 2019-08-05"));
    assertTrue(Yaml.pretty().writeValueAsString(props.get("dateTime")).contains("example: 2019-08-05T12:34:56Z"));
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 19 with Example

use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS31.

@Test
public void testDeserializationOnOAS31() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertEquals(openAPI.getInfo().getSummary(), "petstore sample for OAS 3.1.0");
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test");
    assertNotNull(openAPI.getWebhooks());
    assertFalse(openAPI.getWebhooks().isEmpty());
    assertNotNull(openAPI.getWebhooks().get("newPet"));
    assertNotNull(openAPI.getWebhooks().get("newPet").getPost());
    assertNotNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet"));
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getDescription(), "get a pet");
    assertNotNull(openAPI.getComponents().getPathItems().get("/pet").getGet());
    assertEquals(openAPI.getComponents().getPathItems().get("/pet").getGet().getOperationId(), "getPet");
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions().get("x-test-extension"), "extended");
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertNull(openAPI.getInfo().getSummary());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 20 with Example

use of io.swagger.v3.oas.models.examples.Example in project swagger-core by swagger-api.

the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS30.

@Test
public void testDeserializationOnOAS30() throws IOException {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
    OpenAPI openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
    assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
    assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
    assertNull(openAPI.getInfo().getSummary());
    assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
    assertNull(openAPI.getInfo().getLicense().getIdentifier());
    assertNull(openAPI.getWebhooks());
    assertNull(openAPI.getComponents().getPathItems());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
    assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
    assertNotNull(openAPI.getComponents().getResponses());
    assertNotNull(openAPI.getComponents().getResponses().get("201"));
    assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
    assertNotNull(openAPI.getComponents().getParameters());
    assertNotNull(openAPI.getComponents().getParameters().get("param"));
    assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
    assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
    assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
    assertNotNull(openAPI.getComponents().getExamples());
    assertNotNull(openAPI.getComponents().getExamples().get("example"));
    assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
    assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
    assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
    assertNotNull(openAPI.getComponents().getRequestBodies());
    assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
    assertNotNull(openAPI.getComponents().getHeaders());
    assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
    assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
    assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
    assertNotNull(openAPI.getComponents().getLinks());
    assertNotNull(openAPI.getComponents().getLinks().get("Link"));
    assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
    assertNotNull(openAPI.getComponents().getCallbacks());
    assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
    openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(openAPI);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)28 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Schema (io.swagger.v3.oas.models.media.Schema)12 Components (io.swagger.v3.oas.models.Components)9 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)9 Info (io.swagger.v3.oas.models.info.Info)8 StringSchema (io.swagger.v3.oas.models.media.StringSchema)7 Example (io.swagger.v3.oas.models.examples.Example)6 Content (io.swagger.v3.oas.models.media.Content)6 MediaType (io.swagger.v3.oas.models.media.MediaType)6 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 Operation (io.swagger.v3.oas.models.Operation)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)3 PathItem (io.swagger.v3.oas.models.PathItem)3 Link (io.swagger.v3.oas.models.links.Link)3 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)3