Search in sources :

Example 21 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testInlineMapResponse.

@Test
public void testInlineMapResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    Schema schema = new Schema();
    schema.setAdditionalProperties(new StringSchema());
    schema.addExtension("x-ext", "ext-prop");
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.description("it works!");
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);
    Content content = new Content();
    content.addMediaType("*/*", mediaType);
    apiResponse.setContent(content);
    apiResponse.addExtension("x-foo", "bar");
    ApiResponses apiResponses = new ApiResponses();
    apiResponses.addApiResponse("200", apiResponse);
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(apiResponses)));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 22 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testInlineResponseModelWithTitle.

@Test
public void testInlineResponseModelWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    String responseTitle = "GetBarResponse";
    StringSchema stringSchema1 = new StringSchema();
    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.setTitle(responseTitle);
    objectSchema1.addProperties("name", stringSchema1);
    MediaType mediaType1 = new MediaType();
    mediaType1.setSchema(objectSchema1);
    Content content1 = new Content();
    content1.addMediaType("*/*", mediaType1);
    ApiResponse response1 = new ApiResponse();
    response1.setDescription("it works!");
    response1.setContent(content1);
    ApiResponses responses1 = new ApiResponses();
    responses1.addApiResponse("200", response1);
    Operation operation1 = new Operation();
    operation1.setResponses(responses1);
    PathItem pathItem1 = new PathItem();
    pathItem1.setGet(operation1);
    openAPI.path("/foo/bar", pathItem1);
    StringSchema stringSchema2 = new StringSchema();
    ObjectSchema objectSchema2 = new ObjectSchema();
    objectSchema2.addProperties("name", stringSchema2);
    objectSchema2.addExtension("x-foo", "bar");
    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);
    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2);
    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");
    response2.setContent(content2);
    ApiResponses responses2 = new ApiResponses();
    responses2.addApiResponse("200", response2);
    Operation operation2 = new Operation();
    operation2.setResponses(responses2);
    PathItem pathItem2 = new PathItem();
    pathItem2.setGet(operation2);
    openAPI.path("/foo/baz", pathItem2);
    new InlineModelResolver().flatten(openAPI);
    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();
    ApiResponse response = responses.get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema().get$ref() != null);
    Schema model = openAPI.getComponents().getSchemas().get(responseTitle);
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) 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) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 23 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineArrayResponseWithTitle.

@Test
public void resolveInlineArrayResponseWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.description("it works!");
    Map<String, Schema> properties = new HashMap<>();
    properties.put("name", new StringSchema());
    apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new ObjectSchema().title("FooBar").properties(properties)))));
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", apiResponse))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(response);
    assertNotNull(response.getContent().get("*/*").getSchema());
    Schema responseProperty = response.getContent().get("*/*").getSchema();
    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);
    ArraySchema ap = (ArraySchema) responseProperty;
    Schema p = ap.getItems();
    assertNotNull(p);
    assertEquals(p.get$ref(), "#/components/schemas/" + "FooBar");
    Schema inline = openAPI.getComponents().getSchemas().get("FooBar");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
Also used : HashMap(java.util.HashMap) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) 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) PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 24 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method doRelativeFileTest.

private OpenAPI doRelativeFileTest(String location) {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult readResult = parser.readLocation(location, null, options);
    if (readResult.getMessages().size() > 0) {
        Json.prettyPrint(readResult.getMessages());
    }
    final OpenAPI openAPI = readResult.getOpenAPI();
    final PathItem path = openAPI.getPaths().get("/health");
    // we successfully converted the RefPath to a Path
    assertEquals(path.getClass(), PathItem.class);
    final List<Parameter> parameters = path.getParameters();
    assertParamDetails(parameters, 0, QueryParameter.class, "param1", "query");
    assertParamDetails(parameters, 1, HeaderParameter.class, "param2", "header");
    final Operation operation = path.getGet();
    final List<Parameter> operationParams = operation.getParameters();
    assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
    assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");
    final Map<String, ApiResponse> responsesMap = operation.getResponses();
    assertResponse(openAPI, responsesMap, "200", "application/json", "Health information from the server", "#/components/schemas/health");
    assertResponse(openAPI, responsesMap, "400", "*/*", "Your request was not valid", "#/components/schemas/error");
    assertResponse(openAPI, responsesMap, "500", "*/*", "An unexpected error occur during processing", "#/components/schemas/error");
    final Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    final Schema refInDefinitions = definitions.get("refInDefinitions");
    assertEquals(refInDefinitions.getDescription(), "The example model");
    expectedPropertiesInModel(refInDefinitions, "foo", "bar");
    final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel");
    final Schema arrayModelItems = arrayModel.getItems();
    assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo");
    final Schema fooModel = definitions.get("foo");
    assertEquals(fooModel.getDescription(), "Just another model");
    expectedPropertiesInModel(fooModel, "hello", "world");
    final ComposedSchema composedCat = (ComposedSchema) definitions.get("composedCat");
    final Schema child = composedCat.getAllOf().get(2);
    expectedPropertiesInModel(child, "huntingSkill", "prop2", "reflexes", "reflexMap");
    final ArraySchema reflexes = (ArraySchema) child.getProperties().get("reflexes");
    final Schema reflexItems = reflexes.getItems();
    assertEquals(reflexItems.get$ref(), "#/components/schemas/reflex");
    assertTrue(definitions.containsKey(reflexItems.get$ref().substring(reflexItems.get$ref().lastIndexOf("/") + 1)));
    final Schema reflexMap = (Schema) child.getProperties().get("reflexMap");
    final Schema reflexMapAdditionalProperties = (Schema) reflexMap.getAdditionalProperties();
    assertEquals(reflexMapAdditionalProperties.get$ref(), "#/components/schemas/reflex");
    assertEquals(composedCat.getAllOf().size(), 3);
    assertEquals(composedCat.getAllOf().get(0).get$ref(), "#/components/schemas/pet");
    assertEquals(composedCat.getAllOf().get(1).get$ref(), "#/components/schemas/foo_1");
    return openAPI;
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 25 with ApiResponse

use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method assertResponse.

private void assertResponse(OpenAPI openAPI, Map<String, ApiResponse> responsesMap, String responseCode, String mediaType, String expectedDescription, String expectedSchemaRef) {
    final ApiResponse response = responsesMap.get(responseCode);
    final Schema schema = response.getContent().get(mediaType).getSchema();
    assertEquals(response.getDescription(), expectedDescription);
    assertEquals(schema.getClass(), Schema.class);
    assertEquals(schema.get$ref(), expectedSchemaRef);
    assertTrue(openAPI.getComponents().getSchemas().containsKey(schema.get$ref().substring(schema.get$ref().lastIndexOf("/") + 1)));
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)113 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)99 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)84 Test (org.testng.annotations.Test)53 Operation (io.swagger.v3.oas.models.Operation)50 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)48 OpenAPI (io.swagger.v3.oas.models.OpenAPI)47 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)43 PathItem (io.swagger.v3.oas.models.PathItem)39 Schema (io.swagger.v3.oas.models.media.Schema)38 MediaType (io.swagger.v3.oas.models.media.MediaType)31 Path (javax.ws.rs.Path)30 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)29 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)27 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)25 Content (io.swagger.v3.oas.models.media.Content)25 ArrayList (java.util.ArrayList)24 Components (io.swagger.v3.oas.models.Components)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)18