@@ -36,23 +36,24 @@ public final class BsonDocumentToSchema {
3636 private static final Schema DEFAULT_INFER_SCHEMA_TYPE = Schema .OPTIONAL_STRING_SCHEMA ;
3737 public static final String SCHEMA_NAME_TEMPLATE = "inferred_name_%s" ;
3838
39- public static Schema inferDocumentSchema (final BsonDocument document , final boolean optional ) {
39+ public static Schema inferDocumentSchema (
40+ final BsonDocument document , final boolean optional , final String fieldName ) {
4041 SchemaBuilder builder = SchemaBuilder .struct ();
4142 if (document .containsKey (ID_FIELD )) {
42- builder .field (ID_FIELD , inferSchema (document .get (ID_FIELD )));
43+ builder .field (ID_FIELD , inferSchema (document .get (ID_FIELD ), ID_FIELD ));
4344 }
4445 document .entrySet ().stream ()
4546 .filter (kv -> !kv .getKey ().equals (ID_FIELD ))
4647 .sorted (Map .Entry .comparingByKey ())
47- .forEach (kv -> builder .field (kv .getKey (), inferSchema (kv .getValue ())));
48- builder .name (generateName (builder ));
48+ .forEach (kv -> builder .field (kv .getKey (), inferSchema (kv .getValue (), kv . getKey () )));
49+ builder .name (generateName (builder , fieldName ));
4950 if (optional ) {
5051 builder .optional ();
5152 }
5253 return builder .build ();
5354 }
5455
55- public static Schema inferSchema (final BsonValue bsonValue ) {
56+ public static Schema inferSchema (final BsonValue bsonValue , final String fieldName ) {
5657 switch (bsonValue .getBsonType ()) {
5758 case BOOLEAN :
5859 return Schema .OPTIONAL_BOOLEAN_SCHEMA ;
@@ -70,16 +71,17 @@ public static Schema inferSchema(final BsonValue bsonValue) {
7071 case TIMESTAMP :
7172 return Timestamp .builder ().optional ().build ();
7273 case DOCUMENT :
73- return inferDocumentSchema (bsonValue .asDocument (), true );
74+ return inferDocumentSchema (bsonValue .asDocument (), true , fieldName );
7475 case ARRAY :
7576 List <BsonValue > values = bsonValue .asArray ().getValues ();
7677 Schema firstItemSchema =
77- values .isEmpty () ? DEFAULT_INFER_SCHEMA_TYPE : inferSchema (values .get (0 ));
78+ values .isEmpty () ? DEFAULT_INFER_SCHEMA_TYPE : inferSchema (values .get (0 ), fieldName );
7879 if (values .isEmpty ()
79- || values .stream ().anyMatch (bv -> !Objects .equals (inferSchema (bv ), firstItemSchema ))) {
80+ || values .stream ()
81+ .anyMatch (bv -> !Objects .equals (inferSchema (bv , fieldName ), firstItemSchema ))) {
8082 return SchemaBuilder .array (DEFAULT_INFER_SCHEMA_TYPE ).optional ().build ();
8183 }
82- return SchemaBuilder .array (inferSchema (bsonValue .asArray ().getValues ().get (0 )))
84+ return SchemaBuilder .array (inferSchema (bsonValue .asArray ().getValues ().get (0 ), fieldName ))
8385 .optional ()
8486 .build ();
8587 case BINARY :
@@ -101,8 +103,9 @@ public static Schema inferSchema(final BsonValue bsonValue) {
101103 }
102104 }
103105
104- public static String generateName (final SchemaBuilder builder ) {
105- return format (SCHEMA_NAME_TEMPLATE , Objects .hashCode (builder .build ())).replace ("-" , "_" );
106+ public static String generateName (final SchemaBuilder builder , final String fieldName ) {
107+ builder .build ();
108+ return format (SCHEMA_NAME_TEMPLATE , fieldName );
106109 }
107110
108111 private BsonDocumentToSchema () {}
0 commit comments