use of io.swagger.v3.oas.models.responses.ApiResponse in project swagger-core by swagger-api.
the class SecurityDefinitionTest method createModelWithSecurityRequirements.
@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() 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 OpenAPI oas = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
oas.schemaRequirement("githubAccessCode", new SecurityScheme().flows(new OAuthFlows().authorizationCode(new OAuthFlow().scopes(new Scopes().addString("user:email", "Grants read access to a user’s email addresses.")))));
final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id");
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("*/*", new MediaType().schema(new Schema().$ref("Person"))));
final ApiResponse errorResponse = new ApiResponse().description("error response").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Error"))));
get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse)).addSecurityItem(new SecurityRequirement().addList("internal_oauth2", "user:email")).addSecurityItem(new SecurityRequirement().addList("api_key"));
oas.path("/pets", new PathItem().get(get));
final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
SerializationMatchers.assertEqualsToJson(oas, json);
}
use of io.swagger.v3.oas.models.responses.ApiResponse in project swagger-core by swagger-api.
the class OpenAPI3_1SerializationTest method testResponseRefSerialization.
@Test
public void testResponseRefSerialization() {
OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/test", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("point to a $ref response").$ref("#/components/responses/okResponse"))))).components(new Components().addResponses("okResponse", new ApiResponse().description("everything is good")));
SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + " /test:\n" + " description: test path item\n" + " get:\n" + " operationId: testPathItem\n" + " responses:\n" + " \"200\":\n" + " description: point to a $ref response\n" + " $ref: '#/components/responses/okResponse'\n" + "components:\n" + " responses:\n" + " okResponse:\n" + " description: everything is good");
SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + " \"openapi\" : \"3.1.0\",\n" + " \"paths\" : {\n" + " \"/test\" : {\n" + " \"description\" : \"test path item\",\n" + " \"get\" : {\n" + " \"operationId\" : \"testPathItem\",\n" + " \"responses\" : {\n" + " \"200\" : {\n" + " \"description\" : \"point to a $ref response\",\n" + " \"$ref\" : \"#/components/responses/okResponse\"\n" + " }\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"components\" : {\n" + " \"responses\" : {\n" + " \"okResponse\" : {\n" + " \"description\" : \"everything is good\"\n" + " }\n" + " }\n" + " }\n" + "}");
}
use of io.swagger.v3.oas.models.responses.ApiResponse in project swagger-core by swagger-api.
the class ReaderTest method testResponseWithFilter.
@Test(description = "Responses with filter")
public void testResponseWithFilter() {
Components components = new Components();
components.addResponses("invalidJWT", new ApiResponse().description("when JWT token invalid/expired"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(SimpleResponsesResource.class);
OpenAPISpecFilter filterImpl = new RefResponseFilter();
SpecFilter f = new SpecFilter();
openAPI = f.filter(openAPI, filterImpl, null, null, null);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /:\n" + " get:\n" + " summary: Simple get operation\n" + " description: Defines a simple get operation with no inputs and a complex output\n" + " object\n" + " operationId: getWithPayloadResponse\n" + " responses:\n" + " \"200\":\n" + " description: voila!\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/SampleResponseSchema'\n" + " default:\n" + " description: boo\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/GenericError'\n" + " \"401\":\n" + " $ref: '#/components/responses/invalidJWT'\n" + " deprecated: true\n" + "components:\n" + " schemas:\n" + " GenericError:\n" + " type: object\n" + " SampleResponseSchema:\n" + " type: object\n" + " responses:\n" + " invalidJWT:\n" + " description: when JWT token invalid/expired";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.responses.ApiResponse in project swagger-core by swagger-api.
the class DecoratorExtensionTest method scanSimpleResourceWithoutDecorator.
/**
* Test for method annotated without vendor annotation.
*/
@Test(description = "scan a simple resource without custom decorator")
public void scanSimpleResourceWithoutDecorator() {
final OpenAPI openAPI = getOpenAPI(SimpleResourceWithVendorAnnotation.class);
Assert.assertEquals(openAPI.getPaths().size(), 2);
final Operation get = getGet(openAPI, "/{id}/value");
Assert.assertNotNull(get);
final ApiResponse response = get.getResponses().get(RESPONSE_STATUS_401);
Assert.assertNull(response);
}
use of io.swagger.v3.oas.models.responses.ApiResponse in project swagger-core by swagger-api.
the class ReaderTest method testResponseWithRef.
@Test(description = "Responses with ref")
public void testResponseWithRef() {
Components components = new Components();
components.addResponses("invalidJWT", new ApiResponse().description("when JWT token invalid/expired"));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(RefResponsesResource.class);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /:\n" + " get:\n" + " summary: Simple get operation\n" + " description: Defines a simple get operation with no inputs and a complex output\n" + " object\n" + " operationId: getWithPayloadResponse\n" + " responses:\n" + " \"200\":\n" + " description: voila!\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/SampleResponseSchema'\n" + " default:\n" + " description: boo\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/GenericError'\n" + " \"401\":\n" + " $ref: '#/components/responses/invalidJWT'\n" + " deprecated: true\n" + "components:\n" + " schemas:\n" + " GenericError:\n" + " type: object\n" + " SampleResponseSchema:\n" + " type: object\n" + " responses:\n" + " invalidJWT:\n" + " description: when JWT token invalid/expired";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
Aggregations