Search in sources :

Example 1 with ObjectType

use of com.github.sebastianfrey.joa.models.schema.type.ObjectType in project joa by sebastianfrey.

the class JSONSchemaBuilderTest method should_serialize_full_object_type.

@Test
public void should_serialize_full_object_type() throws Exception {
    ObjectType objectType = objectType().title("title").property("string", stringType().title("string")).property("number", numberType().title("number")).patternProperty("^_S", stringType()).required(List.of("string")).additionalProperties().unevaluatedProperties().minProperties(1).maxProperties(2).propertyNames("^[A-Za-z_][A-Za-z0-9_]*$");
    final String expected = MAPPER.writeValueAsString(MAPPER.readValue(fixture("fixtures/schemas/type/objectFull.json"), ObjectType.class));
    assertThat(MAPPER.writeValueAsString(objectType)).isEqualTo(expected);
}
Also used : ObjectType(com.github.sebastianfrey.joa.models.schema.type.ObjectType) Test(org.junit.jupiter.api.Test)

Example 2 with ObjectType

use of com.github.sebastianfrey.joa.models.schema.type.ObjectType in project joa by sebastianfrey.

the class JSONSchemaBuilderTest method should_serialize_object_type.

@Test
public void should_serialize_object_type() throws Exception {
    ObjectType objectType = objectType();
    final String expected = MAPPER.writeValueAsString(MAPPER.readValue(fixture("fixtures/schemas/type/object.json"), ObjectType.class));
    assertThat(MAPPER.writeValueAsString(objectType)).isEqualTo(expected);
}
Also used : ObjectType(com.github.sebastianfrey.joa.models.schema.type.ObjectType) Test(org.junit.jupiter.api.Test)

Example 3 with ObjectType

use of com.github.sebastianfrey.joa.models.schema.type.ObjectType in project joa by sebastianfrey.

the class GeoPackageService method getQueryables.

@Override
public Queryables getQueryables(String serviceId, String collectionId) {
    try (GeoPackage gpkg = loadService(serviceId)) {
        ObjectType schema = JSONSchemaBuilder.objectType().title(collectionId).schema(Schemas.DRAFT_2019_09);
        FeatureDao featureDao = loadCollection(gpkg, collectionId);
        String geometryColumn = featureDao.getGeometryColumnName();
        featureDao.getColumns().stream().forEach((column) -> {
            if (column.isGeometry()) {
                return;
            }
            Long max = column.getMax();
            Object defaultValue = column.getDefaultValue();
            GenericType<?> type = null;
            switch(column.getDataType()) {
                case BOOLEAN:
                    type = JSONSchemaBuilder.booleanType();
                    break;
                case BLOB:
                case TINYINT:
                case TEXT:
                    type = JSONSchemaBuilder.stringType().maxLength(max);
                    break;
                case DATE:
                    type = JSONSchemaBuilder.stringType().maxLength(max).format("date");
                    break;
                case DATETIME:
                    type = JSONSchemaBuilder.stringType().maxLength(max).format("date-time");
                    break;
                case DOUBLE:
                case FLOAT:
                case REAL:
                    type = JSONSchemaBuilder.numberType();
                    break;
                case INT:
                case INTEGER:
                case MEDIUMINT:
                case SMALLINT:
                    type = JSONSchemaBuilder.integerType().maximum(max);
                    break;
                default:
                    break;
            }
            if (type == null) {
                return;
            }
            String columnName = column.getName();
            type.title(columnName);
            if (defaultValue != null) {
                type.defaultValue(defaultValue.toString());
            }
            schema.property(columnName, type);
        });
        GeometryType geometryType = featureDao.getGeometryType();
        JSONSchema geometrySchema = null;
        switch(geometryType) {
            case GEOMETRY:
                geometrySchema = Schemas.GeoJSON.geometry();
                break;
            case POINT:
                geometrySchema = Schemas.GeoJSON.point();
                break;
            case LINESTRING:
                geometrySchema = Schemas.GeoJSON.lineString();
                break;
            case POLYGON:
                geometrySchema = Schemas.GeoJSON.polygon();
                break;
            case MULTIPOINT:
                geometrySchema = Schemas.GeoJSON.multiPoint();
                break;
            case MULTILINESTRING:
                geometrySchema = Schemas.GeoJSON.multiLineString();
                break;
            case MULTIPOLYGON:
                geometrySchema = Schemas.GeoJSON.multiPolygon();
                break;
            default:
                break;
        }
        if (geometrySchema != null) {
            schema.property(geometryColumn, geometrySchema);
        }
        return new Queryables().serviceId(serviceId).collectionId(collectionId).schema(schema);
    }
}
Also used : ObjectType(com.github.sebastianfrey.joa.models.schema.type.ObjectType) GeometryType(mil.nga.sf.GeometryType) Queryables(com.github.sebastianfrey.joa.models.Queryables) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) JSONSchema(com.github.sebastianfrey.joa.models.schema.JSONSchema) GeoPackage(mil.nga.geopackage.GeoPackage)

Aggregations

ObjectType (com.github.sebastianfrey.joa.models.schema.type.ObjectType)3 Test (org.junit.jupiter.api.Test)2 Queryables (com.github.sebastianfrey.joa.models.Queryables)1 JSONSchema (com.github.sebastianfrey.joa.models.schema.JSONSchema)1 GeoPackage (mil.nga.geopackage.GeoPackage)1 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)1 GeometryType (mil.nga.sf.GeometryType)1