@@ -85,56 +85,30 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
8585
8686 List <NamedType > subTypes = getProvider ().getAnnotationIntrospector ().findSubtypes (bean .getClassInfo ());
8787 if (subTypes != null && !subTypes .isEmpty ()) {
88-
8988 // 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).
9691 // 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+ }
10497 try {
10598 for (NamedType subType : subTypes ) {
10699 JsonSerializer <?> ser = getProvider ().findValueSerializer (subType .getType ());
107100 VisitorFormatWrapperImpl visitor = _visitorWrapper .createChildWrapper ();
108101 ser .acceptJsonFormatVisitor (visitor , getProvider ().getTypeFactory ().constructType (subType .getType ()));
109- Schema subTypeSchema = visitor .getAvroSchema ();
110102 // Add subType schema into this union, unless it is already there.
103+ Schema subTypeSchema = visitor .getAvroSchema ();
111104 // When subType schema is union itself, include each its type into this union if not there already
112105 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- // }
123106 unionSchemas .addAll (subTypeSchema .getTypes ());
124107 } else {
125- // if (!unionSchemas.contains(subTypeSchema)) {
126- // unionSchemas.add(subTypeSchema);
127- // }
128108 unionSchemas .add (subTypeSchema );
129109 }
130110 }
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 ));
138112 } catch (JsonMappingException jme ) {
139113 throw new RuntimeException ("Failed to build schema" , jme );
140114 }
0 commit comments