Search in sources :

Example 11 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 12 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 13 with ApiResponse

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

Example 14 with ApiResponse

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

the class ComponentsProcessor method processResponses.

private void processResponses(Set<String> responseKey, Map<String, ApiResponse> responses) {
    responseKey.addAll(responses.keySet());
    for (String responseName : responseKey) {
        final ApiResponse response = responses.get(responseName);
        responseProcessor.processResponse(response);
    }
}
Also used : ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 15 with ApiResponse

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

the class V2ConverterTest method testIssue33.

@Test(description = "Convert response examples")
public void testIssue33() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_33_JSON);
    ApiResponse apiResponse = oas.getPaths().get(USERS_PATH).getGet().getResponses().get("200");
    assertNotNull(apiResponse);
    assertNotNull(apiResponse.getContent().get(APPLICATION_YAML).getExample());
    assertNotNull(apiResponse.getContent().get(APPLICATION_JSON));
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

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