Java Code Examples for com.fasterxml.jackson.module.jsonSchema.JsonSchema#isObjectSchema()

The following examples show how to use com.fasterxml.jackson.module.jsonSchema.JsonSchema#isObjectSchema() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSchemaUtil.java    From mPaaS with Apache License 2.0 6 votes vote down vote up
/** 根据schema转换 */
public static Object convert(Object src, JsonSchema schema,
		boolean checkReadOnly, boolean ignoreException, String path) {
	if (src == null) {
		return null;
	}
	if (schema.isArraySchema()) {
		return convertArray(src, schema.asArraySchema(), checkReadOnly,
				ignoreException, path);
	} else if (schema.isObjectSchema()) {
		return convertObject(src, schema.asObjectSchema(), checkReadOnly,
				ignoreException, path);
	} else if (schema.isBooleanSchema()) {
		return TypeUtil.cast(src, Boolean.class);
	} else if (schema.isIntegerSchema()) {
		return TypeUtil.cast(src, Long.class);
	} else if (schema.isNumberSchema()) {
		return TypeUtil.cast(src, Double.class);
	} else if (schema.isStringSchema()) {
		return TypeUtil.cast(src, String.class);
	} else if (schema.isNullSchema()) {
		return null;
	} else {
		return src;
	}
}
 
Example 2
Source File: JsonSchemaUtil.java    From mPass with Apache License 2.0 6 votes vote down vote up
/** 根据schema转换 */
public static Object convert(Object src, JsonSchema schema,
		boolean checkReadOnly, boolean ignoreException, String path) {
	if (src == null) {
		return null;
	}
	if (schema.isArraySchema()) {
		return convertArray(src, schema.asArraySchema(), checkReadOnly,
				ignoreException, path);
	} else if (schema.isObjectSchema()) {
		return convertObject(src, schema.asObjectSchema(), checkReadOnly,
				ignoreException, path);
	} else if (schema.isBooleanSchema()) {
		return TypeUtil.cast(src, Boolean.class);
	} else if (schema.isIntegerSchema()) {
		return TypeUtil.cast(src, Long.class);
	} else if (schema.isNumberSchema()) {
		return TypeUtil.cast(src, Double.class);
	} else if (schema.isStringSchema()) {
		return TypeUtil.cast(src, String.class);
	} else if (schema.isNullSchema()) {
		return null;
	} else {
		return src;
	}
}
 
Example 3
Source File: JsonSchemaInspector.java    From syndesis with Apache License 2.0 6 votes vote down vote up
static void fetchPaths(final String context, final List<String> paths, final Map<String, JsonSchema> properties) {
    for (final Map.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, subschema.asObjectSchema().getProperties());
        } else if (subschema.isArraySchema()) {
            COLLECTION_PATHS.stream().map(p -> path + "." + p).forEach(paths::add);
            ObjectSchema itemSchema = getItemSchema(subschema.asArraySchema());
            if (itemSchema != null) {
                fetchPaths(path + ARRAY_CONTEXT, paths, itemSchema.getProperties());
            }
        }
    }
}
 
Example 4
Source File: AggregateMetadataHandler.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Converts unified Json body specification. Unified Json schema specifications hold
 * the actual body specification in a property. This method converts this body specification
 * from array to single element if necessary.
 */
private static String adaptUnifiedJsonBodySpecToSingleElement(String specification) throws IOException {
    JsonSchema schema = JsonSchemaUtils.reader().readValue(specification);
    if (schema.isObjectSchema()) {
        JsonSchema bodySchema = schema.asObjectSchema().getProperties().get(BODY);
        if (bodySchema != null && bodySchema.isArraySchema()) {
            ArraySchema.Items items = bodySchema.asArraySchema().getItems();
            if (items.isSingleItems()) {
                schema.asObjectSchema().getProperties().put(BODY, items.asSingleItems().getSchema());
                return JsonUtils.writer().writeValueAsString(schema);
            }
        }
    }

    return specification;
}
 
Example 5
Source File: AggregateMetadataHandler.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Converts unified Json body specification. Unified Json schema specifications hold
 * the actual body specification in a property. This method converts this body specification
 * from single element to collection if necessary.
 */
private static String adaptUnifiedJsonBodySpecToCollection(String specification) throws IOException {
    JsonSchema schema = JsonSchemaUtils.reader().readValue(specification);
    if (schema.isObjectSchema()) {
        JsonSchema bodySchema = schema.asObjectSchema().getProperties().get(BODY);
        if (bodySchema != null && bodySchema.isObjectSchema()) {
            ArraySchema arraySchema = new ArraySchema();
            arraySchema.set$schema(JSON_SCHEMA_ORG_SCHEMA);
            arraySchema.setItemsSchema(bodySchema);
            schema.asObjectSchema().getProperties().put(BODY, arraySchema);
            return JsonUtils.writer().writeValueAsString(schema);
        }
    }

    return specification;
}
 
Example 6
Source File: SplitMetadataHandler.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Extract unified Json body specification from data shape specification. Unified Json schema specifications hold
 * the actual body specification in a property. This method extracts this property as new body specification.
 */
private static String extractUnifiedJsonBodySpec(String specification) throws IOException {
    JsonSchema schema = JsonSchemaUtils.reader().readValue(specification);
    if (schema.isObjectSchema()) {
        JsonSchema bodySchema = schema.asObjectSchema().getProperties().get("body");
        if (bodySchema != null) {
            if (bodySchema.isArraySchema()) {
                ArraySchema.Items items = bodySchema.asArraySchema().getItems();
                if (items.isSingleItems()) {
                    JsonSchema itemSchema = items.asSingleItems().getSchema();
                    itemSchema.set$schema(schema.get$schema());
                    return JsonUtils.writer().writeValueAsString(itemSchema);
                }
            } else {
                return JsonUtils.writer().writeValueAsString(bodySchema);
            }
        }
    }

    return specification;
}
 
Example 7
Source File: Doc.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private void findRefs(JsonSchema schema, Map<String, JsonSchema> refs, Set<String> referenced) {
  addRef(schema, refs);
  if (schema instanceof ReferenceSchema) {
    referenced.add(schema.get$ref());
  } else if (schema.isArraySchema()) {
    ArraySchema as = schema.asArraySchema();
    if (as.getItems() != null) {
      if (as.getItems().isSingleItems()) {
        findRefs(as.getItems().asSingleItems().getSchema(), refs, referenced);
      } else if (as.getItems().isArrayItems()) {
        ArrayItems items = as.getItems().asArrayItems();
        for (JsonSchema item : items.getJsonSchemas()) {
          findRefs(item, refs, referenced);
        }
      } else {
        throw new UnsupportedOperationException(as.getItems().toString());
      }
    }
  } else if (schema.isObjectSchema()) {
    ObjectSchema os = schema.asObjectSchema();
    for (JsonSchema value : os.getProperties().values()) {
      findRefs(value, refs, referenced);
    }
  }
}
 
Example 8
Source File: GoogleSheetsMetadataRetrieval.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static void applyMetadata(DataShape.Builder builder, JsonSchema spec) {
    if (spec.isObjectSchema()) {
        builder.putMetadata("variant", "element");
    }

    if (spec.isArraySchema()) {
        builder.putMetadata("variant", "collection");
    }
}
 
Example 9
Source File: JsonSchemaInspector.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getPaths(final String kind, final String type, final String specification,
    final Optional<byte[]> exemplar) {
    final JsonSchema schema;
    try {
        schema = JsonSchemaUtils.reader().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", e);
        LOG.debug(specification);

        return Collections.emptyList();
    }

    String context = null;
    final List<String> paths = new ArrayList<>();
    ObjectSchema objectSchema;
    if (schema.isObjectSchema()) {
        objectSchema = schema.asObjectSchema();
    } else if (schema.isArraySchema()) {
        objectSchema = getItemSchema(schema.asArraySchema());
        // add collection specific paths
        paths.addAll(COLLECTION_PATHS);
        context = ARRAY_CONTEXT;
    } else {
        throw new IllegalStateException(String.format("Unexpected schema type %s - expected object or array schema", schema.getType()));
    }

    if (objectSchema != null) {
        final Map<String, JsonSchema> properties = objectSchema.getProperties();
        fetchPaths(context, paths, properties);
    }

    return Collections.unmodifiableList(paths);
}
 
Example 10
Source File: Doc.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * generates a json example
 * @param sb the output
 * @param indent the current indent prefix
 * @param schema the schema to print an example for
 * @param refs for referenced schema (when used more than once)
 * @param followed to avoid infinite loops in recursive schema
 * @param referenced the types that are referenced and should have an annotation
 */
private void example(StringBuilder sb, int maxlength, String indent, JsonSchema schema, Map<String, JsonSchema> refs, Set<String> followed, Set<String> referenced) {
  if (sb.length() > maxlength) {
    sb.append("example is too big, truncated...");
    return;
  }
  followed = new HashSet<>(followed);
  String id = schema.getId();
  if (id == null && schema instanceof ReferenceSchema) {
    id = schema.get$ref();
  }
  if (id != null && followed.contains(id)) {
    sb.append(refExample(id));
    return;
  }
  if (schema.isObjectSchema()) {
    followed.add(id);
    objectExample(sb, maxlength, indent, schema, refs, followed, referenced, id);
  } else if (schema.isArraySchema()) {
    arrayExample(sb, maxlength, indent, schema, refs, followed, referenced);
  } else if (schema instanceof ReferenceSchema) {
    if (refs.containsKey(schema.get$ref())) {
      example(sb, maxlength, indent, refs.get(schema.get$ref()), refs, followed, referenced);
    } else {
      sb.append(refExample(schema.get$ref()));
    }
  } else {
    sb.append(primitiveTypeExample(schema));
  }
}
 
Example 11
Source File: SqlMetadataRetrieval.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@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<>();
    @SuppressWarnings("unchecked")
    final SqlStatementMetaData sqlStatementMetaData = (SqlStatementMetaData) metadata.getPayload();

    if (sqlStatementMetaData != null) {
        enrichedProperties.put(QUERY, Collections.singletonList(new PropertyPair(sqlStatementMetaData.getSqlStatement())));

        // build the input and output schemas
        final JsonSchema specIn;
        final ObjectSchema builderIn = new ObjectSchema();
        builderIn.setTitle("SQL_PARAM_IN");

        if (sqlStatementMetaData.isVerifiedBatchUpdateMode()) {
            ArraySchema arraySpec = new ArraySchema();
            arraySpec.set$schema(JSON_SCHEMA_ORG_SCHEMA);
            arraySpec.setItemsSchema(builderIn);
            specIn = arraySpec;
        } else {
            builderIn.set$schema(JSON_SCHEMA_ORG_SCHEMA);
            specIn = builderIn;
        }

        for (SqlParam inParam: sqlStatementMetaData.getInParams()) {
            builderIn.putProperty(inParam.getName(), schemaFor(inParam.getJdbcType()));
        }

        final ObjectSchema builderOut = new ObjectSchema();
        builderOut.setTitle("SQL_PARAM_OUT");
        for (SqlParam outParam: sqlStatementMetaData.getOutParams()) {
            builderOut.putProperty(outParam.getName(), schemaFor(outParam.getJdbcType()));
        }
        final ArraySchema outputSpec = new ArraySchema();
        outputSpec.set$schema(JSON_SCHEMA_ORG_SCHEMA);
        outputSpec.setItemsSchema(builderOut);

        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(JsonUtils.writer().writeValueAsString(specIn));

                if (specIn.isObjectSchema()) {
                    inDataShapeBuilder.putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_ELEMENT);
                }

                if (specIn.isArraySchema()) {
                    inDataShapeBuilder.putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION);
                }
            }
            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()))
                    .putMetadata(DataShapeMetaData.VARIANT, DataShapeMetaData.VARIANT_COLLECTION)
                    .specification(JsonUtils.writer().writeValueAsString(outputSpec));
            }

            return new SyndesisMetadata(enrichedProperties,
                    inDataShapeBuilder.build(), outDataShapeBuilder.build());
        } catch (JsonProcessingException e) {
            throw new IllegalStateException(e);
        }
    } else {
        return new SyndesisMetadata(enrichedProperties, null, null);
    }
}