Skip to content

Commit

Permalink
Fixes bug in C++ parser where empty contained resources would print w…
Browse files Browse the repository at this point in the history
…ith incorrect trailing comma. See #25

PiperOrigin-RevId: 380668911
  • Loading branch information
nickgeorge authored and Cameron Tew committed Jun 21, 2021
1 parent f09ae59 commit d802f7d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
11 changes: 7 additions & 4 deletions cc/google/fhir/json_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ class Printer {
}

OpenJsonObject();
std::vector<const FieldDescriptor*> 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<const FieldDescriptor*> 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" &&
Expand Down
18 changes: 16 additions & 2 deletions cc/google/fhir/r4/json_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -479,6 +480,19 @@ TEST(JsonFormatR4Test, PrintForAnalyticsWithContained) {
}
}

TEST(JsonFormatR4Test, WithEmptyContainedResourcePrintsValidJson) {
const Parameters proto = ReadProto<Parameters>(
"testdata/r4/examples/Parameters-empty-resource.prototxt");
absl::StatusOr<std::string> 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<std::string> files{"Account-ewg", "Account-example"};
TestPair<Account>(files);
Expand Down
26 changes: 20 additions & 6 deletions cc/google/fhir/stu3/json_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -111,7 +112,7 @@ void TestPrintForAnalytics(const std::string& proto_filepath,
const std::string& json_filepath, bool pretty) {
R proto = ReadProto<R>(proto_filepath);
if (IsProfile(R::descriptor())) {
proto = NormalizeR4(proto).value();
proto = NormalizeStu3(proto).value();
}
auto result = pretty ? PrettyPrintFhirToJsonStringForAnalytics(proto)
: PrintFhirToJsonStringForAnalytics(proto);
Expand Down Expand Up @@ -202,6 +203,19 @@ TEST(JsonFormatStu3Test, PrintAnalyticsElementIdsDropped) {
"testdata/jsonformat/location_element_with_ids_analytic.json");
}

TEST(JsonFormatStu3Test, WithEmptyContainedResourcePrintsValidJson) {
const Parameters proto = ReadProto<Parameters>(
"testdata/stu3/examples/Parameters-empty-resource.prototxt");
absl::StatusOr<std::string> 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. */
Expand Down Expand Up @@ -2206,19 +2220,19 @@ TEST(JsonFormatTest, InvalidControlCharactersReturnsError) {
ASSERT_FALSE(PrettyPrintFhirToJsonString(proto).ok());
}

TEST(JsonFormatR4Test, PadsThreeDigitYearToFourCharacters) {
TEST(JsonFormatStu3Test, PadsThreeDigitYearToFourCharacters) {
TestPairWithFilePaths<Observation>(
"testdata/jsonformat/observation_three_digit_year.prototxt",
"testdata/jsonformat/observation_three_digit_year.json");
}

TEST(JsonFormatR4Test, DecimalCornerCases) {
TEST(JsonFormatStu3Test, DecimalCornerCases) {
TestPairWithFilePaths<Observation>(
"testdata/jsonformat/observation_decimal_corner_cases.prototxt",
"testdata/jsonformat/observation_decimal_corner_cases.json");
}

TEST(JsonFormatR4Test, NdjsonLocation) {
TEST(JsonFormatStu3Test, NdjsonLocation) {
TestPairWithFilePaths<Location>(
"testdata/jsonformat/location_ndjson.prototxt",
"testdata/jsonformat/location_ndjson.json");
Expand Down

0 comments on commit d802f7d

Please sign in to comment.