Search in sources :

Example 1 with ApiResponse

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

the class OpenAPIDeserializerTest method testPathsWithRefResponse.

@Test
public void testPathsWithRefResponse() {
    String json = "{\n" + "  \"openapi\": \"3.0.0\",\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"get\": {\n" + "        \"responses\": {\n" + "          \"200\": {\n" + "            \"$ref\": \"#/components/responses/OK\"" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().containsKey("200"));
    assertEquals(ApiResponse.class, operation.getResponses().get("200").getClass());
    ApiResponse refResponse = operation.getResponses().get("200");
    assertEquals("#/components/responses/OK", refResponse.get$ref());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 2 with ApiResponse

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

the class NetworkReferenceTest method testIssue411.

@Test
public void testIssue411() throws Exception {
    final List<AuthorizationValue> auths = new ArrayList<>();
    AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
    auths.add(auth);
    new Expectations() {

        {
            remoteUrl.urlToString("http://remote1/resources/swagger.yaml", auths);
            result = issue_411_server;
            remoteUrl.urlToString("http://remote2/resources/foo", auths);
            result = issue_411_components;
        }
    };
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);
    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths().get("/health"));
    PathItem health = swagger.getPaths().get("/health");
    assertTrue(health.getGet().getParameters().size() == 0);
    Schema responseRef = health.getGet().getResponses().get("200").getContent().get("*/*").getSchema();
    assertTrue(responseRef.get$ref() != null);
    assertEquals(responseRef.get$ref(), "#/components/schemas/Success");
    assertNotNull(swagger.getComponents().getSchemas().get("Success"));
    Parameter param = swagger.getPaths().get("/stuff").getGet().getParameters().get(0);
    assertEquals(param.getIn(), "query");
    assertEquals(param.getName(), "skip");
    ApiResponse response = swagger.getPaths().get("/stuff").getGet().getResponses().get("200");
    assertNotNull(response);
    assertTrue(response.getContent().get("*/*").getSchema() instanceof StringSchema);
    ApiResponse error = swagger.getPaths().get("/stuff").getGet().getResponses().get("400");
    assertNotNull(error);
    Schema errorProp = error.getContent().get("*/*").getSchema();
    assertNotNull(errorProp);
    assertTrue(errorProp.get$ref() != null);
    assertEquals(errorProp.get$ref(), "#/components/schemas/Error");
    assertTrue(swagger.getComponents().getSchemas().get("Error") instanceof Schema);
}
Also used : Expectations(mockit.Expectations) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ArrayList(java.util.ArrayList) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) PathItem(io.swagger.v3.oas.models.PathItem) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 3 with ApiResponse

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

the class InlineModelResolverTest method testArbitraryObjectResponseArrayInline.

@Test
public void testArbitraryObjectResponseArrayInline() {
    OpenAPI openAPI = new OpenAPI();
    ArraySchema arraySchema = new ArraySchema();
    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("arbitrary", new ObjectSchema());
    arraySchema.items(objectSchema);
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.description("it works!");
    apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema)));
    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();
    assertTrue(responseProperty instanceof ArraySchema);
    ArraySchema arraySchema1 = (ArraySchema) responseProperty;
    Schema items = arraySchema1.getItems();
    assertNotNull(items);
    assertEquals("#/components/schemas/inline_response_200", items.get$ref());
    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);
    Schema inlineProp = (Schema) inline.getProperties().get("arbitrary");
    assertNotNull(inlineProp);
    assertTrue(inlineProp instanceof ObjectSchema);
    assertNull(inlineProp.getProperties());
}
Also used : 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) 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) 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 4 with ApiResponse

use of io.swagger.v3.oas.models.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 5 with ApiResponse

use of io.swagger.v3.oas.models.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)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)100 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 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 Path (javax.ws.rs.Path)17