Search in sources :

Example 16 with RequestBody

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

the class SwaggerConverter method convertParameterToRequestBody.

private RequestBody convertParameterToRequestBody(io.swagger.models.parameters.Parameter param, List<String> consumes) {
    RequestBody body = new RequestBody();
    BodyParameter bp = (BodyParameter) param;
    List<String> mediaTypes = new ArrayList<>(globalConsumes);
    if (consumes != null && consumes.size() > 0) {
        mediaTypes.clear();
        mediaTypes.addAll(consumes);
    }
    if (mediaTypes.size() == 0) {
        mediaTypes.add("*/*");
    }
    if (StringUtils.isNotBlank(param.getDescription())) {
        body.description(param.getDescription());
    }
    body.required(param.getRequired());
    Content content = new Content();
    for (String type : mediaTypes) {
        content.addMediaType(type, new MediaType().schema(convert(bp.getSchema())));
        if (StringUtils.isNotBlank(bp.getDescription())) {
            body.setDescription(bp.getDescription());
        }
    }
    convertExamples(((BodyParameter) param).getExamples(), content);
    body.content(content);
    return body;
}
Also used : Content(io.swagger.v3.oas.models.media.Content) ArrayList(java.util.ArrayList) MediaType(io.swagger.v3.oas.models.media.MediaType) BodyParameter(io.swagger.models.parameters.BodyParameter) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 17 with RequestBody

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

the class V2ConverterTest method testIssue600.

@Test(description = "OpenAPI v2 converter - required Form parameters are converted as optional ")
public void testIssue600() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_600_JSON);
    RequestBody requestBody = oas.getPaths().get(LOGIN_PATH).getPost().getRequestBody();
    assertNotNull(requestBody);
    assertTrue(requestBody.getRequired());
    Schema schema = requestBody.getContent().get(CONTENT_TYPE).getSchema();
    List required = schema.getRequired();
    assertNotNull(required);
    assertEquals(required.size(), REQUIRED_SIZE);
}
Also used : BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ArrayList(java.util.ArrayList) List(java.util.List) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 18 with RequestBody

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

the class V2ConverterTest method testIssue19.

@Test(description = "Request Body Completions")
public void testIssue19() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_19_JSON);
    RequestBody requestBody = oas.getPaths().get(POST_PATH).getPost().getRequestBody();
    assertNotNull(requestBody);
    Schema schema = requestBody.getContent().get(URLENCODED_CONTENT).getSchema();
    assertNotNull(schema);
    Map properties = schema.getProperties();
    assertEquals(PROPERTIES_SIZE, properties.size());
    ArraySchema ids = (ArraySchema) properties.get("ids");
    assertEquals(new Integer(MIN_ITEMS), ids.getMinItems());
    assertEquals(new Integer(MAXIMUM), ids.getMaxItems());
    // Fixed from Issue 19
    assertEquals(Boolean.TRUE, ids.getUniqueItems());
    Schema login = (Schema) properties.get("login");
    assertEquals(new Integer(MIN_LENGTH), login.getMinLength());
    assertEquals(new Integer(MAX_LENGTH), login.getMaxLength());
    assertEquals(PATTERN, login.getPattern());
    Schema favNumber = (Schema) properties.get("favNumber");
    assertEquals(new BigDecimal(MAXIMUM), favNumber.getMinimum());
    assertEquals(new BigDecimal(MAXIMUM), favNumber.getMaximum());
    assertEquals(Boolean.TRUE, favNumber.getExclusiveMinimum());
    assertEquals(Boolean.TRUE, favNumber.getExclusiveMaximum());
    assertEquals(new BigDecimal(MULTIPLE_OF_VALUE), new BigDecimal(favNumber.getMultipleOf().doubleValue()));
    Schema dayOfWeek = (Schema) properties.get("dayOfWeek");
    assertEquals(MONDAY_TUESDAY_VALUE, dayOfWeek.getDefault());
    assertEquals(MONDAY_TUESDAY_VALUE, dayOfWeek.getEnum().get(0));
    assertEquals(TUESDAY_WEDNESDAY_VALUE, dayOfWeek.getEnum().get(1));
    assertEquals(WEDNESDAY_THURSDAY_VALUE, dayOfWeek.getEnum().get(2));
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) BigDecimal(java.math.BigDecimal) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 19 with RequestBody

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

the class V2ConverterTest method testIssue17.

@Test(description = "Referenced parameters are converted incorrectly")
public void testIssue17() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_17_JSON);
    Map<String, RequestBody> requestBodies = oas.getComponents().getRequestBodies();
    Map<String, Schema> schemas = oas.getComponents().getSchemas();
    assertNotNull(schemas.get("formData_formEmail"));
    assertNotNull(schemas.get("formData_formPassword"));
    assertNotNull(requestBodies.get("bodyParam").getContent().get("*/*"));
    assertNull(oas.getPaths().get("/formPost").getPost().getParameters());
    assertNotNull(oas.getPaths().get("/formPost").getPost().getRequestBody());
    assertNotNull(oas.getPaths().get("/report/{userId}").getGet().getRequestBody().getContent().get("multipart/form-data").getSchema().getProperties().get("limitForm"));
}
Also used : BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 20 with RequestBody

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

the class V2ConverterTest method testIssue765.

@Test(description = "requestBody not correctly populated when Parameters is a list of $refs (OAS 2 to 3 conversion)")
public void testIssue765() throws Exception {
    final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_765_YAML);
    assertNotNull(oas);
    RequestBody requestBody = oas.getPaths().get("/ping/{ActivityTypePath}").getPost().getRequestBody();
    assertNotNull(requestBody);
    assertEquals("#/components/requestBodies/Block", requestBody.get$ref());
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) 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