Java Code Examples for io.swagger.v3.oas.models.responses.ApiResponse#addExtension()

The following examples show how to use io.swagger.v3.oas.models.responses.ApiResponse#addExtension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 2
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineMapResponseWithObjectSchema() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    Schema schema = new Schema();
    schema.setAdditionalProperties(new ObjectSchema()
            .addProperties("name", new StringSchema()));
    schema.addExtension("x-ext", "ext-prop");

    ApiResponse apiResponse = new ApiResponse()
            .description("it works!")
            .content(new Content().addMediaType("*/*",new MediaType().schema(schema)));
    apiResponse.addExtension("x-foo", "bar");

    ApiResponses apiResponses = new 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);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
    assertTrue(openAPI.getComponents().getSchemas().size() == 1);

    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_map200");
    assertTrue(inline instanceof Schema);
    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 3
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayResponse() {
    OpenAPI openAPI = new OpenAPI();


    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.addProperties("name", new StringSchema());
    ArraySchema schema = new ArraySchema();
    schema.setItems(objectSchema);

    ApiResponse apiResponse = new ApiResponse();
    apiResponse.addExtension("x-foo", "bar");
    apiResponse.setDescription("it works!");
    apiResponse.setContent(new Content().addMediaType("*/*", new MediaType().schema(schema)));

    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");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);

    ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = am.getItems();
    assertTrue(items.get$ref() != null);

    assertEquals(items.get$ref(), "#/components/schemas/inline_response_200");


    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(inline instanceof Schema);

    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 4
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testInlineResponseModel() throws Exception {
    OpenAPI openAPI = new OpenAPI();


    StringSchema stringSchema1 = new StringSchema();

    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.addProperties("name", stringSchema1);
    objectSchema1.addExtension("x-ext", "ext-prop");

    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-ext", "ext-prop");
    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);

    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2 );

    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");
    response2.addExtension("x-foo","bar");
    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);

    Schema schema = response.getContent().get("*/*").getSchema();
    assertTrue(schema.get$ref() != null);
    assertEquals(1, schema.getExtensions().size());
    assertEquals("ext-prop", schema.getExtensions().get("x-ext"));

    Schema model = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
 
Example 5
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineArrayResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();

    ObjectSchema items = new ObjectSchema();
    items.addExtension("x-ext", "ext-items");
    items.addProperties("name", new StringSchema());


    ArraySchema schema = new ArraySchema()
            .items(items);
    schema.addExtension("x-ext", "ext-prop");

    ApiResponse response  = new ApiResponse();
    response.addExtension("x-foo", "bar");
    response.description("it works!");
    response.content(new Content().addMediaType("*/*", new MediaType().schema(schema)));

    openAPI.path("/foo/baz", new PathItem()
            .get(new Operation()
                    .responses(new ApiResponses().addApiResponse("200",response))));

    new InlineModelResolver().flatten(openAPI);

    ApiResponse apiResponse = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertNotNull(apiResponse);

    assertNotNull(apiResponse.getContent().get("*/*").getSchema());
    Schema responseProperty = apiResponse.getContent().get("*/*").getSchema();

    // no need to flatten more
    assertTrue(responseProperty instanceof ArraySchema);

    ArraySchema ap = (ArraySchema) responseProperty;
    assertEquals(1, ap.getExtensions().size());
    assertEquals("ext-prop", ap.getExtensions().get("x-ext"));

    Schema p = ap.getItems();

    assertNotNull(p);

    assertEquals("#/components/schemas/inline_response_200", p.get$ref());

    assertEquals(1, p.getExtensions().size());
    assertEquals("ext-items", p.getExtensions().get("x-ext"));

    Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertNotNull(inline);
    assertTrue(inline instanceof Schema);

    assertNotNull(inline.getProperties().get("name"));
    assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
 
Example 6
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@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);
}