Skip to content
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

Support enforcing fields to string according to schema #10

Merged
merged 4 commits into from
Dec 14, 2021
Merged
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
1 change: 1 addition & 0 deletions .java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ private Object read(Schema.Field field, Schema schema, Object value, Deque<Strin
result = onValidType(value, String.class, path, silently, string -> ensureEnum(schema, string, path));
break;
case STRING:
result = onValidType(value, String.class, path, silently, string -> string);
// When the schema is string, the value is forced to a string.
// This is necessary to handle a Json array field without items specification.
// In that case, the schema converter simply assume that it is an array of strings.
result = value == null ? INCOMPATIBLE : AdditionalPropertyField.getValue(value);
break;
case BYTES:
result = onValidType(value, String.class, path, silently, string -> bytesForString(string));
Expand Down Expand Up @@ -301,4 +304,3 @@ public Object onValidNumber(Object value, Deque<String> path, boolean silently,
return onValidType(value, Number.class, path, silently, function);
}
}

55 changes: 28 additions & 27 deletions converter/src/test/resources/json_avro_converter.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
[ {
"testCase": "logical_type_date_unconvertible_value",
"avroSchema": {
"type": "record",
"name": "logical_type_date_unconvertible_value",
"fields": [
{
"name": "last_convert_date",
"type": [
"null",
{
"type": "int",
"logicalType": "date"
},
"string"
],
"default": null
}
]
},
"jsonObject": {
"last_convert_date": "2021-10-10L"
},
"avroObject": {
"last_convert_date": "2021-10-10L"
}
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a redundant test case.

[
{
"testCase": "simple_schema",
"avroSchema": {
Expand Down Expand Up @@ -686,5 +660,32 @@
"avroObject": {
"last_convert_date": "2021-10-10L"
}
},
{
"testCase": "unknown_array_to_string_array",
"description": "Json objects with array fields of unknown types will be converted to string array according to the schema",
"avroSchema": {
"type": "record",
"name": "unknown_array_to_string_array",
"fields": [
{
"name": "array_field",
"type": [
"null",
{
"type": "array",
"items": ["null", "string"]
}
],
"default": null
}
]
},
"jsonObject": {
"array_field": [101, "102", true, false, { "id": 103, "tag": "104"}, null]
},
"avroObject": {
"array_field": ["101", "102", "true", "false", "{\"id\":103,\"tag\":\"104\"}", null]
}
}
]