Search in sources :

Example 1 with ObjectSchema

use of com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema in project syndesis by syndesisio.

the class SalesforceMetadataRetrieval method convertSalesforceGlobalObjectJsonToSchema.

static ObjectSchema convertSalesforceGlobalObjectJsonToSchema(final JsonNode payload) {
    final Set<Object> allSchemas = new HashSet<>();
    for (final JsonNode sobject : payload) {
        // generate SObject schema from description
        final ObjectSchema sobjectSchema = new ObjectSchema();
        sobjectSchema.setId(JsonUtils.DEFAULT_ID_PREFIX + ":" + sobject.get("name").asText());
        sobjectSchema.setTitle(sobject.get("label").asText());
        allSchemas.add(sobjectSchema);
    }
    final ObjectSchema schema = new ObjectSchema();
    schema.setOneOf(allSchemas);
    return schema;
}
Also used : ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashSet(java.util.HashSet)

Example 2 with ObjectSchema

use of com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema in project syndesis by syndesisio.

the class SalesforceMetadataRetrievalTest method shouldAdaptObjectTypesMetadataForProperties.

@Test
public void shouldAdaptObjectTypesMetadataForProperties() {
    final ObjectSchema globalObjectsPayload = new ObjectSchema();
    final HashSet<Object> oneOf = new HashSet<>();
    oneOf.add(simpleObjectSchema("Object1", "Object1 Label"));
    oneOf.add(simpleObjectSchema("Object2", "Object2 Label"));
    globalObjectsPayload.setOneOf(oneOf);
    final SyndesisMetadata metadata = adapter.adapt(null, null, null, NOT_USED, MetaDataBuilder.on(CONTEXT).withPayload(globalObjectsPayload).build());
    assertThat(metadata.properties).containsKey("sObjectName");
    final List<PropertyPair> values = metadata.properties.get("sObjectName");
    assertThat(values).containsOnly(new PropertyPair("Object1", "Object1 Label"), new PropertyPair("Object2", "Object2 Label"));
}
Also used : SyndesisMetadata(io.syndesis.connector.support.verifier.api.SyndesisMetadata) ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ObjectSchema

use of com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema in project syndesis by syndesisio.

the class SqlMetadataRetrieval method adaptForSql.

@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity" })
public SyndesisMetadata adaptForSql(final String actionId, final Map<String, Object> properties, final MetaData metadata) {
    final Map<String, List<PropertyPair>> enrichedProperties = new HashMap<>();
    final List<PropertyPair> ppList = new ArrayList<>();
    @SuppressWarnings("unchecked") final SqlStatementMetaData sqlStatementMetaData = (SqlStatementMetaData) metadata.getPayload();
    if (sqlStatementMetaData != null) {
        ppList.add(new PropertyPair(sqlStatementMetaData.getSqlStatement(), QUERY));
        enrichedProperties.put(QUERY, ppList);
        // build the input and output schemas
        final ObjectSchema builderIn = new ObjectSchema();
        builderIn.set$schema("http://json-schema.org/schema#");
        builderIn.setTitle("SQL_PARAM_IN");
        for (SqlParam inParam : sqlStatementMetaData.getInParams()) {
            builderIn.putProperty(inParam.getName(), schemaFor(inParam.getJdbcType()));
        }
        final ObjectSchema builderOut = new ObjectSchema();
        builderOut.setTitle("SQL_PARAM_OUT");
        builderOut.set$schema("http://json-schema.org/schema#");
        for (SqlParam outParam : sqlStatementMetaData.getOutParams()) {
            builderOut.putProperty(outParam.getName(), schemaFor(outParam.getJdbcType()));
        }
        try {
            DataShape.Builder inDataShapeBuilder = new DataShape.Builder().type(builderIn.getTitle());
            if (builderIn.getProperties().isEmpty()) {
                inDataShapeBuilder.kind(DataShapeKinds.NONE);
            } else {
                inDataShapeBuilder.kind(DataShapeKinds.JSON_SCHEMA).name("SQL Parameter").description(String.format("Parameters of SQL [%s]", sqlStatementMetaData.getSqlStatement())).specification(Json.writer().writeValueAsString(builderIn));
            }
            DataShape.Builder outDataShapeBuilder = new DataShape.Builder().type(builderOut.getTitle());
            if (builderOut.getProperties().isEmpty()) {
                outDataShapeBuilder.kind(DataShapeKinds.NONE);
            } else {
                outDataShapeBuilder.kind(DataShapeKinds.JSON_SCHEMA).name("SQL Result").description(String.format("Result of SQL [%s]", sqlStatementMetaData.getSqlStatement())).specification(Json.writer().writeValueAsString(builderOut));
            }
            return new SyndesisMetadata(enrichedProperties, inDataShapeBuilder.build(), outDataShapeBuilder.build());
        } catch (JsonProcessingException e) {
            throw new IllegalStateException(e);
        }
    } else {
        return new SyndesisMetadata(enrichedProperties, null, null);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PropertyPair(io.syndesis.connector.support.verifier.api.PropertyPair) DataShape(io.syndesis.common.model.DataShape) SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SyndesisMetadata(io.syndesis.connector.support.verifier.api.SyndesisMetadata) ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) ArrayList(java.util.ArrayList) List(java.util.List) SqlParam(io.syndesis.connector.sql.common.SqlParam) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with ObjectSchema

use of com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema in project syndesis by syndesisio.

the class JsonSchemaInspector method getPaths.

@Override
public List<String> getPaths(final String kind, final String type, final String specification, final Optional<byte[]> exemplar) {
    final ObjectSchema schema;
    try {
        schema = MAPPER.readerFor(ObjectSchema.class).readValue(specification);
    } catch (final IOException e) {
        LOG.warn("Unable to parse the given JSON schema, increase log level to DEBUG to see the schema being parsed");
        LOG.debug(specification);
        return Collections.emptyList();
    }
    final Map<String, JsonSchema> properties = schema.getProperties();
    final List<String> paths = new ArrayList<>();
    fetchPaths(null, paths, properties);
    return paths;
}
Also used : ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 5 with ObjectSchema

use of com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema in project syndesis by syndesisio.

the class JsonSchemaInspector method fetchPaths.

static void fetchPaths(final String context, final List<String> paths, final Map<String, JsonSchema> properties) {
    for (final Entry<String, JsonSchema> entry : properties.entrySet()) {
        final JsonSchema subschema = entry.getValue();
        String path;
        final String key = entry.getKey();
        if (context == null) {
            path = key;
        } else {
            path = context + "." + key;
        }
        if (subschema.isValueTypeSchema()) {
            paths.add(path);
        } else if (subschema.isObjectSchema()) {
            fetchPaths(path, paths, ((ObjectSchema) subschema).getProperties());
        }
    }
}
Also used : ObjectSchema(com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema) JsonSchema(com.fasterxml.jackson.module.jsonSchema.JsonSchema)

Aggregations

ObjectSchema (com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema)11 SyndesisMetadata (io.syndesis.connector.support.verifier.api.SyndesisMetadata)5 PropertyPair (io.syndesis.connector.support.verifier.api.PropertyPair)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonSchema (com.fasterxml.jackson.module.jsonSchema.JsonSchema)3 DataShape (io.syndesis.common.model.DataShape)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleTypeSchema (com.fasterxml.jackson.module.jsonSchema.types.SimpleTypeSchema)1 DataShapeKinds (io.syndesis.common.model.DataShapeKinds)1 Json (io.syndesis.common.util.Json)1 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 SqlParam (io.syndesis.connector.sql.common.SqlParam)1 SqlStatementMetaData (io.syndesis.connector.sql.common.SqlStatementMetaData)1