diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java index 51e8ce9be48dd..531110662b23e 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroCodeGenProviderBase.java @@ -4,8 +4,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; @@ -141,6 +143,14 @@ public static class AvroOptions { */ final boolean optionalGettersForNullableFieldsOnly; + /** + * A list of custom converter classes to register on the avro compiler. Conversions.UUIDConversion is + * registered by default. + *

+ * Passed as a comma-separated list. + */ + final List customConversions = new ArrayList<>(); + AvroOptions(Config config, String specificPropertyKey) { this.config = config; String imports = prop("avro.codegen." + specificPropertyKey + ".imports", ""); @@ -153,6 +163,12 @@ public static class AvroOptions { gettersReturnOptional = getBooleanProperty("avro.codegen.gettersReturnOptional", false); optionalGettersForNullableFieldsOnly = getBooleanProperty("avro.codegen.optionalGettersForNullableFieldsOnly", false); + String conversions = prop("avro.codegen.customConversions", ""); + if (!"".equals(conversions)) { + for (String conversion : conversions.split(",")) { + customConversions.add(conversion.trim()); + } + } } private String prop(String propName, String defaultValue) { diff --git a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java index 4f9da2da4a0e2..55ad09f4eee8a 100644 --- a/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java +++ b/extensions/avro/deployment/src/main/java/io/quarkus/avro/deployment/AvroSchemaCodeGenProvider.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.nio.file.Path; +import org.apache.avro.Conversions; import org.apache.avro.Schema; import org.apache.avro.compiler.specific.SpecificCompiler; @@ -65,6 +66,19 @@ void compileSingleFile(Path filePath, compiler.setCreateSetters(options.createSetters); compiler.setEnableDecimalLogicalType(options.enableDecimalLogicalType); compiler.setOutputCharacterEncoding("UTF-8"); + compiler.addCustomConversion(Conversions.UUIDConversion.class); + + for (String customConversion : options.customConversions) { + try { + Class conversionClass = Class.forName(customConversion); + if (!conversionClass.isInstance(Conversions.UUIDConversion.class)) { + compiler.addCustomConversion(conversionClass); + } + } catch (ClassNotFoundException e) { + throw new CodeGenException("Unable to find the following conversion class: " + customConversion, e); + } + } + try { compiler.compileToDestination(file, outputDirectory.toFile()); } catch (IOException e) { diff --git a/integration-tests/avro-reload/src/test/avro/User.avsc b/integration-tests/avro-reload/src/test/avro/User.avsc index 11c4aa7d51ab8..49ad0b3697590 100644 --- a/integration-tests/avro-reload/src/test/avro/User.avsc +++ b/integration-tests/avro-reload/src/test/avro/User.avsc @@ -6,7 +6,7 @@ "fields": [ { "name": "id", - "type": ["null", "string"], + "type": ["null", {"type": "string", "logicalType": "UUID"}], "default": null }, {