Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg-builder committed Dec 14, 2023
1 parent 1d43ede commit d2aec97
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,19 @@ protected Object translateField(List<Object> path, Object fieldValue, RecordData
if (_options.getOptionalDefaultMode() != OptionalDefaultMode.TRANSLATE_TO_NULL &&
field.getDefault() != null)
{
throw new IllegalArgumentException(
message(path,
"cannot translate absent optional field (to have null value) because this field is optional and has a default value"));
DataSchema.Type fieldDefaultValueType = field.getType().getType();
// punt on other default values for now (too complex to handle)
if (fieldDefaultValueType == DataSchema.Type.UNION || fieldDefaultValueType == DataSchema.Type.RECORD
|| fieldDefaultValueType == DataSchema.Type.TYPEREF)
{
throw new IllegalArgumentException(message(path,
"cannot translate absent optional field (to have null value) because this field is optional and has a default value"));
}
else
// use default value that was user set for maps/arrays
{
return translateField(path, field.getDefault(), field);
}
}
fieldValue = Data.NULL;
fieldDataSchema = DataSchemaConstants.NULL_DATA_SCHEMA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,25 @@ public Object[][] toAvroSchemaData()
null,
null
},
{
"{\n" + " \"type\": \"record\",\n" + " \"name\": \"Outer\",\n" + " \"namespace\": \"foo\",\n"
+ " \"fields\": [\n" + " {\n" + " \"name\": \"f1\",\n" + " \"type\": {\n"
+ " \"type\": \"record\",\n" + " \"name\": \"Inner\",\n"
+ " \"namespace\": \"bar\",\n" + " \"fields\": [\n" + " {\n"
+ " \"name\": \"innerArray\",\n" + " \"type\": {\n"
+ " \"type\": \"array\",\n" + " \"items\": \"string\"\n" + " },\n"
+ " \"default\": [],\n" + " \"optional\": true\n" + " },\n"
+ " {\n" + " \"name\": \"innerMap\",\n" + " \"type\": {\n"
+ " \"type\": \"map\",\n" + " \"values\": \"string\"\n" + " },\n"
+ " \"default\": {},\n" + " \"optional\": true\n" + " }\n"
+ " ]\n" + " },\n" + " \"default\": {},\n" + " \"optional\": true\n" + " }\n"
+ " ]\n" + "}",
translateDefault,
"{ \"type\" : \"record\", \"name\" : \"Outer\", \"namespace\" : \"foo\", \"fields\" : [ { \"name\" : \"f1\", \"type\" : [ { \"type\" : \"record\", \"name\" : \"Inner\", \"namespace\" : \"bar\", \"fields\" : [ { \"name\" : \"innerArray\", \"type\" : [ { \"type\" : \"array\", \"items\" : \"string\" }, \"null\" ], \"default\" : [ ] }, { \"name\" : \"innerMap\", \"type\" : [ { \"type\" : \"map\", \"values\" : \"string\" }, \"null\" ], \"default\" : { } } ] }, \"null\" ], \"default\" : { \"innerArray\" : [ ], \"innerMap\" : { } } } ] }",
null,
null,
null
},
{
// required, optional not specified
"{ \"type\" : \"record\", \"name\" : \"foo\", \"fields\" : [ { \"name\" : \"bar\", \"type\" : ##T_START \"int\" ##T_END } ] }",
Expand Down Expand Up @@ -2645,36 +2664,6 @@ public Object[][] toAvroSchemaErrorData()
IllegalArgumentException.class,
"cannot translate union value"
},
{
// inconsistent default,
// a referenced record has an optional field "frank" with default,
// but field of referenced record type has default value which does not provide value for "frank"
"{ " +
" \"type\" : \"record\", " +
" \"name\" : \"Bar\", " +
" \"fields\" : [ " +
" { " +
" \"name\" : \"barbara\", " +
" \"type\" : { " +
" \"type\" : \"record\", " +
" \"name\" : \"Foo\", " +
" \"fields\" : [ " +
" { " +
" \"name\" : \"frank\", " +
" \"type\" : \"string\", " +
" \"default\" : \"abc\", " +
" \"optional\" : true" +
" } " +
" ] " +
" }, " +
" \"default\" : { } " +
" } " +
" ]" +
"}",
translateDefault,
IllegalArgumentException.class,
"cannot translate absent optional field (to have null value) because this field is optional and has a default value"
},
{
// inconsistent default,
// a referenced record has an optional field "bar1" without default which translates with union with null as 1st member
Expand Down

0 comments on commit d2aec97

Please sign in to comment.