Skip to content
Merged
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
4 changes: 4 additions & 0 deletions lang/java/protobuf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<arg value="--java_out=src/test/java/"/>
<arg value="src/test/protobuf/test_multiple_files.proto"/>
</exec>
<exec executable="protoc">
<arg value="--java_out=src/test/java/"/>
<arg value="src/test/protobuf/test_proto3.proto"/>
</exec>
</target>
</configuration>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ private Schema getNonRepeatedSchema(FieldDescriptor f) {
case SFIXED64:
return Schema.create(Schema.Type.LONG);
case ENUM:
return getSchema(f.getEnumType());
return f.hasDefaultValue() ? getSchema(f.getEnumType(), ((EnumValueDescriptor) f.getDefaultValue()).getName())
: getSchema(f.getEnumType());
case MESSAGE:
result = getSchema(f.getMessageType());
if (f.isOptional())
Expand All @@ -315,11 +316,22 @@ private Schema getNonRepeatedSchema(FieldDescriptor f) {
}

public Schema getSchema(EnumDescriptor d) {
List<String> symbols = getEnumSymbols(d);
String enumDefault = symbols.isEmpty() ? null : symbols.get(0);
return Schema.createEnum(d.getName(), null, getNamespace(d.getFile(), d.getContainingType()), symbols, enumDefault);
}

public Schema getSchema(EnumDescriptor d, String enumDefault) {
List<String> symbols = getEnumSymbols(d);
return Schema.createEnum(d.getName(), null, getNamespace(d.getFile(), d.getContainingType()), symbols, enumDefault);
}

private List<String> getEnumSymbols(EnumDescriptor d) {
List<String> symbols = new ArrayList<>(d.getValues().size());
for (EnumValueDescriptor e : d.getValues()) {
symbols.add(e.getName());
}
return Schema.createEnum(d.getName(), null, getNamespace(d.getFile(), d.getContainingType()), symbols);
return symbols;
}

private static final JsonFactory FACTORY = new JsonFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.protobuf.noopt.TestProto3;
import org.apache.avro.specific.SpecificData;
import org.apache.commons.compress.utils.Lists;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -146,4 +147,17 @@ void getNonRepeatedSchemaWithLogicalType() throws Exception {
Schema s2 = instance2.getSchema(com.google.protobuf.Timestamp.class);
assertEquals(conversion.getRecommendedSchema(), s2);
}

@Test
void enumDefault() {
Schema fooSchema = ProtobufData.get().getSchema(Foo.class);
assertEquals("Z", fooSchema.getField("enum").schema().getEnumDefault());

Schema nSchema = ProtobufData.get().getSchema(N.class);
assertEquals("A", nSchema.getEnumDefault());

Schema proto3Schema = ProtobufData.get().getSchema(TestProto3.User.class);
assertEquals("STATUS_UNKNOWN", proto3Schema.getField("status").defaultVal());
assertEquals("STATUS_UNKNOWN", proto3Schema.getField("status").schema().getEnumDefault());
}
}
Loading