Search in sources :

Example 11 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testAdditionalPropertiesBoolean.

@Test
public void testAdditionalPropertiesBoolean() {
    String yaml = "openapi: 3.0.0\n" + "info:\n" + "  title: Test\n" + "  version: 1.0.0\n" + "paths:\n" + "  \"/store/inventory\":\n" + "    post:\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              additionalProperties: true\n" + "      responses:\n" + "        '200':\n" + "          description: successful operation\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                additionalProperties: false\n";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertEquals(result.getMessages(), emptyList());
    OpenAPI openAPI = result.getOpenAPI();
    Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
    assertEquals(body.getAdditionalProperties(), Boolean.TRUE);
    assertEquals(body.getClass(), MapSchema.class);
    Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
    assertEquals(response.getAdditionalProperties(), Boolean.FALSE);
    assertEquals(response.getClass(), ObjectSchema.class);
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) 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) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 12 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testEmptyDefinitions.

@Test
public void testEmptyDefinitions() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers:\n" + "  - url: 'http://abc:5555/mypath'\n" + "info:\n" + "  version: '1.0'\n" + "  title: dd\n" + "paths:\n" + "  /resource1/Id:\n" + "    post:\n" + "      description: ''\n" + "      operationId: postOp\n" + "      responses:\n" + "        '200':\n" + "          description: Successful\n" + "        '401':\n" + "          description: Access Denied\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/mydefinition'\n" + "        required: true\n" + "components:\n" + "  schemas:\n" + "    mydefinition: {}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents().getSchemas().get("mydefinition"));
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 13 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testBodyContent.

@Test
public void testBodyContent() {
    String json = "{" + "  \"openapi\": \"3.0.0\"," + "  \"info\": {" + "    \"title\": \"Operations\"," + "    \"version\": \"0.0.0\"" + "  }," + "  \"paths\": {" + "    \"/operations\": {" + "      \"post\": {" + "        \"requestBody\": {" + "            \"description\": \"Content empty\"," + "            \"content\": {" + "            }" + "        }," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }," + "      \"put\": {" + "        \"requestBody\": {" + "            \"description\": \"Content undefined\"" + "        }," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }" + "    }" + "  }" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    Operation post = result.getOpenAPI().getPaths().get("/operations").getPost();
    assertEquals(post.getRequestBody().getContent(), null, "Empty content");
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).requestBody.content with no media type is unsupported"), true, "Empty content error reported");
    Operation put = result.getOpenAPI().getPaths().get("/operations").getPut();
    assertEquals(put.getRequestBody().getContent(), null, "Empty content");
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(put).requestBody.content is missing"), true, "Missing content error reported");
    assertEquals(result.getMessages().size(), 2, "Messages");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 14 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody 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 15 with RequestBody

use of io.swagger.v3.oas.annotations.parameters.RequestBody in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue36.

@Test(description = "Nice to have: Convert x-example to example")
public void testIssue36() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_36_JSON);
    List<Parameter> parameters = oas.getPaths().get(FOO_PATH).getGet().getParameters();
    assertNotNull(parameters);
    assertEquals(parameters.get(0).getExample(), FOO_VALUE);
    assertEquals(parameters.get(1).getExample(), NUMBER_VALUE_TWENTY);
    assertEquals(parameters.get(2).getExample(), CODE_EXAMPLE);
    RequestBody requestBody = oas.getPaths().get(LOGIN_PATH).getPost().getRequestBody();
    assertNotNull(requestBody);
    Map properties = requestBody.getContent().get(CONTENT_TYPE).getSchema().getProperties();
    assertEquals(((Schema) properties.get(EMAIL)).getExample(), EMAIL_VALUE);
    assertEquals(((Schema) properties.get(PASSWORD)).getExample(), PASSWORD_VALUE);
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)67 OpenAPI (io.swagger.v3.oas.models.OpenAPI)62 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)59 Schema (io.swagger.v3.oas.models.media.Schema)46 Operation (io.swagger.v3.oas.annotations.Operation)41 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)40 MediaType (io.swagger.v3.oas.models.media.MediaType)36 StringSchema (io.swagger.v3.oas.models.media.StringSchema)35 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)32 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)32 Content (io.swagger.v3.oas.models.media.Content)31 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)28 Operation (io.swagger.v3.oas.models.Operation)27 PathItem (io.swagger.v3.oas.models.PathItem)23 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)21 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)19 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)14 Components (io.swagger.v3.oas.models.Components)13 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)12