use of com.devonfw.cobigen.openapiplugin.model.ResponseDef in project cobigen by devonfw.
the class OpenAPIInputReader method extractResponses.
/**
* Returns a {@link ResponseDef} from a operation '200' response definition depending on the tags
*
* @param overlay overlay of the operator
* @param responses list of OpenApu responses definition
* @param tags list of oasp4j relative tags
* @return List of {@link ResponseDef}'s
*/
private List<ResponseDef> extractResponses(Map<String, ? extends Response> responses, Collection<String> tags) {
ResponseDef response;
List<String> mediaTypes = new LinkedList<>();
List<ResponseDef> resps = new LinkedList<>();
String schemaType;
for (String resp : responses.keySet()) {
response = new ResponseDef();
response.setCode(resp);
Map<String, MediaType> contentMediaTypes = responses.get(resp).getContentMediaTypes();
response.setDescription(responses.get(resp).getDescription());
if (contentMediaTypes != null) {
if (contentMediaTypes.isEmpty()) {
response.setIsVoid(true);
}
for (String media : contentMediaTypes.keySet()) {
mediaTypes.add(media);
Schema schema = contentMediaTypes.get(media).getSchema();
if (schema != null) {
schemaType = schema.getType();
if (Constants.OBJECT.equals(schemaType)) {
response.setType(schema.getName());
response.setIsEntity(true);
EntityDef eDef = new EntityDef();
List<PropertyDef> propDefs = new LinkedList<>();
for (Schema propertySchema : schema.getProperties().values()) {
PropertyDef prop = new PropertyDef();
prop.setDescription(propertySchema.getDescription());
prop.setFormat(propertySchema.getFormat());
prop.setName(propertySchema.getName());
prop.setType(propertySchema.getType());
propDefs.add(prop);
}
eDef.setName(schema.getName());
eDef.setProperties(propDefs);
response.setEntityRef(eDef);
} else if (Constants.ARRAY.equals(schemaType)) {
if (schema.getItemsSchema() != null) {
response.setType(schema.getItemsSchema().getName());
response.setIsEntity(true);
} else {
response.setType(schema.getItemsSchema().getType());
}
if (tags.contains(Constants.PAGINATED)) {
response.setIsPaginated(true);
} else {
response.setIsArray(true);
}
} else if (schemaType != null) {
response.setType(schemaType);
} else {
response.setIsVoid(true);
}
}
}
} else {
response.setIsVoid(true);
}
response.setMediaTypes(mediaTypes);
resps.add(response);
}
return resps;
}
use of com.devonfw.cobigen.openapiplugin.model.ResponseDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testRetrieveResponsesOfPath.
@Test
public void testRetrieveResponsesOfPath() throws Exception {
List<Object> inputObjects = getInputs("two-components.yaml");
boolean found = false;
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef eDef = (EntityDef) o;
if (eDef.getName().equals("Table")) {
assertThat(eDef.getComponent().getPaths()).hasSize(2);
for (PathDef pathDef : eDef.getComponent().getPaths()) {
for (OperationDef opDef : pathDef.getOperations()) {
if (opDef.getOperationId() != null && opDef.getOperationId().equals("findTable")) {
found = true;
assertThat(opDef.getResponses()).hasSize(2);
for (ResponseDef respDef : opDef.getResponses()) {
if (respDef.getCode().equals("200")) {
assertThat(respDef.getMediaTypes()).hasSize(2);
assertThat(respDef.getMediaTypes()).containsExactly("application/json", "text/plain");
} else if (respDef.getCode().equals("404")) {
assertThat(respDef.getDescription()).isEqualTo("Not found");
} else {
found = false;
}
}
}
}
}
}
}
}
assertThat(found).as("findTable path operation not found!").isTrue();
}
use of com.devonfw.cobigen.openapiplugin.model.ResponseDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testResponse.
@Test
public void testResponse() throws Exception {
List<Object> inputObjects = getInputs("componentResponseType.yaml");
boolean found = false;
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef e = (EntityDef) o;
ComponentDef c = e.getComponent();
for (PathDef p : c.getPaths()) {
if (p.getPathURI().equals("/sampledata/customSearch/")) {
for (OperationDef op : p.getOperations()) {
for (ResponseDef r : op.getResponses()) {
assertThat(r.getType()).isEqualTo("SampleData");
found = true;
}
}
}
}
}
}
assertThat(found).isTrue();
}
Aggregations