Search in sources :

Example 1 with HeaderDef

use of com.devonfw.cobigen.openapiplugin.model.HeaderDef in project cobigen by devonfw.

the class OpenAPIInputReader method extractComponents.

/**
 * Get a list of entities defined at an OpenaApi3 file returning a list of {@link EntityDef}'s
 *
 * @param openApi the model for an OpenApi3 file
 * @return list of entities
 */
private List<EntityDef> extractComponents(OpenApi3 openApi) {
    HeaderDef header = new HeaderDef();
    header.setServers(extractServers(openApi));
    header.setInfo(extractInfo(openApi));
    List<EntityDef> objects = new LinkedList<>();
    this.components = new LinkedList<>();
    for (String key : openApi.getSchemas().keySet()) {
        EntityDef entityDef = new EntityDef();
        entityDef.setName(key);
        entityDef.setDescription(openApi.getSchema(key).getDescription());
        ComponentDef componentDef = new ComponentDef();
        entityDef.setProperties(extractProperties(openApi, key));
        // If no x-component tag was found on the input file, throw invalid configuration
        if (openApi.getSchema(key).getExtensions().get(Constants.COMPONENT_EXT) == null) {
            throw new InvalidConfigurationException("Your Swagger file is not correctly formatted, it lacks of x-component tags.\n\n" + "Go to the documentation " + "(https://github.com/devonfw/cobigen/wiki/cobigen-openapiplugin#full-example) " + "to check how to correctly format it." + " If it is still not working, check your file indentation!");
        }
        String componentName = openApi.getSchema(key).getExtensions().get(Constants.COMPONENT_EXT).toString();
        entityDef.setComponentName(componentName);
        // If the path's tag was not found on the input file, throw invalid configuration
        if (openApi.getPaths().size() == 0) {
            throw new InvalidConfigurationException("Your Swagger file is not correctly formatted, it lacks of the correct path syntax.\n\n" + "Go to the documentation (https://github.com/devonfw/cobigen" + "/wiki/cobigen-openapiplugin#paths) to check how to correctly format it." + " If it is still not working, check your file indentation!");
        }
        // Sets a Map containing all the extensions of the info part of the OpenAPI file
        if (Overlay.isPresent((JsonOverlay<?>) openApi.getInfo())) {
            entityDef.setUserPropertiesMap(openApi.getInfo().getExtensions());
        }
        // Traverse the extensions of the entity for setting those attributes to the Map
        Iterator<String> it = openApi.getSchema(key).getExtensions().keySet().iterator();
        while (it.hasNext()) {
            String keyMap = it.next();
            entityDef.setUserProperty(keyMap, openApi.getSchema(key).getExtensions().get(keyMap).toString());
        }
        componentDef.setPaths(extractPaths(openApi.getPaths(), componentName));
        componentDef.setName(componentName);
        this.components.add(componentDef);
        entityDef.setComponent(componentDef);
        entityDef.setHeader(header);
        objects.add(entityDef);
    }
    return objects;
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) HeaderDef(com.devonfw.cobigen.openapiplugin.model.HeaderDef) LinkedList(java.util.LinkedList) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 2 with HeaderDef

use of com.devonfw.cobigen.openapiplugin.model.HeaderDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method testRetrieveHeaderInfo.

@Test
public void testRetrieveHeaderInfo() throws Exception {
    List<Object> inputObjects = getInputs("two-components.yaml");
    for (Object o : inputObjects) {
        if (isEntityDef(o)) {
            EntityDef entityDef = (EntityDef) o;
            HeaderDef header = entityDef.getHeader();
            InfoDef info = header.getInfo();
            assertThat(info.getDescription()).isEqualTo("Example of a API definition");
            assertThat(info.getTitle()).isEqualTo("Devon Example");
            List<ServerDef> servers = header.getServers();
            assertThat(servers).hasSize(1);
            ServerDef server = servers.get(0);
            assertThat(server.getDescription()).isEqualTo("Just some data");
            assertThat(server.getURI()).isEqualTo("https://localhost:8081/server/services/rest");
        }
    }
}
Also used : HeaderDef(com.devonfw.cobigen.openapiplugin.model.HeaderDef) InfoDef(com.devonfw.cobigen.openapiplugin.model.InfoDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) ServerDef(com.devonfw.cobigen.openapiplugin.model.ServerDef) Test(org.junit.Test)

Aggregations

EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)2 HeaderDef (com.devonfw.cobigen.openapiplugin.model.HeaderDef)2 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)1 InfoDef (com.devonfw.cobigen.openapiplugin.model.InfoDef)1 ServerDef (com.devonfw.cobigen.openapiplugin.model.ServerDef)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1