Skip to content

Commit

Permalink
Add three conformance test edge cases for ProtoJSON repr of Any
Browse files Browse the repository at this point in the history
1) An Any with no type_url or value should be represented as `{}`
2) A `null` should be handled as the Any being absent.
3) JSON of `{"@type_url": "", "value":""}` should be a parse failure.

PiperOrigin-RevId: 712938072
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 7, 2025
1 parent 79e331f commit b985164
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3524,6 +3524,40 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForAny() {
}
}
)");
// When the Any is in WKT form (with "@type"), the type_url must be present
// and URL shaped, otherwise it should be a parse error (because it can't be
// parsed into the Any schema).
ExpectParseFailureForJson("AnyWktRepresentationWithEmptyTypeAndValue",
REQUIRED,
R"({
"optionalAny": {
"@type": "",
"value": ""
}
})");
ExpectParseFailureForJson("AnyWktRepresentationWithBadType", REQUIRED,
R"({
"optionalAny": {
"@type": "not_a_url",
"value": ""
}
})");
// When the Any can be parsed as non-WKT form, the type_url could be missing
// or invalid, since that can still be parsed into the Any schema.
RunValidJsonTest("AnyWithNoType", REQUIRED,
R"({
"optionalAny": {}
})",
R"(
optional_any: {}
)");
// `null` where an Any exists should just result in the field being unset.
RunValidJsonTest("AnyNull", REQUIRED,
R"({
"optionalAny": null
})",
R"(
)");
}

template <typename MessageType>
Expand Down
4 changes: 4 additions & 0 deletions conformance/failure_list_csharp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ Recommended.Editions_Proto3.ValueRejectInfNumberValue.JsonOutput
Recommended.Editions_Proto3.ValueRejectNanNumberValue.JsonOutput
Required.Editions_Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.Editions_Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output.
Required.*.JsonInput.AnyWktRepresentationWithEmptyTypeAndValue # Should have failed to parse, but didn't.
Required.*.JsonInput.AnyWktRepresentationWithBadType # Should have failed to parse, but didn't.

1 change: 1 addition & 0 deletions conformance/failure_list_jruby.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,4 @@ Required.Editions_Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.Editions_Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.Proto2.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.Proto3.ProtobufInput.UnknownOrdering.ProtobufOutput
Required.*.JsonInput.AnyWithNoType.JsonOutput # Failed to parse input or produce output.
1 change: 1 addition & 0 deletions conformance/failure_list_jruby_ffi.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output.
1 change: 1 addition & 0 deletions conformance/failure_list_php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ Required.*.ProtobufInput.ValidDataRepeated.FLOAT.PackedInput.JsonOutput
Required.*.ProtobufInput.ValidDataRepeated.FLOAT.UnpackedInput.JsonOutput
Required.*.ProtobufInput.ValidDataScalar.FLOAT[2].JsonOutput
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
Required.*.JsonInput.AnyWithNoType.*
1 change: 1 addition & 0 deletions conformance/failure_list_php_c.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Required.Proto2.JsonInput.EnumField.ProtobufOutput
Required.Proto2.JsonInput.EnumFieldNumericValueZero.JsonOutput
Required.Proto2.JsonInput.EnumFieldNumericValueZero.ProtobufOutput
Required.Proto2.JsonInput.StoresDefaultPrimitive.Validator
Required.*.JsonInput.AnyWithNoType.*
1 change: 1 addition & 0 deletions conformance/failure_list_ruby.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
Required.*.JsonInput.AnyWithNoType.*
1 change: 1 addition & 0 deletions upb/conformance/conformance_upb_failures.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output.

0 comments on commit b985164

Please sign in to comment.