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