Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ public Field(String name, Schema schema, String doc) {
*/
public Field(String name, Schema schema, String doc, Object defaultValue) {
this(name, schema, doc,
defaultValue == NULL_DEFAULT_VALUE ? NullNode.getInstance() : JacksonUtils.toJsonNode(defaultValue), true,
Order.ASCENDING);
defaultValue == NULL_DEFAULT_VALUE ? NullNode.getInstance() : JacksonUtils.toJsonNode(defaultValue, true),
true, Order.ASCENDING);
}

/**
Expand All @@ -623,8 +623,8 @@ public Field(String name, Schema schema, String doc, Object defaultValue) {
*/
public Field(String name, Schema schema, String doc, Object defaultValue, Order order) {
this(name, schema, doc,
defaultValue == NULL_DEFAULT_VALUE ? NullNode.getInstance() : JacksonUtils.toJsonNode(defaultValue), true,
Objects.requireNonNull(order));
defaultValue == NULL_DEFAULT_VALUE ? NullNode.getInstance() : JacksonUtils.toJsonNode(defaultValue, true),
true, Objects.requireNonNull(order));
}

public String name() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,47 @@ public class JacksonUtils {
private JacksonUtils() {
}

public static JsonNode toJsonNode(Object datum) {
public static JsonNode toJsonNode(Object object) {
return toJsonNode(object, false);
}

public static JsonNode toJsonNode(Object datum, boolean inSchemaContext) {
if (datum == null) {
return null;
}
try {
TokenBuffer generator = new TokenBuffer(MAPPER, false);
toJson(datum, generator);
toJson(datum, generator, inSchemaContext);
return MAPPER.readTree(generator.asParser());
} catch (IOException e) {
throw new AvroRuntimeException(e);
}
}

@SuppressWarnings(value = "unchecked")
static void toJson(Object datum, JsonGenerator generator) throws IOException {
static void toJson(Object datum, JsonGenerator generator, boolean inSchemaContext) throws IOException {
if (datum == JsonProperties.NULL_VALUE) { // null
generator.writeNull();
} else if (datum instanceof Map) { // record, map
generator.writeStartObject();
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) datum).entrySet()) {
generator.writeFieldName(entry.getKey().toString());
toJson(entry.getValue(), generator);
toJson(entry.getValue(), generator, inSchemaContext);
}
generator.writeEndObject();
} else if (datum instanceof Collection) { // array
generator.writeStartArray();
for (Object element : (Collection<?>) datum) {
toJson(element, generator);
toJson(element, generator, inSchemaContext);
}
generator.writeEndArray();
} else if (datum instanceof byte[]) { // bytes, fixed
generator.writeBinary((byte[]) datum);// writeString(new String((byte[]) datum, StandardCharsets.ISO_8859_1));
if (inSchemaContext) {
// when writing schemas, bytes must be encoded to string
generator.writeString(new String((byte[]) datum, StandardCharsets.ISO_8859_1));
} else {
generator.writeBinary((byte[]) datum);
}
} else if (datum instanceof CharSequence || datum instanceof Enum<?>) { // string, enum
generator.writeString(datum.toString());
} else if (datum instanceof Double) { // double
Expand Down
11 changes: 11 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -59,6 +60,7 @@
import org.junit.jupiter.api.Test;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -238,6 +240,15 @@ void serialization() throws IOException, ClassNotFoundException {
}
}

@Test
void byteArrayDefaultField() {
byte[] defaultBytes = new byte[] { 1, 2, 3 };
Schema.Field field = new Schema.Field("bytesField", Schema.create(Schema.Type.BYTES), "my bytes field", defaultBytes);
Schema rSchema = Schema.createRecord("myRecord", "myRecord docs", "me", false, List.of(field));
GenericData.Record rec = new GenericRecordBuilder(rSchema).build();
assertArrayEquals(defaultBytes, ((ByteBuffer) rec.get("bytesField")).array());
}

@Test
void reconstructSchemaStringWithoutInlinedChildReference() {
String child = "{\"type\":\"record\"," + "\"name\":\"Child\"," + "\"namespace\":\"org.apache.avro.nested\","
Expand Down
Loading