Skip to content

Commit

Permalink
AVRO-3649: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec committed Jan 5, 2023
1 parent bc3f50a commit 343e343
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions doc/content/en/docs/++version++/Specification/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Records use the type name "record" and support the following attributes:
* _name_: a JSON string providing the name of the field (required), and
* _doc_: a JSON string describing this field for users (optional).
* _type_: a [schema]({{< ref "#schema-declaration" >}} "Schema declaration"), as defined above
* _default_: A default value for this field, only used when reading instances that lack the field for schema evolution purposes. The presence of a default value does not make the field optional at encoding time. Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema in the union. Default values for bytes and fixed fields are JSON strings, where Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255. Avro encodes a field even if its value is equal to its default.
* _default_: A default value for this field, only used when reading instances that lack the field for schema evolution purposes. The presence of a default value does not make the field optional at encoding time. Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema that match in the union. Default values for bytes and fixed fields are JSON strings, where Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255. Avro encodes a field even if its value is equal to its default.

*field default values*

Expand Down Expand Up @@ -161,7 +161,7 @@ For example, a map from string to long is declared with:
### Unions
Unions, as mentioned above, are represented using JSON arrays. For example, `["null", "string"]` declares a schema which may be either a null or string.

(Note that when a [default value]({{< ref "#schema-record" >}} "Schema record") is specified for a record field whose type is a union, the type of the default value must match the first element of the union. Thus, for unions containing "null", the "null" is usually listed first, since the default value of such unions is typically null.)
(Note that when a [default value]({{< ref "#schema-record" >}} "Schema record") is specified for a record field whose type is a union, the type of the default value must match with one element of the union.

Unions may not contain more than one schema with the same type, except for the named types record, fixed and enum. For example, unions containing two array types or two map types are not permitted, but two types with different names are permitted. (Names permit efficient resolution when reading and writing unions.)

Expand Down
6 changes: 6 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 @@ -25,6 +25,7 @@
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -36,6 +37,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BinaryNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NullNode;
Expand Down Expand Up @@ -470,6 +472,10 @@ void validValue() {
arrayUnionValue.add(NullNode.getInstance());
assertTrue(arrayUnion.isValidValue(arrayUnionValue));

// Union String, bytes
final Schema unionStrBytes = Schema.createUnion(strSchema, Schema.create(Type.BYTES));
assertTrue(unionStrBytes.isValidValue("Hello"));
assertFalse(unionStrBytes.isValidValue(123));
}

}

0 comments on commit 343e343

Please sign in to comment.