diff --git a/cc/google/fhir/json_printer.cc b/cc/google/fhir/json_printer.cc index 798708fa7..c07796ec1 100644 --- a/cc/google/fhir/json_printer.cc +++ b/cc/google/fhir/json_printer.cc @@ -163,13 +163,16 @@ class Printer { } OpenJsonObject(); + std::vector set_fields; + reflection->ListFields(proto, &set_fields); if (IsResource(descriptor) && json_format_ == kFormatPure) { absl::StrAppend(&output_, "\"resourceType\": \"", descriptor->name(), - "\","); - AddNewline(); + "\""); + if (!set_fields.empty()) { + absl::StrAppend(&output_, ","); + AddNewline(); + } } - std::vector set_fields; - reflection->ListFields(proto, &set_fields); for (size_t i = 0; i < set_fields.size(); i++) { const FieldDescriptor* field = set_fields[i]; if (json_format_ == kFormatAnalytic && field->name() == "id" && diff --git a/cc/google/fhir/r4/json_format_test.cc b/cc/google/fhir/r4/json_format_test.cc index 28cde87c7..0a77e146c 100644 --- a/cc/google/fhir/r4/json_format_test.cc +++ b/cc/google/fhir/r4/json_format_test.cc @@ -189,8 +189,9 @@ namespace r4 { namespace { -using namespace google::fhir::r4::core; // NOLINT -using google::protobuf::FieldDescriptor; +using namespace ::google::fhir::r4::core; // NOLINT +using ::google::protobuf::FieldDescriptor; +using ::testing::Eq; static const char* const kTimeZoneString = "Australia/Sydney"; @@ -479,6 +480,19 @@ TEST(JsonFormatR4Test, PrintForAnalyticsWithContained) { } } +TEST(JsonFormatR4Test, WithEmptyContainedResourcePrintsValidJson) { + const Parameters proto = ReadProto( + "testdata/r4/examples/Parameters-empty-resource.prototxt"); + absl::StatusOr from_proto_status = + PrettyPrintFhirToJsonString(proto); + ASSERT_TRUE(from_proto_status.ok()); + std::string expected = + ReadFile("testdata/r4/examples/Parameters-empty-resource.json"); + ASSERT_THAT(expected.back(), Eq('\n')); + expected = expected.substr(0, expected.length() - 1); + ASSERT_THAT(*from_proto_status, Eq(expected)); +} + TEST(JsonFormatR4Test, TestAccount) { std::vector files{"Account-ewg", "Account-example"}; TestPair(files); diff --git a/cc/google/fhir/stu3/json_format_test.cc b/cc/google/fhir/stu3/json_format_test.cc index 430711198..5830476f6 100644 --- a/cc/google/fhir/stu3/json_format_test.cc +++ b/cc/google/fhir/stu3/json_format_test.cc @@ -35,9 +35,10 @@ namespace stu3 { namespace { -using namespace google::fhir::stu3::proto; // NOLINT +using namespace ::google::fhir::stu3::proto; // NOLINT -using google::fhir::stu3::proto::Condition; +using ::google::fhir::stu3::proto::Condition; +using ::testing::Eq; static const char* const kTimeZoneString = "Australia/Sydney"; @@ -111,7 +112,7 @@ void TestPrintForAnalytics(const std::string& proto_filepath, const std::string& json_filepath, bool pretty) { R proto = ReadProto(proto_filepath); if (IsProfile(R::descriptor())) { - proto = NormalizeR4(proto).value(); + proto = NormalizeStu3(proto).value(); } auto result = pretty ? PrettyPrintFhirToJsonStringForAnalytics(proto) : PrintFhirToJsonStringForAnalytics(proto); @@ -202,6 +203,19 @@ TEST(JsonFormatStu3Test, PrintAnalyticsElementIdsDropped) { "testdata/jsonformat/location_element_with_ids_analytic.json"); } +TEST(JsonFormatStu3Test, WithEmptyContainedResourcePrintsValidJson) { + const Parameters proto = ReadProto( + "testdata/stu3/examples/Parameters-empty-resource.prototxt"); + absl::StatusOr from_proto_status = + PrettyPrintFhirToJsonString(proto); + ASSERT_TRUE(from_proto_status.ok()); + std::string expected = + ReadFile("testdata/stu3/examples/Parameters-empty-resource.json"); + ASSERT_THAT(expected.back(), Eq('\n')); + expected = expected.substr(0, expected.length() - 1); + ASSERT_THAT(*from_proto_status, Eq(expected)); +} + /* Resource tests start here. */ /** Test parsing of the Account FHIR resource. */ @@ -2206,19 +2220,19 @@ TEST(JsonFormatTest, InvalidControlCharactersReturnsError) { ASSERT_FALSE(PrettyPrintFhirToJsonString(proto).ok()); } -TEST(JsonFormatR4Test, PadsThreeDigitYearToFourCharacters) { +TEST(JsonFormatStu3Test, PadsThreeDigitYearToFourCharacters) { TestPairWithFilePaths( "testdata/jsonformat/observation_three_digit_year.prototxt", "testdata/jsonformat/observation_three_digit_year.json"); } -TEST(JsonFormatR4Test, DecimalCornerCases) { +TEST(JsonFormatStu3Test, DecimalCornerCases) { TestPairWithFilePaths( "testdata/jsonformat/observation_decimal_corner_cases.prototxt", "testdata/jsonformat/observation_decimal_corner_cases.json"); } -TEST(JsonFormatR4Test, NdjsonLocation) { +TEST(JsonFormatStu3Test, NdjsonLocation) { TestPairWithFilePaths( "testdata/jsonformat/location_ndjson.prototxt", "testdata/jsonformat/location_ndjson.json");