ahmedabu98 commented on code in PR #25927:
URL: https://github.com/apache/beam/pull/25927#discussion_r1170393253
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/JsonUtils.java:
##########
@@ -128,50 +134,151 @@ public String apply(Row input) {
};
}
+ public static String jsonSchemaStringFromBeamSchema(Schema beamSchema) {
+ return jsonSchemaFromBeamSchema(beamSchema).toString();
+ }
+
+ public static ObjectSchema jsonSchemaFromBeamSchema(Schema beamSchema) {
+ return jsonSchemaBuilderFromBeamSchema(beamSchema).build();
+ }
+
+ private static ObjectSchema.Builder jsonSchemaBuilderFromBeamSchema(Schema
beamSchema) {
+ // Beam Schema is strict, so we should not accept additional properties
+ ObjectSchema.Builder jsonSchemaBuilder =
ObjectSchema.builder().additionalProperties(false);
+
+ for (Field field : beamSchema.getFields()) {
+ String name = field.getName();
+ org.everit.json.schema.Schema propertySchema =
jsonPropertyFromBeamType(field.getType());
+
+ // Add property and make it required
+ jsonSchemaBuilder = jsonSchemaBuilder.addPropertySchema(name,
propertySchema);
+ jsonSchemaBuilder.addRequiredProperty(name);
+ }
+
+ return jsonSchemaBuilder;
+ }
+
+ private static org.everit.json.schema.Schema
jsonPropertyFromBeamType(FieldType beamType) {
+ org.everit.json.schema.Schema.Builder<? extends
org.everit.json.schema.Schema> propertySchema;
+
+ switch (beamType.getTypeName()) {
+ case BYTE:
+ case INT16:
+ case INT32:
+ case INT64:
+ propertySchema = NumberSchema.builder().requiresInteger(true);
Review Comment:
Hmmm I'm not sure I understand. This library is for building json schemas,
so it wouldn't be parsing any values. As far as these lines go, we're only
concerned with whether the number is an integer or not.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]