Search in sources :

Example 16 with ApiResponses

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

the class InlineModelResolverTest method testInlineMapResponseWithObjectSchema.

@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);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) 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) 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 17 with ApiResponses

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

the class InlineModelResolverTest method testInlineResponseModel.

@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);
}
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 18 with ApiResponses

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

the class InlineModelResolverTest method testArbitraryObjectResponseArray.

@Test
public void testArbitraryObjectResponseArray() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new ObjectSchema()))))))));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) response.getContent().get("*/*").getSchema();
    Schema items = arraySchema.getItems();
    assertTrue(items instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) items;
    assertNull(op.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) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 19 with ApiResponses

use of io.swagger.v3.oas.models.responses.ApiResponses 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 20 with ApiResponses

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

the class SwaggerConverter method convert.

public Operation convert(io.swagger.models.Operation v2Operation) {
    Operation operation = new Operation();
    if (StringUtils.isNotBlank(v2Operation.getDescription())) {
        operation.setDescription(v2Operation.getDescription());
    }
    if (StringUtils.isNotBlank(v2Operation.getSummary())) {
        operation.setSummary(v2Operation.getSummary());
    }
    operation.setDeprecated(v2Operation.isDeprecated());
    operation.setOperationId(v2Operation.getOperationId());
    operation.setExtensions(convert(v2Operation.getVendorExtensions()));
    operation.setTags(v2Operation.getTags());
    if (v2Operation.getParameters() != null) {
        List<io.swagger.models.parameters.Parameter> formParams = new ArrayList<>();
        for (io.swagger.models.parameters.Parameter param : v2Operation.getParameters()) {
            if ("formData".equals(param.getIn())) {
                formParams.add(param);
            } else if ("body".equals(param.getIn())) {
                operation.setRequestBody(convertParameterToRequestBody(param, v2Operation.getConsumes()));
                operation.addExtension("x-codegen-request-body-name", param.getName());
            } else {
                Parameter convert = convert(param);
                String $ref = convert.get$ref();
                if ($ref != null && $ref.startsWith("#/components/requestBodies/") && isRefABodyParam(param)) {
                    operation.setRequestBody(new RequestBody().$ref($ref));
                } else if ($ref != null && $ref.startsWith("#/components/schemas/") && isRefAFormParam(param)) {
                    formParams.add(param);
                } else {
                    operation.addParametersItem(convert);
                }
            }
        }
        if (formParams.size() > 0) {
            RequestBody body = convertFormDataToRequestBody(formParams, v2Operation.getConsumes());
            body.getContent().forEach((key, content) -> {
                Schema schema = content.getSchema();
                if (schema != null && schema.getRequired() != null && schema.getRequired().size() > 0) {
                    body.setRequired(Boolean.TRUE);
                }
            });
            operation.requestBody(body);
        }
    }
    if (v2Operation.getResponses() != null) {
        for (String responseCode : v2Operation.getResponses().keySet()) {
            io.swagger.models.Response v2Response = v2Operation.getResponses().get(responseCode);
            ApiResponse response = convert(v2Response, v2Operation.getProduces());
            ApiResponses responses = operation.getResponses();
            if (responses == null) {
                responses = new ApiResponses();
            }
            operation.responses(responses.addApiResponse(responseCode, response));
        }
    }
    if (v2Operation.getExternalDocs() != null) {
        operation.setExternalDocs(convert(v2Operation.getExternalDocs()));
    }
    if (v2Operation.getSecurity() != null && v2Operation.getSecurity().size() > 0) {
        operation.setSecurity(convertSecurityRequirementsMap(v2Operation.getSecurity()));
    }
    return operation;
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Response(io.swagger.models.Response) Parameter(io.swagger.v3.oas.models.parameters.Parameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)99 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)99 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)48 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)47 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)47 Operation (io.swagger.v3.oas.models.Operation)39 PathItem (io.swagger.v3.oas.models.PathItem)35 OpenAPI (io.swagger.v3.oas.models.OpenAPI)34 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)23 Schema (io.swagger.v3.oas.models.media.Schema)22 Content (io.swagger.v3.oas.models.media.Content)21 StringSchema (io.swagger.v3.oas.models.media.StringSchema)21 MediaType (io.swagger.v3.oas.models.media.MediaType)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)19 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)17 Path (javax.ws.rs.Path)17 Produces (javax.ws.rs.Produces)17 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)16 Components (io.swagger.v3.oas.models.Components)10