From 6b39d28f546ed71d2060555ef7cbb976c4c8c3da Mon Sep 17 00:00:00 2001 From: prygunovx Date: Sun, 7 Jun 2026 03:06:04 +0400 Subject: [PATCH] Add support for enum default values in IDL serialization --- .../java/org/apache/avro/idl/IdlUtils.java | 7 ++++++- .../org/apache/avro/idl/IdlUtilsTest.java | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java b/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java index 29c787c9e27..b9694a3f4ad 100644 --- a/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java +++ b/lang/java/idl/src/main/java/org/apache/avro/idl/IdlUtils.java @@ -285,7 +285,12 @@ private static void writeSchema(Schema schema, boolean insideProtocol, Writer wr } else { throw new AvroRuntimeException("Enum schema must have at least a symbol " + schema); } - writer.append(NEWLINE).append(indent).append("}").append(NEWLINE); + writer.append(NEWLINE).append(indent).append("}"); + var enumDefault = schema.getEnumDefault(); + if (enumDefault != null) { + writer.append(" = ").append(enumDefault).append(";"); + } + writer.append(NEWLINE); } else /* (type == Schema.Type.FIXED) */ { writer.append(indent).append("fixed ").append(schemaName).append('(') .append(Integer.toString(schema.getFixedSize())).append(");").append(NEWLINE); diff --git a/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java b/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java index ef2a81d3ffd..9f0416665b8 100644 --- a/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java +++ b/lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java @@ -33,11 +33,14 @@ import org.apache.avro.Schema; import org.junit.jupiter.api.Test; +import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class IdlUtilsTest { @Test @@ -103,6 +106,23 @@ public void cannotWriteProtocolWithUnnamedTypes() { () -> IdlUtils.writeIdlProtocol(new StringWriter(), Schema.create(Schema.Type.STRING))); } + @Test + public void enumDefaultIsWrittenToIdl() throws IOException { + Schema withDefault = Schema.createEnum("Status", null, "naming", asList("ACTIVE", "INACTIVE"), "ACTIVE"); + Schema withoutDefault = Schema.createEnum("Status", null, "naming", asList("ACTIVE", "INACTIVE")); + + StringWriter withDefaultWriter = new StringWriter(); + IdlUtils.writeIdlProtocol(withDefaultWriter, withDefault); + + StringWriter withoutDefaultWriter = new StringWriter(); + IdlUtils.writeIdlProtocol(withoutDefaultWriter, withoutDefault); + + assertTrue(withDefaultWriter.toString().contains("} = ACTIVE;"), + "Enum with default should serialize default value"); + assertFalse(withoutDefaultWriter.toString().contains("="), + "Enum without default should not contain '='"); + } + @Test public void cannotWriteEmptyEnums() { assertThrows(AvroRuntimeException.class,