Skip to content

Commit

Permalink
Fix for wrong behavior of Json codec when record schema has no fields (
Browse files Browse the repository at this point in the history
…#2833)

Co-authored-by: Thiruvalluvan M G <thiru@afer.com>
  • Loading branch information
thiru-mg and Thiruvalluvan M G committed Apr 6, 2024
1 parent ee647a6 commit cffffe7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/c++/impl/parsing/JsonCodec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ class JsonEncoder : public Encoder {
template<typename P, typename F>
void JsonEncoder<P, F>::init(OutputStream &os) {
out_.init(os);
parser_.reset();
}

template<typename P, typename F>
Expand Down
14 changes: 14 additions & 0 deletions lang/c++/impl/parsing/Symbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ template<typename Handler>
class SimpleParser {
Decoder *decoder_;
Handler &handler_;
/*
* parsingStack always has root at the bottom of it.
* So it is safe to call top() on it.
*/
std::stack<Symbol> parsingStack;

static void throwMismatch(Symbol::Kind actual, Symbol::Kind expected) {
Expand Down Expand Up @@ -742,6 +746,14 @@ public:
} else if (s.kind() == Symbol::Kind::SkipStart) {
parsingStack.pop();
skip(*decoder_);
} else if (s.kind() == Symbol::Kind::Indirect) {
ProductionPtr pp = s.extra<ProductionPtr>();
parsingStack.pop();
append(pp);
} else if (s.kind() == Symbol::Kind::Symbolic) {
ProductionPtr pp(s.extra<std::weak_ptr<Production>>());
parsingStack.pop();
append(pp);
} else {
break;
}
Expand All @@ -756,6 +768,8 @@ public:
while (parsingStack.size() > 1) {
parsingStack.pop();
}
Symbol &s = parsingStack.top();
append(boost::tuples::get<0>(*s.extrap<RootInfo>()));
}
};

Expand Down
15 changes: 15 additions & 0 deletions lang/c++/test/CodecTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,11 @@ static const TestData data[] = {
{R"({"type":"map", "values": "boolean"})",
"{c1sK5Bc2sK5BsK5B}", 2},

// Record with no fields
{"{\"type\":\"record\",\"name\":\"empty\",\"fields\":[]}",
"", 1},

// Single-field records
{"{\"type\":\"record\",\"name\":\"r\",\"fields\":["
"{\"name\":\"f\", \"type\":\"boolean\"}]}",
"B", 1},
Expand Down Expand Up @@ -1002,6 +1007,16 @@ static const TestData data[] = {
"{\"name\":\"f7\", \"type\":\"bytes\"}]}",
"NBILFDS10b25", 1},
// record of records
{"{\"type\":\"record\",\"name\":\"r\",\"fields\":["
"{\"name\":\"f1\",\"type\":\"boolean\"},"
"{\"name\":\"f2\", \"type\":{\"type\":\"record\","
"\"name\":\"inner\",\"fields\":[]}}]}",
"B", 1},
{"{\"type\":\"record\",\"name\":\"r\",\"fields\":["
"{\"name\":\"f1\",\"type\":\"boolean\"},"
"{\"name\":\"f2\", \"type\":{\"type\":\"array\","
"\"items\":\"r\"}}]}",
"B[]", 1},
{"{\"type\":\"record\",\"name\":\"outer\",\"fields\":["
"{\"name\":\"f1\", \"type\":{\"type\":\"record\", "
"\"name\":\"inner\", \"fields\":["
Expand Down

0 comments on commit cffffe7

Please sign in to comment.