Search in sources :

Example 21 with Example

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

the class ArrayPropertyDeserializerTest method testArrayDeserialization.

@Test(description = "it should includes the example in the arrayproperty")
public void testArrayDeserialization() throws Exception {
    Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
    ApiResponse response = operation.getResponses().get("200");
    assertNotNull(response);
    MediaType media = response.getContent().get("*/*");
    Schema responseSchema = media.getSchema();
    assertTrue(media.getExamples().size() == 2);
    assertNotNull(responseSchema);
    assertTrue(responseSchema instanceof ArraySchema);
    ArraySchema mp = (ArraySchema) responseSchema;
    assertEquals(mp.getMinItems(), new Integer(3));
    assertEquals(mp.getMaxItems(), new Integer(100));
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 22 with Example

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

the class ModelExampleTest method createModelWithExample.

@Test(description = "it should create a model with example")
public void createModelWithExample() {
    ObjectSchema model = new ObjectSchema();
    model.addProperties("name", new StringSchema().example("Tony"));
    model.addProperties("id", new IntegerSchema().example(123));
    model.example("{\"name\":\"Fred\",\"id\":123456\"}");
    assertEquals(model.getExample(), "{\"name\":\"Fred\",\"id\":123456\"}");
}
Also used : ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 23 with Example

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

the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.

@Test
public void testComponentPathItemsSerialization() {
    Schema schema = new StringSchema();
    schema.addType(schema.getType());
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema: {}\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem\n" + "  pathItems:\n" + "    /pathTest:\n" + "      description: test path item\n" + "      get:\n" + "        operationId: testPathItem\n" + "        responses:\n" + "          \"200\":\n" + "            description: response description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : { }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"pathItems\" : {\n" + "      \"/pathTest\" : {\n" + "        \"description\" : \"test path item\",\n" + "        \"get\" : {\n" + "          \"operationId\" : \"testPathItem\",\n" + "          \"responses\" : {\n" + "            \"200\" : {\n" + "              \"description\" : \"response description\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
    openAPI.openapi("3.0.3");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema:\n" + "            type: object\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem");
    SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + "  \"openapi\" : \"3.0.3\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : {\n" + "              \"type\" : \"object\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) 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) Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 24 with Example

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

the class ResponseExamplesTest method createResponse.

@Test(description = "it should create a response")
public void createResponse() throws IOException {
    final ApiResponse response = new ApiResponse().content(new Content().addMediaType("application/json", new MediaType().addExamples("test", new Example().value("{\"name\":\"Fred\",\"id\":123456\"}"))));
    final String json = "{\n" + "  \"content\" : {\n" + "    \"application/json\" : {\n" + "      \"examples\" : {\n" + "        \"test\" : {\n" + "          \"value\" : \"{\\\"name\\\":\\\"Fred\\\",\\\"id\\\":123456\\\"}\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    SerializationMatchers.assertEqualsToJson(response, json);
}
Also used : Content(io.swagger.v3.oas.models.media.Content) Example(io.swagger.v3.oas.models.examples.Example) MediaType(io.swagger.v3.oas.models.media.MediaType) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 25 with Example

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

the class Ticket3365Test method testTicket3365.

@Test
public void testTicket3365() {
    Schema model = context.resolve(new AnnotatedType(ExampleWithJson.class));
    assertNotNull(model);
    SerializationMatchers.assertEqualsToYaml(model, "required:\n" + "- param1\n" + "type: object\n" + "properties:\n" + "  param1:\n" + "    title: Cron formatted invoice schedule\n" + "    type: object\n" + "    example:\n" + "      type: array\n" + "      items:\n" + "        type: string\n" + "        format: byte");
    model = context.resolve(new AnnotatedType(ExampleStartingWithInteger.class));
    assertNotNull(model);
    SerializationMatchers.assertEqualsToYaml(model, "required:\n" + "- param1\n" + "type: object\n" + "properties:\n" + "  param1:\n" + "    title: Cron formatted invoice schedule\n" + "    type: string\n" + "    example: 0 0 5 * *\n");
}
Also used : AnnotatedType(io.swagger.v3.core.converter.AnnotatedType) Schema(io.swagger.v3.oas.models.media.Schema) 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