use of io.swagger.v3.oas.models.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);
}
use of io.swagger.v3.oas.models.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"));
}
use of io.swagger.v3.oas.models.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");
}
use of io.swagger.v3.oas.models.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;
}
use of io.swagger.v3.oas.models.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);
}
Aggregations