Skip to content

Commit

Permalink
Merge pull request #22315 from glefloch/fix/21529
Browse files Browse the repository at this point in the history
Add the ability to set custom converters to avro compiler
  • Loading branch information
michalszynkiewicz authored Dec 20, 2021
2 parents cc118fd + e8b15c7 commit 7c71329
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,6 +143,14 @@ public static class AvroOptions {
*/
final boolean optionalGettersForNullableFieldsOnly;

/**
* A list of custom converter classes to register on the avro compiler. <code>Conversions.UUIDConversion</code> is
* registered by default.
* <p>
* Passed as a comma-separated list.
*/
final List<String> customConversions = new ArrayList<>();

AvroOptions(Config config, String specificPropertyKey) {
this.config = config;
String imports = prop("avro.codegen." + specificPropertyKey + ".imports", "");
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/avro-reload/src/test/avro/User.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"fields": [
{
"name": "id",
"type": ["null", "string"],
"type": ["null", {"type": "string", "logicalType": "UUID"}],
"default": null
},
{
Expand Down

0 comments on commit 7c71329

Please sign in to comment.