Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +288 to +293
} else /* (type == Schema.Type.FIXED) */ {
writer.append(indent).append("fixed ").append(schemaName).append('(')
.append(Integer.toString(schema.getFixedSize())).append(");").append(NEWLINE);
Expand Down
20 changes: 20 additions & 0 deletions lang/java/idl/src/test/java/org/apache/avro/idl/IdlUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '='");
Comment on lines +120 to +123
}

@Test
public void cannotWriteEmptyEnums() {
assertThrows(AvroRuntimeException.class,
Expand Down
Loading