use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.
the class SwaggerSerializerTest method convertSpec.
@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
final Info info = new Info().version("1.0.0").title("Swagger Petstore");
final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
info.setContact(contact);
final Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "value");
info.addExtension("x-test2", map);
info.addExtension("x-test", "value");
final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
final String swaggerJson = Json.mapper().writeValueAsString(swagger);
Json.prettyPrint(swagger);
final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.
the class SwaggerSerializerTest method writeSpecWithParameterReferences.
@Test(description = "it should write a spec with parameter references")
public void writeSpecWithParameterReferences() throws IOException {
final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
final Info info = new Info().version("1.0.0").title("Swagger Petstore");
final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
info.setContact(contact);
final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel);
final QueryParameter parameter = (QueryParameter) new QueryParameter().name("id").description("a common get parameter").schema(new IntegerSchema());
final Operation get = new Operation().summary("finds pets in the system").description("a longer description").operationId("get pet by id").addParametersItem(new Parameter().$ref("#/parameters/Foo"));
swagger.components(new Components().addParameters("Foo", parameter)).path("/pets", new PathItem().get(get));
final String swaggerJson = Json.mapper().writeValueAsString(swagger);
final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
assertEquals(Json.pretty(rebuilt), Json.pretty(swagger));
}
use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.
the class SpecFilterTest method retainNonBrokenReferenceModelComposedProperties.
@Test(description = "it should retain non-broken reference model composed properties")
public void retainNonBrokenReferenceModelComposedProperties() throws IOException {
final OpenAPI openAPI = getOpenAPI(RESOURCE_REFERRED_SCHEMAS);
assertNotNull(openAPI.getComponents().getSchemas().get("User"));
final NoOpOperationsFilter noOperationsFilter = new NoOpOperationsFilter();
OpenAPI filtered = new SpecFilter().filter(openAPI, noOperationsFilter, null, null, null);
assertNotNull(filtered.getComponents().getSchemas().get("User"));
final RemoveUnreferencedDefinitionsFilter refFilter = new RemoveUnreferencedDefinitionsFilter();
filtered = new SpecFilter().filter(openAPI, refFilter, null, null, null);
assertNotNull(filtered.getComponents().getSchemas().get("User"));
assertNotNull(filtered.getComponents().getSchemas().get("Pet"));
}
use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.
the class OpenAPI3_1DeserializationTest method testRefDeserializationOnOAS31.
@Test
public void testRefDeserializationOnOAS31() throws IOException {
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_refs_siblings.yaml");
OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
assertEquals(openAPI.getPaths().get("/ref_pet").get$ref(), "#/components/pathItems/pet");
assertEquals(openAPI.getPaths().get("/ref_pet").getDescription(), "ref pathItem description");
assertEquals(openAPI.getPaths().get("/ref_pet").getSummary(), "ref pathItem summary");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(0).get$ref(), "#/components/parameters/testParameter");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(0).getDescription(), "ref parameter description");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getName(), "randomParam");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getIn(), "query");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").get$ref(), "#/components/examples/testExample");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").getDescription(), "ref example description");
assertEquals(openAPI.getPaths().get("/pets").getPost().getParameters().get(1).getExamples().get("refExample").getSummary(), "ref example summary");
assertEquals(openAPI.getPaths().get("/pets").getPost().getCallbacks().get("callIt").get$ref(), "#/components/callbacks/TestCallback");
assertEquals(openAPI.getPaths().get("/pets").getPost().getRequestBody().get$ref(), "#/components/requestBodies/body");
assertEquals(openAPI.getPaths().get("/pets").getPost().getRequestBody().getDescription(), "ref request body description");
assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("201").get$ref(), "#/components/responses/okResponse");
assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("201").getDescription(), "ref response description");
assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("default").getHeaders().get("head").get$ref(), "#/components/headers/head");
assertEquals(openAPI.getPaths().get("/pets").getPost().getResponses().get("default").getHeaders().get("head").getDescription(), "ref header description");
}
use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testSerializePetstore.
@Test
public void testSerializePetstore() throws Exception {
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.yaml");
final OpenAPI swagger = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(swagger);
assertEquals(swagger.getInfo().getLicense().getIdentifier(), "test");
SerializationMatchers.assertEqualsToYaml31(swagger, "openapi: 3.1.0\n" + "info:\n" + " title: Swagger Petstore\n" + " license:\n" + " name: MIT\n" + " identifier: test\n" + " version: 1.0.0\n" + "servers:\n" + "- url: http://petstore.swagger.io/v1\n" + "paths:\n" + " /pets:\n" + " get:\n" + " tags:\n" + " - pets\n" + " summary: List all pets\n" + " operationId: listPets\n" + " parameters:\n" + " - name: limit\n" + " in: query\n" + " description: How many items to return at one time (max 100)\n" + " required: false\n" + " schema:\n" + " type: integer\n" + " format: int32\n" + " responses:\n" + " \"200\":\n" + " description: An paged array of pets\n" + " headers:\n" + " x-next:\n" + " description: A link to the next page of responses\n" + " schema:\n" + " type: string\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Pets'\n" + " default:\n" + " description: unexpected error\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Error'\n" + " post:\n" + " tags:\n" + " - pets\n" + " summary: Create a pet\n" + " operationId: createPets\n" + " responses:\n" + " \"201\":\n" + " description: Null response\n" + " default:\n" + " description: unexpected error\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Error'\n" + " /pets/{petId}:\n" + " get:\n" + " tags:\n" + " - pets\n" + " summary: Info for a specific pet\n" + " operationId: showPetById\n" + " parameters:\n" + " - name: petId\n" + " in: path\n" + " description: The id of the pet to retrieve\n" + " required: true\n" + " schema:\n" + " type: string\n" + " responses:\n" + " \"200\":\n" + " description: Expected response to a valid request\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Pets'\n" + " default:\n" + " description: unexpected error\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Error'\n" + "components:\n" + " schemas:\n" + " Pet:\n" + " required:\n" + " - id\n" + " - name\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " name:\n" + " type:\n" + " - string\n" + " - integer\n" + " tag:\n" + " type: string\n" + " Pets:\n" + " type: array\n" + " items:\n" + " $ref: '#/components/schemas/Pet'\n" + " Error:\n" + " required:\n" + " - code\n" + " - message\n" + " properties:\n" + " code:\n" + " type: integer\n" + " format: int32\n" + " message:\n" + " type: string\n" + "webhooks:\n" + " newPet:\n" + " post:\n" + " requestBody:\n" + " description: Information about a new pet in the system\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Pet'\n" + " responses:\n" + " \"200\":\n" + " description: Return a 200 status to indicate that the data was received\n" + " successfully");
SerializationMatchers.assertEqualsToJson31(swagger, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"info\" : {\n" + " \"title\" : \"Swagger Petstore\",\n" + " \"license\" : {\n" + " \"name\" : \"MIT\",\n" + " \"identifier\" : \"test\"\n" + " },\n" + " \"version\" : \"1.0.0\"\n" + " },\n" + " \"servers\" : [ {\n" + " \"url\" : \"http://petstore.swagger.io/v1\"\n" + " } ],\n" + " \"paths\" : {\n" + " \"/pets\" : {\n" + " \"get\" : {\n" + " \"tags\" : [ \"pets\" ],\n" + " \"summary\" : \"List all pets\",\n" + " \"operationId\" : \"listPets\",\n" + " \"parameters\" : [ {\n" + " \"name\" : \"limit\",\n" + " \"in\" : \"query\",\n" + " \"description\" : \"How many items to return at one time (max 100)\",\n" + " \"required\" : false,\n" + " \"schema\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " }\n" + " } ],\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"An paged array of pets\",\n" + " \"headers\" : {\n" + " \"x-next\" : {\n" + " \"description\" : \"A link to the next page of responses\",\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Pets\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"default\" : {\n" + " \"description\" : \"unexpected error\",\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Error\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"post\" : {\n" + " \"tags\" : [ \"pets\" ],\n" + " \"summary\" : \"Create a pet\",\n" + " \"operationId\" : \"createPets\",\n" + " \"responses\" : {\n" + " \"201\" : {\n" + " \"description\" : \"Null response\"\n" + " },\n" + " \"default\" : {\n" + " \"description\" : \"unexpected error\",\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Error\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"/pets/{petId}\" : {\n" + " \"get\" : {\n" + " \"tags\" : [ \"pets\" ],\n" + " \"summary\" : \"Info for a specific pet\",\n" + " \"operationId\" : \"showPetById\",\n" + " \"parameters\" : [ {\n" + " \"name\" : \"petId\",\n" + " \"in\" : \"path\",\n" + " \"description\" : \"The id of the pet to retrieve\",\n" + " \"required\" : true,\n" + " \"schema\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " } ],\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"Expected response to a valid request\",\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Pets\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"default\" : {\n" + " \"description\" : \"unexpected error\",\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Error\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"components\" : {\n" + " \"schemas\" : {\n" + " \"Pet\" : {\n" + " \"required\" : [ \"id\", \"name\" ],\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int64\"\n" + " },\n" + " \"name\" : {\n" + " \"type\" : [\"string\", \"integer\"]\n" + " },\n" + " \"tag\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " },\n" + " \"Pets\" : {\n" + " \"type\" : \"array\",\n" + " \"items\" : {\n" + " \"$ref\" : \"#/components/schemas/Pet\"\n" + " }\n" + " },\n" + " \"Error\" : {\n" + " \"required\" : [ \"code\", \"message\" ],\n" + " \"properties\" : {\n" + " \"code\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " },\n" + " \"message\" : {\n" + " \"type\" : \"string\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"webhooks\" : {\n" + " \"newPet\" : {\n" + " \"post\" : {\n" + " \"requestBody\" : {\n" + " \"description\" : \"Information about a new pet in the system\",\n" + " \"content\" : {\n" + " \"application/json\" : {\n" + " \"schema\" : {\n" + " \"$ref\" : \"#/components/schemas/Pet\"\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"Return a 200 status to indicate that the data was received successfully\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}");
}
Aggregations