From b985164e7cd075711817a02392c61fd85c74d992 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 7 Jan 2025 09:22:16 -0800 Subject: [PATCH] Add three conformance test edge cases for ProtoJSON repr of Any 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 --- conformance/binary_json_conformance_suite.cc | 34 ++++++++++++++++++++ conformance/failure_list_csharp.txt | 4 +++ conformance/failure_list_jruby.txt | 1 + conformance/failure_list_jruby_ffi.txt | 1 + conformance/failure_list_php.txt | 1 + conformance/failure_list_php_c.txt | 1 + conformance/failure_list_ruby.txt | 1 + upb/conformance/conformance_upb_failures.txt | 1 + 8 files changed, 44 insertions(+) diff --git a/conformance/binary_json_conformance_suite.cc b/conformance/binary_json_conformance_suite.cc index cd0ef8c744dea..848e8502d2011 100644 --- a/conformance/binary_json_conformance_suite.cc +++ b/conformance/binary_json_conformance_suite.cc @@ -3524,6 +3524,40 @@ void BinaryAndJsonConformanceSuiteImpl::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 diff --git a/conformance/failure_list_csharp.txt b/conformance/failure_list_csharp.txt index e16578c329d96..27560100fee77 100644 --- a/conformance/failure_list_csharp.txt +++ b/conformance/failure_list_csharp.txt @@ -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. + diff --git a/conformance/failure_list_jruby.txt b/conformance/failure_list_jruby.txt index b02f9a5ecf429..778b9b9e62d5e 100644 --- a/conformance/failure_list_jruby.txt +++ b/conformance/failure_list_jruby.txt @@ -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. \ No newline at end of file diff --git a/conformance/failure_list_jruby_ffi.txt b/conformance/failure_list_jruby_ffi.txt index 74f87f99ff8bb..d22be3bf1dd92 100644 --- a/conformance/failure_list_jruby_ffi.txt +++ b/conformance/failure_list_jruby_ffi.txt @@ -1 +1,2 @@ Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output. +Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output. diff --git a/conformance/failure_list_php.txt b/conformance/failure_list_php.txt index 5f450ac2d2e47..ce4e24ac565f1 100644 --- a/conformance/failure_list_php.txt +++ b/conformance/failure_list_php.txt @@ -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.* diff --git a/conformance/failure_list_php_c.txt b/conformance/failure_list_php_c.txt index 09df19fb0fc10..f00f746e87769 100644 --- a/conformance/failure_list_php_c.txt +++ b/conformance/failure_list_php_c.txt @@ -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.* diff --git a/conformance/failure_list_ruby.txt b/conformance/failure_list_ruby.txt index 74f87f99ff8bb..882ab1dc74707 100644 --- a/conformance/failure_list_ruby.txt +++ b/conformance/failure_list_ruby.txt @@ -1 +1,2 @@ Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output. +Required.*.JsonInput.AnyWithNoType.* \ No newline at end of file diff --git a/upb/conformance/conformance_upb_failures.txt b/upb/conformance/conformance_upb_failures.txt index 74f87f99ff8bb..67e6a5f1526ab 100644 --- a/upb/conformance/conformance_upb_failures.txt +++ b/upb/conformance/conformance_upb_failures.txt @@ -1 +1,2 @@ Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output. +Required.*.JsonInput.AnyWithNoType.* # Failed to parse input or produce output. \ No newline at end of file