use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-core by swagger-api.
the class MapPropertyDeserializerTest method testIssue1261InlineSchemaExample.
@Test(description = "it should read an example within an inlined schema")
public void testIssue1261InlineSchemaExample() throws Exception {
Operation operation = Yaml.mapper().readValue(" responses:\n" + " \"200\":\n" + " content:\n" + " '*/*':\n" + " description: OK\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int32\n" + " name:\n" + " type: string\n" + " required: [id, name]\n" + " example: ok", Operation.class);
ApiResponse response = operation.getResponses().get("200");
assertNotNull(response);
Schema schema = response.getContent().get("*/*").getSchema();
Object example = schema.getExample();
assertNotNull(example);
assertTrue(example instanceof String);
assertEquals(example, "ok");
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-core by swagger-api.
the class MapPropertyDeserializerTest method testMapDeserialization.
@Test(description = "it should deserialize a response per #1349")
public void testMapDeserialization() throws Exception {
Operation operation = Json.mapper().readValue(json, Operation.class);
ApiResponse response = operation.getResponses().get("200");
assertNotNull(response);
Schema responseSchema = response.getContent().get("*/*").getSchema();
assertNotNull(responseSchema);
assertTrue(responseSchema instanceof MapSchema);
MapSchema mp = (MapSchema) responseSchema;
assertTrue(mp.getAdditionalProperties() instanceof IntegerSchema);
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse 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.oas.annotations.responses.ApiResponse in project swagger-core by swagger-api.
the class SpecFilter method filterResponse.
protected ApiResponse filterResponse(OpenAPISpecFilter filter, Operation operation, ApiResponse response, String resourcePath, String key, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (response != null) {
ApiDescription description = new ApiDescription(resourcePath, key);
Optional<ApiResponse> filteredResponse = filter.filterResponse(response, operation, description, params, cookies, headers);
if (filteredResponse.isPresent()) {
return filteredResponse.get();
}
}
return null;
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testPathItemsRefSerialization.
@Test
public void testPathItemsRefSerialization() {
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/pathTest", new PathItem().$ref("#/components/pathItems/pathTest").description("This is a ref path item").summary("ref path item")).components(new Components().addPathItem("pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + " /pathTest:\n" + " $ref: '#/components/pathItems/pathTest'\n" + " description: This is a ref path item\n" + " summary: ref path item\n" + "components:\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" + " \"paths\" : {\n" + " \"/pathTest\" : {\n" + " \"summary\" : \"ref path item\",\n" + " \"description\" : \"This is a ref path item\",\n" + " \"$ref\" : \"#/components/pathItems/pathTest\"\n" + " }\n" + " },\n" + " \"components\" : {\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" + "}");
}
Aggregations