-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3649: Fix for union type to match default values for any innertype #1922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1332,6 +1332,16 @@ public UnionSchema(LockableArrayList<Schema> types) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a JSON value matches the schema. | ||
| * | ||
| * @param jsonValue a value to check against the schema | ||
| * @return true if the value is valid according to this schema | ||
| */ | ||
| public boolean isValidDefault(JsonNode jsonValue) { | ||
| return this.types.stream().anyMatch((Schema s) -> s.isValidDefault(jsonValue)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Schema> getTypes() { | ||
| return types; | ||
|
|
@@ -1768,13 +1778,23 @@ public static void setNameValidator(final Schema.NameValidator validator) { | |
| private static final ThreadLocal<Boolean> VALIDATE_DEFAULTS = ThreadLocalWithInitial.of(() -> true); | ||
|
|
||
| private static JsonNode validateDefault(String fieldName, Schema schema, JsonNode defaultValue) { | ||
| if (VALIDATE_DEFAULTS.get() && (defaultValue != null) && !isValidDefault(schema, defaultValue)) { // invalid default | ||
| if (VALIDATE_DEFAULTS.get() && (defaultValue != null) && !schema.isValidDefault(defaultValue)) { // invalid default | ||
| String message = "Invalid default for field " + fieldName + ": " + defaultValue + " not a " + schema; | ||
| throw new AvroTypeException(message); // throw exception | ||
| } | ||
| return defaultValue; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a JSON value matches the schema. | ||
| * | ||
| * @param jsonValue a value to check against the schema | ||
| * @return true if the value is valid according to this schema | ||
| */ | ||
| public boolean isValidDefault(JsonNode jsonValue) { | ||
| return isValidDefault(this, jsonValue); | ||
| } | ||
|
|
||
| private static boolean isValidDefault(Schema schema, JsonNode defaultValue) { | ||
| if (defaultValue == null) | ||
| return false; | ||
|
|
@@ -1809,8 +1829,8 @@ private static boolean isValidDefault(Schema schema, JsonNode defaultValue) { | |
| if (!isValidDefault(schema.getValueType(), value)) | ||
| return false; | ||
| return true; | ||
| case UNION: // union default: first branch | ||
| return isValidDefault(schema.getTypes().get(0), defaultValue); | ||
|
Comment on lines
-1812
to
-1813
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks to me that these changes make any value for the null schema or a union an invalid value. What am I overlooking?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static method isValidDefault (line 1650 on new version) is private, called by new virtual function "public boolean isValidValue(final Object value) {", line 1645. |
||
| case UNION: // union default: any branch | ||
| return schema.getTypes().stream().anyMatch((Schema s) -> isValidValue(s, defaultValue)); | ||
| case RECORD: | ||
| if (!defaultValue.isObject()) | ||
| return false; | ||
|
|
||
Check notice
Code scanning / CodeQL
Missing Override annotation