-
Notifications
You must be signed in to change notification settings - Fork 85
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
feat: Add ignoreUnknownField support in JsonWriter #1455
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 |
---|---|---|
|
@@ -64,7 +64,8 @@ public static DynamicMessage convertJsonToProtoMessage(Descriptor protoSchema, J | |
Preconditions.checkNotNull(protoSchema, "Protobuf descriptor is null."); | ||
Preconditions.checkState(json.length() != 0, "JSONObject is empty."); | ||
|
||
return convertJsonToProtoMessageImpl(protoSchema, null, json, "root", /*topLevel=*/ true); | ||
return convertJsonToProtoMessageImpl( | ||
protoSchema, null, json, "root", /*topLevel=*/ true, false); | ||
} | ||
|
||
/** | ||
|
@@ -85,7 +86,39 @@ public static DynamicMessage convertJsonToProtoMessage( | |
Preconditions.checkState(json.length() != 0, "JSONObject is empty."); | ||
|
||
return convertJsonToProtoMessageImpl( | ||
protoSchema, tableSchema.getFieldsList(), json, "root", /*topLevel=*/ true); | ||
protoSchema, | ||
tableSchema.getFieldsList(), | ||
json, | ||
"root", | ||
/*topLevel=*/ true, | ||
/*ignoreUnknownFields*/ false); | ||
} | ||
|
||
/** | ||
* Converts Json data to protocol buffer messages given the protocol buffer descriptor. | ||
* | ||
* @param protoSchema | ||
* @param tableSchema bigquery table schema is needed for type conversion of DATETIME, TIME, | ||
* NUMERIC, BIGNUMERIC | ||
* @param json | ||
* @param ignoreUnknownFields allows unknown fields in JSON input to be ignored. | ||
* @throws IllegalArgumentException when JSON data is not compatible with proto descriptor. | ||
*/ | ||
public static DynamicMessage convertJsonToProtoMessage( | ||
Descriptor protoSchema, TableSchema tableSchema, JSONObject json, boolean ignoreUnknownFields) | ||
throws IllegalArgumentException { | ||
Preconditions.checkNotNull(json, "JSONObject is null."); | ||
Preconditions.checkNotNull(protoSchema, "Protobuf descriptor is null."); | ||
Preconditions.checkNotNull(tableSchema, "TableSchema is null."); | ||
Preconditions.checkState(json.length() != 0, "JSONObject is empty."); | ||
|
||
return convertJsonToProtoMessageImpl( | ||
protoSchema, | ||
tableSchema.getFieldsList(), | ||
json, | ||
"root", | ||
/*topLevel=*/ true, | ||
ignoreUnknownFields); | ||
} | ||
|
||
/** | ||
|
@@ -102,7 +135,8 @@ private static DynamicMessage convertJsonToProtoMessageImpl( | |
List<TableFieldSchema> tableSchema, | ||
JSONObject json, | ||
String jsonScope, | ||
boolean topLevel) | ||
boolean topLevel, | ||
boolean ignoreUnknownFields) | ||
throws IllegalArgumentException { | ||
|
||
DynamicMessage.Builder protoMsg = DynamicMessage.newBuilder(protoSchema); | ||
|
@@ -117,9 +151,11 @@ private static DynamicMessage convertJsonToProtoMessageImpl( | |
String jsonLowercaseName = jsonName.toLowerCase(); | ||
String currentScope = jsonScope + "." + jsonName; | ||
FieldDescriptor field = protoSchema.findFieldByName(jsonLowercaseName); | ||
if (field == null) { | ||
if (field == null && !ignoreUnknownFields) { | ||
throw new IllegalArgumentException( | ||
String.format("JSONObject has fields unknown to BigQuery: %s.", currentScope)); | ||
} else if (field == null) { | ||
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. This check seems redundant. 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. If it is null, it means we cannot proceed on further processing. Otherwise, null ptr will be thrown if we continue. 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. Okay makes sense -- this allows us to skip the unknown field in the middle and proceed onto other fields. |
||
continue; | ||
} | ||
TableFieldSchema fieldSchema = null; | ||
if (tableSchema != null) { | ||
|
@@ -137,9 +173,10 @@ private static DynamicMessage convertJsonToProtoMessageImpl( | |
} | ||
} | ||
if (!field.isRepeated()) { | ||
fillField(protoMsg, field, fieldSchema, json, jsonName, currentScope); | ||
fillField(protoMsg, field, fieldSchema, json, jsonName, currentScope, ignoreUnknownFields); | ||
} else { | ||
fillRepeatedField(protoMsg, field, fieldSchema, json, jsonName, currentScope); | ||
fillRepeatedField( | ||
protoMsg, field, fieldSchema, json, jsonName, currentScope, ignoreUnknownFields); | ||
} | ||
} | ||
|
||
|
@@ -174,7 +211,8 @@ private static void fillField( | |
TableFieldSchema fieldSchema, | ||
JSONObject json, | ||
String exactJsonKeyName, | ||
String currentScope) | ||
String currentScope, | ||
boolean ignoreUnknownFields) | ||
throws IllegalArgumentException { | ||
|
||
java.lang.Object val = json.get(exactJsonKeyName); | ||
|
@@ -303,7 +341,8 @@ private static void fillField( | |
fieldSchema == null ? null : fieldSchema.getFieldsList(), | ||
json.getJSONObject(exactJsonKeyName), | ||
currentScope, | ||
/*topLevel =*/ false)); | ||
/*topLevel =*/ false, | ||
ignoreUnknownFields)); | ||
return; | ||
} | ||
break; | ||
|
@@ -331,7 +370,8 @@ private static void fillRepeatedField( | |
TableFieldSchema fieldSchema, | ||
JSONObject json, | ||
String exactJsonKeyName, | ||
String currentScope) | ||
String currentScope, | ||
boolean ignoreUnknownFields) | ||
throws IllegalArgumentException { | ||
|
||
JSONArray jsonArray; | ||
|
@@ -478,7 +518,8 @@ private static void fillRepeatedField( | |
fieldSchema == null ? null : fieldSchema.getFieldsList(), | ||
jsonArray.getJSONObject(i), | ||
currentScope, | ||
/*topLevel =*/ false)); | ||
/*topLevel =*/ false, | ||
ignoreUnknownFields)); | ||
} else { | ||
fail = true; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline,
topLevel
is not used inconvertJsonToProtoMessageImpl
. Hence, we should remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack