Skip to content

Commit 41d3060

Browse files
committed
Warnings removal, cosmetic changes
1 parent 67c6042 commit 41d3060

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/RecordVisitor.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ public class RecordVisitor
2626
protected final VisitorFormatWrapperImpl _visitorWrapper;
2727

2828
/**
29-
* Tracks if the schema for this record has been overridden (by an annotation or other means), and calls to the {@code property} and
30-
* {@code optionalProperty} methods should be ignored.
29+
* Tracks if the schema for this record has been overridden (by an annotation or other means),
30+
* and calls to the {@code property} and {@code optionalProperty} methods should be ignored.
3131
*/
3232
protected final boolean _overridden;
3333

3434
/**
35-
* When Avro schema for this JavaType ({@code _type}) results in UNION of multiple Avro types, _typeSchema keeps track
36-
* which Avro type in the UNION represents this JavaType ({@code _type}) so that fields of this JavaType can be set to the right Avro type by {@code builtAvroSchema()}.
37-
*
35+
* When Avro schema for this JavaType ({@code _type}) results in UNION of multiple Avro types,
36+
* _typeSchema keeps track of which Avro type in the UNION represents this JavaType ({@code _type})
37+
* so that fields of this JavaType can be set to the right Avro type by {@code builtAvroSchema()}.
38+
*<br>
3839
* Example:
3940
* <pre>
4041
* @JsonSubTypes({
@@ -45,7 +46,7 @@ public class RecordVisitor
4546
* class Apple extends Fruit {}
4647
* class Orange extends Fruit {}
4748
* </pre>
48-
* When _type = Fruit.class
49+
* When {@code _type = Fruit.class}
4950
* Then
5051
* _avroSchema if Fruit.class is union of Fruit record, Apple record and Orange record schemas: [
5152
* { name: Fruit, type: record, fields: [..] }, <--- _typeSchema points here
@@ -56,10 +57,15 @@ public class RecordVisitor
5657
*
5758
* FIXME: When _typeSchema is not null, then _overridden must be true, therefore (_overridden == true) can be replaced with (_typeSchema != null),
5859
* but it might be considered API change cause _overridden has protected access modifier.
60+
*
61+
* @since 2.19.1
5962
*/
60-
private Schema _typeSchema;
63+
private final Schema _typeSchema;
64+
65+
// !!! 19-May-2025: TODO: make final in 2.20
6166
protected Schema _avroSchema;
6267

68+
// !!! 19-May-2025: TODO: make final in 2.20
6369
protected List<Schema.Field> _fields = new ArrayList<>();
6470

6571
public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperImpl visitorWrapper)
@@ -73,6 +79,7 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
7379
if (ann != null) {
7480
_avroSchema = AvroSchemaHelper.parseJsonSchema(ann.value());
7581
_overridden = true;
82+
_typeSchema = null;
7683
} else {
7784
// If Avro schema for this _type results in UNION I want to know Avro type where to assign fields
7885
_typeSchema = AvroSchemaHelper.initializeRecordSchema(bean);
@@ -89,12 +96,14 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
8996
Set<Class<?>> alreadySeenClasses = new HashSet<>();
9097
alreadySeenClasses.add(_type.getRawClass());
9198

92-
// At this point calculating hashCode for _typeSchema fails with NPE because RecordSchema.fields is NULL
93-
// see org.apache.avro.Schema.RecordSchema#computeHash.
94-
// Therefore, unionSchemas must not be HashSet (or any other type using hashCode() for equality check).
99+
// At this point calculating hashCode for _typeSchema fails with
100+
// NPE because RecordSchema.fields is NULL
101+
// (see org.apache.avro.Schema.RecordSchema#computeHash).
102+
// Therefore, unionSchemas must not be HashSet (or any other type
103+
// using hashCode() for equality check).
95104
// Set ensures that each subType schema is once in resulting union.
96105
// IdentityHashMap is used because it is using reference-equality.
97-
Set<Schema> unionSchemas = Collections.newSetFromMap(new IdentityHashMap<>());
106+
final Set<Schema> unionSchemas = Collections.newSetFromMap(new IdentityHashMap<>());
98107
// Initialize with this schema
99108
if (_type.isConcrete()) {
100109
unionSchemas.add(_typeSchema);

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/PolymorphicTypeAnnotationsTest.java

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.fasterxml.jackson.dataformat.avro.schema;
22

3+
import org.junit.jupiter.api.Test;
4+
35
import com.fasterxml.jackson.annotation.JsonSubTypes;
4-
import com.fasterxml.jackson.databind.JsonMappingException;
56
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
67
import com.fasterxml.jackson.dataformat.avro.annotation.AvroNamespace;
8+
79
import org.apache.avro.Schema;
810
import org.apache.avro.reflect.Union;
9-
import org.junit.jupiter.api.Test;
10-
11-
import java.io.IOException;
1211

1312
import static org.assertj.core.api.Assertions.assertThat;
1413

@@ -22,31 +21,31 @@ public class PolymorphicTypeAnnotationsTest {
2221
@JsonSubTypes.Type(value = Cat.class),
2322
@JsonSubTypes.Type(value = Dog.class),
2423
})
25-
private interface AnimalInterface {
24+
interface AnimalInterface {
2625
}
2726

28-
private static abstract class AbstractMammal implements AnimalInterface {
27+
static abstract class AbstractMammal implements AnimalInterface {
2928
public int legs;
3029
}
3130

32-
private static class Cat extends AbstractMammal {
31+
static class Cat extends AbstractMammal {
3332
public String color;
3433
}
3534

36-
private static class Dog extends AbstractMammal {
35+
static class Dog extends AbstractMammal {
3736
public int size;
3837
}
3938

4039
@Test
41-
public void subclasses_of_interface_test() throws JsonMappingException {
40+
public void subclasses_of_interface_test() throws Exception {
4241
// GIVEN
4342
final Schema catSchema = MAPPER.schemaFor(Cat.class).getAvroSchema();
4443
final Schema dogSchema = MAPPER.schemaFor(Dog.class).getAvroSchema();
4544

4645
// WHEN
4746
Schema actualSchema = MAPPER.schemaFor(AnimalInterface.class).getAvroSchema();
4847

49-
System.out.println("Animal schema:\n" + actualSchema.toString(true));
48+
// System.out.println("Animal schema:\n" + actualSchema.toString(true));
5049

5150
// THEN
5251
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -59,22 +58,22 @@ public void subclasses_of_interface_test() throws JsonMappingException {
5958
@JsonSubTypes.Type(value = Pear.class),
6059
})
6160
@AvroNamespace(TEST_NAMESPACE) // @AvroNamespace makes it easier to create schema string representation
62-
private static class Fruit {
61+
static class Fruit {
6362
public boolean eatable;
6463
}
6564

6665
private static final String FRUIT_ITSELF_SCHEMA_STR = "{\"type\":\"record\",\"name\":\"Fruit\",\"namespace\":\"test\",\"fields\":[{\"name\":\"eatable\",\"type\":\"boolean\"}]}";
6766

68-
private static class Apple extends Fruit {
67+
static class Apple extends Fruit {
6968
public String color;
7069
}
7170

72-
private static class Pear extends Fruit {
71+
static class Pear extends Fruit {
7372
public int seeds;
7473
}
7574

7675
@Test
77-
public void jsonSubTypes_on_concrete_class_test() throws IOException {
76+
public void jsonSubTypes_on_concrete_class_test() throws Exception {
7877
// GIVEN
7978
final Schema fruitItselfSchema = MAPPER.schemaFrom(FRUIT_ITSELF_SCHEMA_STR).getAvroSchema();
8079
final Schema appleSchema = MAPPER.schemaFor(Apple.class).getAvroSchema();
@@ -83,7 +82,7 @@ public void jsonSubTypes_on_concrete_class_test() throws IOException {
8382
// WHEN
8483
Schema actualSchema = MAPPER.schemaFor(Fruit.class).getAvroSchema();
8584

86-
System.out.println("Fruit schema:\n" + actualSchema.toString(true));
85+
// System.out.println("Fruit schema:\n" + actualSchema.toString(true));
8786

8887
// THEN
8988
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -95,7 +94,7 @@ public void jsonSubTypes_on_concrete_class_test() throws IOException {
9594
@JsonSubTypes.Type(value = AbstractWaterVehicle.class),
9695
})
9796
@AvroNamespace(TEST_NAMESPACE)
98-
private static class Vehicle {
97+
static class Vehicle {
9998
}
10099

101100
private static final String VEHICLE_ITSELF_SCHEMA_STR = "{\"type\":\"record\",\"name\":\"Vehicle\",\"namespace\":\"test\",\"fields\":[]}";
@@ -105,33 +104,33 @@ private static class Vehicle {
105104
@JsonSubTypes.Type(value = MotorCycle.class),
106105
})
107106
@AvroNamespace(TEST_NAMESPACE)
108-
private static class LandVehicle extends Vehicle {
107+
static class LandVehicle extends Vehicle {
109108
}
110109

111110
private static final String LAND_VEHICLE_ITSELF_SCHEMA_STR = "{\"type\":\"record\",\"name\":\"LandVehicle\",\"namespace\":\"test\",\"fields\":[]}";
112111

113-
private static class Car extends LandVehicle {
112+
static class Car extends LandVehicle {
114113
}
115114

116-
private static class MotorCycle extends LandVehicle {
115+
static class MotorCycle extends LandVehicle {
117116
}
118117

119118
@JsonSubTypes({
120119
@JsonSubTypes.Type(value = Boat.class),
121120
@JsonSubTypes.Type(value = Submarine.class),
122121
})
123-
private static abstract class AbstractWaterVehicle extends Vehicle {
122+
static abstract class AbstractWaterVehicle extends Vehicle {
124123
public int propellers;
125124
}
126125

127-
private static class Boat extends AbstractWaterVehicle {
126+
static class Boat extends AbstractWaterVehicle {
128127
}
129128

130-
private static class Submarine extends AbstractWaterVehicle {
129+
static class Submarine extends AbstractWaterVehicle {
131130
}
132131

133132
@Test
134-
public void jsonSubTypes_of_jsonSubTypes_test() throws IOException {
133+
public void jsonSubTypes_of_jsonSubTypes_test() throws Exception {
135134
// GIVEN
136135
final Schema vehicleItselfSchema = MAPPER.schemaFrom(VEHICLE_ITSELF_SCHEMA_STR).getAvroSchema();
137136
final Schema landVehicleItselfSchema = MAPPER.schemaFrom(LAND_VEHICLE_ITSELF_SCHEMA_STR).getAvroSchema();
@@ -143,7 +142,7 @@ public void jsonSubTypes_of_jsonSubTypes_test() throws IOException {
143142
// WHEN
144143
Schema actualSchema = MAPPER.schemaFor(Vehicle.class).getAvroSchema();
145144

146-
System.out.println("Vehicle schema:\n" + actualSchema.toString(true));
145+
// System.out.println("Vehicle schema:\n" + actualSchema.toString(true));
147146

148147
// THEN
149148
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -180,15 +179,15 @@ private static class Oxygen extends AbstractGas {
180179
}
181180

182181
@Test
183-
public void class_is_referenced_twice_in_hierarchy_test() throws JsonMappingException {
182+
public void class_is_referenced_twice_in_hierarchy_test() throws Exception {
184183
// GIVEN
185184
final Schema heliumSchema = MAPPER.schemaFor(Helium.class).getAvroSchema();
186185
final Schema oxygenSchema = MAPPER.schemaFor(Oxygen.class).getAvroSchema();
187186

188187
// WHEN
189188
Schema actualSchema = MAPPER.schemaFor(ElementInterface.class).getAvroSchema();
190189

191-
System.out.println("ElementInterface schema:\n" + actualSchema.toString(true));
190+
// System.out.println("ElementInterface schema:\n" + actualSchema.toString(true));
192191

193192
// THEN
194193
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -203,19 +202,19 @@ public void class_is_referenced_twice_in_hierarchy_test() throws JsonMappingExce
203202
@JsonSubTypes.Type(value = Png.class),
204203
})
205204
@AvroNamespace(TEST_NAMESPACE) // @AvroNamespace makes it easier to create schema string representation
206-
private static class Image {
205+
static class Image {
207206
}
208207

209208
private static final String IMAGE_ITSELF_SCHEMA_STR = "{\"type\":\"record\",\"name\":\"Image\",\"namespace\":\"test\",\"fields\":[]}";
210209

211-
private static class Jpeg extends Image {
210+
static class Jpeg extends Image {
212211
}
213212

214-
private static class Png extends Image {
213+
static class Png extends Image {
215214
}
216215

217216
@Test
218-
public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws IOException {
217+
public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws Exception {
219218
// GIVEN
220219
final Schema imageItselfSchema = MAPPER.schemaFrom(IMAGE_ITSELF_SCHEMA_STR).getAvroSchema();
221220
final Schema jpegSchema = MAPPER.schemaFor(Jpeg.class).getAvroSchema();
@@ -224,7 +223,7 @@ public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws IOExc
224223
// WHEN
225224
Schema actualSchema = MAPPER.schemaFor(Image.class).getAvroSchema();
226225

227-
System.out.println("Image schema:\n" + actualSchema.toString(true));
226+
// System.out.println("Image schema:\n" + actualSchema.toString(true));
228227

229228
// THEN
230229
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -236,19 +235,19 @@ public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws IOExc
236235
Sport.class,
237236
Football.class, Basketball.class})
238237
@AvroNamespace(TEST_NAMESPACE) // @AvroNamespace makes it easier to create schema string representation
239-
private static class Sport {
238+
static class Sport {
240239
}
241240

242241
private static final String SPORT_ITSELF_SCHEMA_STR = "{\"type\":\"record\",\"name\":\"Sport\",\"namespace\":\"test\",\"fields\":[]}";
243242

244-
private static class Football extends Sport {
243+
static class Football extends Sport {
245244
}
246245

247-
private static class Basketball extends Sport {
246+
static class Basketball extends Sport {
248247
}
249248

250249
@Test
251-
public void base_class_explicitly_in_Union_annotation_test() throws IOException {
250+
public void base_class_explicitly_in_Union_annotation_test() throws Exception {
252251
// GIVEN
253252
final Schema sportItselfSchema = MAPPER.schemaFrom(SPORT_ITSELF_SCHEMA_STR).getAvroSchema();
254253
final Schema footballSchema = MAPPER.schemaFor(Football.class).getAvroSchema();
@@ -257,7 +256,7 @@ public void base_class_explicitly_in_Union_annotation_test() throws IOException
257256
// WHEN
258257
Schema actualSchema = MAPPER.schemaFor(Sport.class).getAvroSchema();
259258

260-
System.out.println("Sport schema:\n" + actualSchema.toString(true));
259+
//System.out.println("Sport schema:\n" + actualSchema.toString(true));
261260

262261
// THEN
263262
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
@@ -268,29 +267,28 @@ public void base_class_explicitly_in_Union_annotation_test() throws IOException
268267
// Interface being explicitly in @Union led to StackOverflowError exception.
269268
DocumentInterface.class,
270269
Word.class, Excel.class})
271-
private interface DocumentInterface {
270+
interface DocumentInterface {
272271
}
273272

274-
private static class Word implements DocumentInterface {
273+
static class Word implements DocumentInterface {
275274
}
276275

277-
private static class Excel implements DocumentInterface {
276+
static class Excel implements DocumentInterface {
278277
}
279278

280279
@Test
281-
public void interface_explicitly_in_Union_annotation_test() throws IOException {
280+
public void interface_explicitly_in_Union_annotation_test() throws Exception {
282281
// GIVEN
283282
final Schema wordSchema = MAPPER.schemaFor(Word.class).getAvroSchema();
284283
final Schema excelSchema = MAPPER.schemaFor(Excel.class).getAvroSchema();
285284

286285
// WHEN
287286
Schema actualSchema = MAPPER.schemaFor(DocumentInterface.class).getAvroSchema();
288287

289-
System.out.println("Document schema:\n" + actualSchema.toString(true));
288+
//System.out.println("Document schema:\n" + actualSchema.toString(true));
290289

291290
// THEN
292291
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
293292
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(wordSchema, excelSchema);
294293
}
295-
296294
}

0 commit comments

Comments
 (0)