Skip to content

Commit

Permalink
Automated rollback of changelist with RevId 358064676
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 359571540
  • Loading branch information
aaronnash authored and Cameron Tew committed May 5, 2021
1 parent 936aff7 commit 443d17c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 49 deletions.
5 changes: 3 additions & 2 deletions cc/google/fhir/json_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ absl::StatusOr<std::string> ToJsonStringValue(absl::string_view raw_value) {
if (replacement != CHARACTERS_TO_ESCAPED_CHARACTER_MAP->end()) {
absl::StrAppend(&result, replacement->second);
} else if (char_byte < 32) {
absl::StrAppend(&result, "\\u",
absl::Hex(static_cast<int>(char_byte), absl::kZeroPad4));
return absl::InvalidArgumentError(absl::StrCat(
"Invalid control character found in string. Decimal value:",
static_cast<int>(char_byte)));
} else {
result.push_back(char_byte);
}
Expand Down
11 changes: 4 additions & 7 deletions cc/google/fhir/json_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ namespace google::fhir {
// this would return the literal:
// R"("this is \n\"my favorite\" string")"
//
// Note that this will print all control characters with their escape
// counterparts. While the FHIR spec states that "Strings SHOULD not contain
// Unicode character points below 32, except for u0009 (horizontal tab), u0010
// (carriage return) and u0013 (line feed)", we take a permissive stance in what
// it allows, and will not reject or otherwise surpress other control
// characters.
// See: https://www.hl7.org/fhir/datatypes.html#string
// Per FHIR spec, the only valid control characters in the range [0,32)
// are [\r\n\t]. This returns a status error if it encounters any other
// characters in that range.
// See https://www.hl7.org/fhir/datatypes.html#string
//
// Note that this does NOT escape the "solidus" `/` as `\/`.
// Per JSON spec, this optional but not required, so to maintain a minimal
Expand Down
12 changes: 4 additions & 8 deletions cc/google/fhir/json_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ TEST(JsonUtilTest, ToJsonStringValueWithMultiByteCharacters) {
TestToJsonStringValue("André", "\"André\"");
}

TEST(JsonUtilTest, ToJsonStringValueControlCharactersPrinted) {
std::string with_null_char{"\r\n\t\0\u0008", 5};
TestToJsonStringValue(with_null_char, R"("\r\n\t\u0000\u0008")");
}

TEST(JsonUtilTest, ToJsonStringValueOnlyNullChar) {
std::string only_null_char{"\0", 1};
TestToJsonStringValue(only_null_char, R"("\u0000")");
TEST(JsonUtilTest, ToJsonStringValueInvalidControlCharactersReturnsError) {
std::string with_null_char{"foo\0bar", 7};
absl::StatusOr<std::string> result = ToJsonStringValue(with_null_char);
EXPECT_FALSE(result.ok());
}

} // namespace
Expand Down
9 changes: 5 additions & 4 deletions cc/google/fhir/r4/json_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1688,10 +1688,11 @@ TEST(JsonFormatR4Test, PrintAndParseAllResources) {
}
}

TEST(JsonFormatR4Test, ControlCharacters) {
TestPairWithFilePaths<Observation>(
"testdata/jsonformat/observation_control_characters.prototxt",
"testdata/jsonformat/observation_control_characters.json");
TEST(JsonFormatR4Test, InvalidControlCharactersReturnsError) {
const Observation proto = ReadProto<Observation>(
"testdata/jsonformat/observation_invalid_unicode.prototxt");

ASSERT_FALSE(PrettyPrintFhirToJsonString(proto).ok());
}

TEST(JsonFormatR4Test, PadsThreeDigitYearToFourCharacters) {
Expand Down
9 changes: 5 additions & 4 deletions cc/google/fhir/stu3/json_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2199,10 +2199,11 @@ TEST(JsonFormatTest, VisionPrescriptionPrint) {
TestPrint<VisionPrescription>("VisionPrescription-33124");
}

TEST(JsonFormatR4Test, ControlCharacters) {
TestPairWithFilePaths<Observation>(
"testdata/jsonformat/observation_control_characters.prototxt",
"testdata/jsonformat/observation_control_characters.json");
TEST(JsonFormatTest, InvalidControlCharactersReturnsError) {
const Observation proto = ReadProto<Observation>(
"testdata/jsonformat/observation_invalid_unicode.prototxt");

ASSERT_FALSE(PrettyPrintFhirToJsonString(proto).ok());
}

TEST(JsonFormatR4Test, PadsThreeDigitYearToFourCharacters) {
Expand Down
13 changes: 9 additions & 4 deletions testdata/jsonformat/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ licenses(["notice"])

filegroup(
name = "jsonformat",
srcs = glob([
"*.json",
"*.prototxt",
]),
srcs = glob(
[
"*.json",
"*.prototxt",
],
exclude = [
"observation_invalid_unicode.json",
],
),
)
11 changes: 0 additions & 11 deletions testdata/jsonformat/observation_control_characters.prototxt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@
}
]
},
"referenceRange" : [
{ "text": "\u0000" },
{ "text": "\r\n" },
{ "text": "\n" }
]
"valueString" : "foobarbaz\r\n\tquux"
}
11 changes: 11 additions & 0 deletions testdata/jsonformat/observation_invalid_unicode.prototxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
status { value: FINAL }
code {
coding {
system { value: "foo" }
code { value: "bar" }
}
}
# Note that the only valid control characters are [\r\n\t], these are preserved
# Other control characters (like \0 and \u0001) are dropped
value { string_value { value: "foo\0bar\u0001baz\r\n\tquux" } }
id { value: "123" }
4 changes: 0 additions & 4 deletions testdata/primitives/String.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ valid_pairs {
json_string: "\"foo\\tbar\\rbaz\\nquux\""
proto { string_proto { value: "foo\tbar\rbaz\nquux" } }
}
valid_pairs {
json_string: "\"\\u0000\""
proto { string_proto { value: "\0" } }
}
valid_pairs {
json_string: "\"André 3000\""
proto { string_proto { value: "Andr\303\251 3000" } }
Expand Down

0 comments on commit 443d17c

Please sign in to comment.