Search in sources :

Example 1 with PropertyDef

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

the class OpenAPIInputReaderTest method testRetrieveTypesAndFormatsOfPropertiesOfEntity.

@Test
public void testRetrieveTypesAndFormatsOfPropertiesOfEntity() throws Exception {
    List<Object> inputObjects = getInputs("two-components.yaml");
    List<PropertyDef> properties = new LinkedList<>();
    for (Object o : inputObjects) {
        if (isEntityDef(o)) {
            properties.addAll(((EntityDef) o).getProperties());
        }
    }
    List<String> types = new LinkedList<>();
    List<String> formats = new LinkedList<>();
    for (PropertyDef property : properties) {
        types.add(property.getType());
        formats.add(property.getFormat());
    }
    assertThat(types).hasSize(2);
    assertThat(formats).hasSize(2);
    assertThat(types).containsExactly("string", "number");
    assertThat(formats).containsExactly(null, "int64");
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with PropertyDef

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

the class OpenAPIInputReaderTest method testPropertyRefManyToOne.

@Test
public void testPropertyRefManyToOne() throws Exception {
    List<Object> inputObjects = getInputs("property-ref-many-to-one.yaml");
    boolean found = false;
    for (Object o : inputObjects) {
        if (isEntityDef(o)) {
            EntityDef entityDef = (EntityDef) o;
            if (entityDef.getName().equals("SampleData")) {
                assertThat(entityDef.getProperties()).hasSize(1);
                PropertyDef prop = entityDef.getProperties().get(0);
                assertThat(prop.getType()).isEqualTo("MoreData");
                assertThat(prop.getName()).isEqualTo("mainData");
                assertThat(prop.getSameComponent()).isTrue();
                assertThat(prop.getDescription()).isEqualTo("a single ref to MoreData (many-to-one)");
                found = true;
            }
        }
    }
    assertThat(found).as("SampleData component schema not found!").isTrue();
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 3 with PropertyDef

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

the class OpenAPIInputReaderTest method testPropertyRefOneToOne.

@Test
public void testPropertyRefOneToOne() throws Exception {
    List<Object> inputObjects = getInputs("property-ref-one-to-one.yaml");
    boolean found = false;
    for (Object o : inputObjects) {
        if (isEntityDef(o)) {
            EntityDef entityDef = (EntityDef) o;
            if (entityDef.getName().equals("SampleData")) {
                assertThat(entityDef.getProperties()).hasSize(1);
                PropertyDef prop = entityDef.getProperties().get(0);
                assertThat(prop.getType()).isEqualTo("MoreData");
                assertThat(prop.getName()).isEqualTo("mainData");
                assertThat(prop.getSameComponent()).isTrue();
                // The description of the property will be ignored in compliance with the JSON
                // specification:
                // https://github.com/RepreZen/KaiZen-OpenApi-Parser/issues/148
                assertThat(prop.getDescription()).isEqualTo("MoreData Desc");
                found = true;
            }
        }
    }
    assertThat(found).as("SampleData component schema not found!").isTrue();
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 4 with PropertyDef

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

the class OpenAPIInputReader method extractProperties.

/**
 * Get the fields of an entity returning a list of {@link PropertyDef}'s
 *
 * @param openApi the OpenApi3 model
 * @param componentName entity name
 * @return List of {@link PropertyDef}'s
 */
private List<PropertyDef> extractProperties(OpenApi3 openApi, String componentName) {
    Schema componentSchema = openApi.getSchema(componentName);
    Map<String, ? extends Schema> properties = componentSchema.getProperties();
    List<PropertyDef> objects = new LinkedList<>();
    for (Entry<String, ? extends Schema> prop : properties.entrySet()) {
        String propertyName = prop.getKey();
        Schema propertySchema = prop.getValue();
        PropertyDef propModel;
        if (propertySchema.getType().equals(Constants.ARRAY)) {
            if (propertySchema.getItemsSchema().getType().equals(Constants.OBJECT) && propertySchema.getItemsSchema() != null && propertySchema.getItemsSchema().getExtension(Constants.COMPONENT_EXT) != null) {
                String targetComponent = propertySchema.getItemsSchema().getExtension(Constants.COMPONENT_EXT).toString();
                propModel = extractReferenceProperty(componentSchema, prop, targetComponent);
            } else {
                propModel = new PropertyDef();
                propModel.setType(propertySchema.getItemsSchema().getType());
            }
            if (propertySchema.getItemsSchema().getFormat() != null) {
                propModel.setFormat(propertySchema.getItemsSchema().getFormat());
            }
            propModel.setIsCollection(true);
        } else {
            if (propertySchema.getType().equals(Constants.OBJECT) && propertySchema.getExtension(Constants.COMPONENT_EXT) != null) {
                String targetComponent = propertySchema.getExtension(Constants.COMPONENT_EXT).toString();
                propModel = extractReferenceProperty(componentSchema, prop, targetComponent);
            } else {
                propModel = new PropertyDef();
                propModel.setType(propertySchema.getType());
            }
            if (propertySchema.getFormat() != null) {
                propModel.setFormat(propertySchema.getFormat());
            }
        }
        if (propertySchema.hasEnums()) {
            Collection<Object> enums = propertySchema.getEnums();
            List<String> enumElements = new ArrayList<>();
            for (Object element : enums) {
                if (element != null) {
                    enumElements.add(element.toString());
                } else {
                    enumElements.add("null");
                }
            }
            propModel.setEnumElements(enumElements);
        }
        propModel.setName(propertyName);
        propModel.setDescription(propertySchema.getDescription());
        Map<String, Object> constraints = extractConstraints(propertySchema);
        if (componentSchema.getRequiredFields().contains(propertyName)) {
            constraints.put(ModelConstant.NOTNULL, true);
        } else {
            constraints.put(ModelConstant.NOTNULL, false);
        }
        propModel.setConstraints(constraints);
        objects.add(propModel);
    }
    return objects;
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) Schema(com.reprezen.kaizen.oasparser.model3.Schema) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 5 with PropertyDef

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

the class OpenAPIInputReader method extractReferenceProperty.

/**
 * @param componentSchema component schema respectively object or entity
 * @param property property name to schema mapping
 * @param targetComponent name of the target component
 * @return model of a property
 */
private PropertyDef extractReferenceProperty(Schema componentSchema, Entry<String, ? extends Schema> property, String targetComponent) {
    Schema propertySchema = property.getValue();
    String refTypeName = componentSchema.getName();
    if (propertySchema.getType().equals(Constants.ARRAY)) {
        refTypeName = propertySchema.getItemsSchema().getName();
    } else {
        refTypeName = propertySchema.getName();
    }
    String thisComponent = componentSchema.getExtension(Constants.COMPONENT_EXT).toString();
    boolean sameComponent;
    if (thisComponent.equals(targetComponent)) {
        sameComponent = true;
    } else {
        sameComponent = false;
    }
    PropertyDef propertyDef = new PropertyDef();
    propertyDef.setIsEntity(true);
    propertyDef.setRequired(componentSchema.getRequiredFields().contains(property.getKey()));
    propertyDef.setSameComponent(sameComponent);
    propertyDef.setType(refTypeName);
    // TODO unidirectional?
    return propertyDef;
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) Schema(com.reprezen.kaizen.oasparser.model3.Schema)

Aggregations

PropertyDef (com.devonfw.cobigen.openapiplugin.model.PropertyDef)8 Test (org.junit.Test)5 EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)4 LinkedList (java.util.LinkedList)4 Schema (com.reprezen.kaizen.oasparser.model3.Schema)3 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)1 MediaType (com.reprezen.kaizen.oasparser.model3.MediaType)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1