Skip to content

Commit 60b2dab

Browse files
authored
[AVRO] Make RecordVisitor _avroSchema and _fields properties final (#600)
1 parent e57dadb commit 60b2dab

File tree

6 files changed

+7
-32
lines changed

6 files changed

+7
-32
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ public class RecordVisitor
6262
*/
6363
private final Schema _typeSchema;
6464

65-
// !!! 19-May-2025: TODO: make final in 2.20
66-
protected Schema _avroSchema;
65+
protected final Schema _avroSchema;
6766

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

7169
public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperImpl visitorWrapper)
7270
{
@@ -82,12 +80,11 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
8280
_typeSchema = null;
8381
} else {
8482
// If Avro schema for this _type results in UNION I want to know Avro type where to assign fields
85-
_avroSchema = AvroSchemaHelper.initializeRecordSchema(bean);
86-
_typeSchema = _avroSchema;
83+
_typeSchema = AvroSchemaHelper.initializeRecordSchema(bean);
8784
_overridden = false;
8885
AvroMeta meta = bean.getClassInfo().getAnnotation(AvroMeta.class);
8986
if (meta != null) {
90-
_avroSchema.addProp(meta.key(), meta.value());
87+
_typeSchema.addProp(meta.key(), meta.value());
9188
}
9289

9390
List<NamedType> subTypes = getProvider().getAnnotationIntrospector().findSubtypes(bean.getClassInfo());
@@ -126,10 +123,12 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
126123
unionSchemas.add(subTypeSchema);
127124
}
128125
}
129-
_avroSchema = Schema.createUnion(new ArrayList<>(unionSchemas));
130126
} catch (JsonMappingException jme) {
131127
throw new RuntimeJsonMappingException("Failed to build schema", jme);
132128
}
129+
_avroSchema = Schema.createUnion(new ArrayList<>(unionSchemas));
130+
} else {
131+
_avroSchema = _typeSchema;
133132
}
134133
}
135134
_visitorWrapper.getSchemas().addSchema(type, _avroSchema);

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/BigDecimal_schemaCreationTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public void testSchemaCreation_withLogicalTypesDisabled_onBigDecimalWithAvroDeci
3939
// because logical types are disabled by default.
4040
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
4141

42-
System.out.println(BigDecimalWithAvroDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));
43-
4442
// THEN
4543
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();
4644
Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema();
@@ -59,8 +57,6 @@ public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithAvroDecim
5957
MAPPER.acceptJsonFormatVisitor(BigDecimalWithAvroDecimalAnnotationWrapper.class, gen);
6058
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
6159

62-
System.out.println(BigDecimalWithAvroDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));
63-
6460
// THEN
6561
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();
6662
Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema();
@@ -90,8 +86,6 @@ public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithAvroDecim
9086
MAPPER.acceptJsonFormatVisitor(BigDecimalWithAvroDecimalAnnotationToFixedWrapper.class, gen);
9187
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
9288

93-
System.out.println(BigDecimalWithAvroDecimalAnnotationToFixedWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));
94-
9589
// THEN
9690
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();
9791

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/jsr310/AvroJavaTimeModule_schemaCreationTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public void testSchemaCreation(Class<?> testClass,
4747
mapper.acceptJsonFormatVisitor(testClass, gen);
4848
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
4949

50-
// System.out.println(testClass.getName() + " schema:\n" + actualSchema.toString(true));
51-
5250
// THEN
5351
assertThat(actualSchema.getType()).isEqualTo(expectedType);
5452
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo(expectedLogicalType);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public void builtAvroSchemaTest(Class<?> testClass,
6868
// WHEN
6969
Schema actualSchema = dateTimeVisitor.builtAvroSchema();
7070

71-
// System.out.println(testClass.getName() + " schema:\n" + actualSchema.toString(true));
72-
7371
// THEN
7472
assertThat(actualSchema.getType()).isEqualTo(expectedAvroType);
7573
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo(expectedLogicalType);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public void testJavaEnumToAvroEnum_test() throws JsonMappingException {
2727
MAPPER.acceptJsonFormatVisitor(NumbersEnum.class , gen);
2828
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
2929

30-
System.out.println("schema:\n" + actualSchema.toString(true));
31-
3230
// THEN
3331
assertThat(actualSchema.getType()).isEqualTo( Schema.Type.ENUM);
3432
assertThat(actualSchema.getEnumSymbols()).containsExactlyInAnyOrder("ONE", "TWO", "THREE");
@@ -44,8 +42,6 @@ public void testJavaEnumToAvroString_test() throws JsonMappingException {
4442
MAPPER.acceptJsonFormatVisitor(NumbersEnum.class , gen);
4543
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();
4644

47-
System.out.println("schema:\n" + actualSchema.toString(true));
48-
4945
// THEN
5046
assertThat(actualSchema.getType()).isEqualTo( Schema.Type.STRING);
5147

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public void subclasses_of_interface_test() throws Exception {
4545
// WHEN
4646
Schema actualSchema = MAPPER.schemaFor(AnimalInterface.class).getAvroSchema();
4747

48-
// System.out.println("Animal schema:\n" + actualSchema.toString(true));
49-
5048
// THEN
5149
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
5250
// Because AnimalInterface is interface and AbstractMammal is abstract, they are not expected to be among types in union
@@ -82,8 +80,6 @@ public void jsonSubTypes_on_concrete_class_test() throws Exception {
8280
// WHEN
8381
Schema actualSchema = MAPPER.schemaFor(Fruit.class).getAvroSchema();
8482

85-
// System.out.println("Fruit schema:\n" + actualSchema.toString(true));
86-
8783
// THEN
8884
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
8985
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(fruitItselfSchema, appleSchema, pearSchema);
@@ -142,8 +138,6 @@ public void jsonSubTypes_of_jsonSubTypes_test() throws Exception {
142138
// WHEN
143139
Schema actualSchema = MAPPER.schemaFor(Vehicle.class).getAvroSchema();
144140

145-
// System.out.println("Vehicle schema:\n" + actualSchema.toString(true));
146-
147141
// THEN
148142
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
149143
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(
@@ -187,8 +181,6 @@ public void class_is_referenced_twice_in_hierarchy_test() throws Exception {
187181
// WHEN
188182
Schema actualSchema = MAPPER.schemaFor(ElementInterface.class).getAvroSchema();
189183

190-
// System.out.println("ElementInterface schema:\n" + actualSchema.toString(true));
191-
192184
// THEN
193185
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
194186
// ElementInterface and AbstractGas are not concrete classes they are not expected to be among types in union
@@ -223,8 +215,6 @@ public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws Excep
223215
// WHEN
224216
Schema actualSchema = MAPPER.schemaFor(Image.class).getAvroSchema();
225217

226-
// System.out.println("Image schema:\n" + actualSchema.toString(true));
227-
228218
// THEN
229219
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
230220
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(imageItselfSchema, jpegSchema, pngSchema);

0 commit comments

Comments
 (0)