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);
}
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));
}
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"));
}
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());
}
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);
}
Aggregations