@@ -85,56 +85,30 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
85
85
86
86
List <NamedType > subTypes = getProvider ().getAnnotationIntrospector ().findSubtypes (bean .getClassInfo ());
87
87
if (subTypes != null && !subTypes .isEmpty ()) {
88
-
89
88
// At this point calculating hashCode for _typeSchema fails with NPE because RecordSchema.fields is NULL
90
- // (see org.apache.avro.Schema.RecordSchema#computeHash).
91
- // Therefore, _typeSchema must be added into union at very end, or unionSchemas must not be HashSet (or any
92
- // other type calling hashCode() for equality check).
93
- Set <Schema > unionSchemas = new HashSet <>();
94
- // ArrayList<Schema> unionSchemas = new ArrayList<>();
95
-
89
+ // see org.apache.avro.Schema.RecordSchema#computeHash.
90
+ // Therefore, unionSchemas must not be HashSet (or any other type using hashCode() for equality check).
96
91
// IdentityHashMap is used because it is using reference-equality.
97
- // Set<Schema> unionSchemas = Collections.newSetFromMap(new IdentityHashMap<>());
98
-
99
- // Initialize with this schema is
100
- // if (_type.isConcrete()) {
101
- // unionSchemas.add(_typeSchema);
102
- // }
103
-
92
+ Set <Schema > unionSchemas = Collections .newSetFromMap (new IdentityHashMap <>());
93
+ // Initialize with this schema
94
+ if (_type .isConcrete ()) {
95
+ unionSchemas .add (_typeSchema );
96
+ }
104
97
try {
105
98
for (NamedType subType : subTypes ) {
106
99
JsonSerializer <?> ser = getProvider ().findValueSerializer (subType .getType ());
107
100
VisitorFormatWrapperImpl visitor = _visitorWrapper .createChildWrapper ();
108
101
ser .acceptJsonFormatVisitor (visitor , getProvider ().getTypeFactory ().constructType (subType .getType ()));
109
- Schema subTypeSchema = visitor .getAvroSchema ();
110
102
// Add subType schema into this union, unless it is already there.
103
+ Schema subTypeSchema = visitor .getAvroSchema ();
111
104
// When subType schema is union itself, include each its type into this union if not there already
112
105
if (subTypeSchema .getType () == Type .UNION ) {
113
- // subTypeSchema.getTypes().stream()
114
- // .filter(unionElement -> !unionSchemas.contains(unionElement))
115
- // .forEach(unionSchemas::add);
116
- // or
117
- // for( Schema unionElement: subTypeSchema.getTypes()) {
118
- // if (unionSchemas.contains(unionElement)) {
119
- // continue;
120
- // }
121
- // unionSchemas.add(unionElement);
122
- // }
123
106
unionSchemas .addAll (subTypeSchema .getTypes ());
124
107
} else {
125
- // if (!unionSchemas.contains(subTypeSchema)) {
126
- // unionSchemas.add(subTypeSchema);
127
- // }
128
108
unionSchemas .add (subTypeSchema );
129
109
}
130
110
}
131
-
132
- ArrayList <Schema > unionList = new ArrayList <>(unionSchemas );
133
- // add _type schema into union
134
- if (_type .isConcrete ()) {
135
- unionList .add (_typeSchema );
136
- }
137
- _avroSchema = Schema .createUnion (unionList );
111
+ _avroSchema = Schema .createUnion (new ArrayList <>(unionSchemas ));
138
112
} catch (JsonMappingException jme ) {
139
113
throw new RuntimeException ("Failed to build schema" , jme );
140
114
}
0 commit comments