Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion ext/protojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function readField(field, value, options, depth) {
throw invalid(field.fullName, value, "expected array");
var arr = [], i = 0;
for (; i < value.length; ++i) {
if (value[i] === null && !isValueType(field.resolvedType))
if (value[i] === null && !isValueType(field.resolvedType) && !isNullValueType(field.resolvedType))
throw invalid(field.fullName, null, "null element");
var el = readSingular(field, value[i], options, depth);
if (el !== SKIP)
Expand All @@ -318,6 +318,10 @@ function isValueType(type) {
return type instanceof Type && type.fullName === ".google.protobuf.Value";
}

function isNullValueType(type) {
return type instanceof Enum && type.fullName === ".google.protobuf.NullValue";
}

function isImplicitDefault(field, value) {
if (field.hasPresence || field.repeated || field.map || field.resolvedType instanceof Type)
return false;
Expand Down
18 changes: 18 additions & 0 deletions tests/api_protojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ message NullMsg {\
google.protobuf.Duration duration = 4;\
google.protobuf.StringValue string_wrapper = 5;\
google.protobuf.Value value_msg = 6;\
repeated google.protobuf.NullValue null_values = 7;\
}", root);
return root.resolveAll();
}
Expand Down Expand Up @@ -536,6 +537,23 @@ tape.test("protojson - rejects null repeated elements and map values", function(
test.end();
});

tape.test("protojson - round trips repeated google.protobuf.NullValue elements", function(test) {
for (var i = 0; i < wktRoots.length; ++i) {
var NullMsg = wktRoots[i].root.lookupType("test.NullMsg"),
label = wktRoots[i].name,
message = protojson.fromJson(NullMsg, {
nullValues: [null, null]
});

test.deepEqual(message.nullValues, [0, 0], label + ": parses null elements as NULL_VALUE");
test.deepEqual(protojson.toJson(NullMsg, message), {
nullValues: [null, null]
}, label + ": emits NULL_VALUE elements as null");
}

test.end();
});

tape.test("protojson - rejects invalid UTF-16 in well-known values", function(test) {
for (var i = 0; i < wktRoots.length; ++i) {
var root = wktRoots[i].root,
Expand Down