Skip to content

Commit 0384404

Browse files
committed
Visitor for {@link java.util.UUID} type. When it is created with logicalTypesEnabled enabled,
* Avro schema is created with logical type uuid.
1 parent 4a792b9 commit 0384404

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ public Schema builtAvroSchema() {
3939
// should we construct JavaType for `Character.class` in case of primitive or... ?
4040
return AvroSchemaHelper.numericAvroSchema(NumberType.INT, _type);
4141
}
42-
// [dataformats-binary#179]: need special help with UUIDs, to coerce into Binary
43-
// (could actually be
44-
if (_type.hasRawClass(java.util.UUID.class)) {
45-
return AvroSchemaHelper.createUUIDSchema();
46-
}
42+
4743
BeanDescription bean = _provider.getConfig().introspectClassAnnotations(_type);
4844
Schema schema = Schema.create(Schema.Type.STRING);
4945
// Stringable classes need to include the type
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fasterxml.jackson.dataformat.avro.schema;
2+
3+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;
4+
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonValueFormat;
5+
import org.apache.avro.LogicalTypes;
6+
import org.apache.avro.Schema;
7+
8+
import java.util.Set;
9+
10+
/**
11+
* Visitor for {@link java.util.UUID} type. When it is created with logicalTypesEnabled enabled,
12+
* Avro schema is created with logical type uuid.
13+
*
14+
* @since 2.19
15+
*/
16+
public class UUIDVisitor extends JsonStringFormatVisitor.Base
17+
implements SchemaBuilder {
18+
protected boolean _logicalTypesEnabled = false;
19+
20+
21+
public UUIDVisitor(boolean logicalTypesEnabled) {
22+
_logicalTypesEnabled = logicalTypesEnabled;
23+
}
24+
25+
@Override
26+
public void format(JsonValueFormat format) {
27+
// Ideally, we'd recognize UUIDs, Dates etc if need be, here...
28+
}
29+
30+
@Override
31+
public void enumTypes(Set<String> enums) {
32+
// Do nothing
33+
}
34+
35+
@Override
36+
public Schema builtAvroSchema() {
37+
// [dataformats-binary#179]: need special help with UUIDs, to coerce into Binary
38+
// (could actually be
39+
Schema schema = AvroSchemaHelper.createUUIDSchema();
40+
return this._logicalTypesEnabled ? LogicalTypes.uuid().addToSchema(schema) : schema;
41+
}
42+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ public JsonStringFormatVisitor expectStringFormat(JavaType type)
214214
return v;
215215
}
216216

217+
if (type.hasRawClass(java.util.UUID.class)) {
218+
UUIDVisitor v = new UUIDVisitor(this._logicalTypesEnabled);
219+
_builder = v;
220+
return v;
221+
}
222+
217223
StringVisitor v = new StringVisitor(_provider, type);
218224
_builder = v;
219225
return v;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fasterxml.jackson.dataformat.avro.schema;
2+
3+
import org.apache.avro.LogicalType;
4+
import org.apache.avro.Schema;
5+
import org.junit.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class UUIDVisitor_builtAvroSchemaTest {
10+
11+
@Test
12+
public void testLogicalTypesDisabled() {
13+
// GIVEN
14+
boolean logicalTypesEnabled = false;
15+
UUIDVisitor uuidVisitor = new UUIDVisitor(logicalTypesEnabled);
16+
17+
// WHEN
18+
Schema actualSchema = uuidVisitor.builtAvroSchema();
19+
20+
// THEN
21+
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.FIXED);
22+
assertThat(actualSchema.getFixedSize()).isEqualTo(16);
23+
assertThat(actualSchema.getName()).isEqualTo("UUID");
24+
assertThat(actualSchema.getNamespace()).isEqualTo("java.util");
25+
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isNull();
26+
}
27+
28+
@Test
29+
public void testLogicalTypesEnabled() {
30+
// GIVEN
31+
boolean logicalTypesEnabled = true;
32+
UUIDVisitor uuidVisitor = new UUIDVisitor(logicalTypesEnabled);
33+
34+
// WHEN
35+
Schema actualSchema = uuidVisitor.builtAvroSchema();
36+
37+
// THEN
38+
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.FIXED);
39+
assertThat(actualSchema.getFixedSize()).isEqualTo(16);
40+
assertThat(actualSchema.getName()).isEqualTo("UUID");
41+
assertThat(actualSchema.getNamespace()).isEqualTo("java.util");
42+
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo("uuid");
43+
}
44+
45+
}

0 commit comments

Comments
 (0)