Search in sources :

Example 1 with ResponseDef

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;
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) Schema(com.reprezen.kaizen.oasparser.model3.Schema) MediaType(com.reprezen.kaizen.oasparser.model3.MediaType) ResponseDef(com.devonfw.cobigen.openapiplugin.model.ResponseDef) LinkedList(java.util.LinkedList) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef)

Example 2 with ResponseDef

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();
}
Also used : OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) ResponseDef(com.devonfw.cobigen.openapiplugin.model.ResponseDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 3 with ResponseDef

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();
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) ResponseDef(com.devonfw.cobigen.openapiplugin.model.ResponseDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Aggregations

EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)3 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)3 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)2 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)2 Test (org.junit.Test)2 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)1 PropertyDef (com.devonfw.cobigen.openapiplugin.model.PropertyDef)1 MediaType (com.reprezen.kaizen.oasparser.model3.MediaType)1 Schema (com.reprezen.kaizen.oasparser.model3.Schema)1 LinkedList (java.util.LinkedList)1