Skip to content

Commit f4c16e0

Browse files
committed
Fix #44: trailing comma when printing null value
1 parent 32680f6 commit f4c16e0

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

JsonVisitor.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ void JsonVisitor::printLocation(const yy::location &location)
2626
<< ",\"end\":" << location.end.column << '}';
2727
}
2828

29-
void JsonVisitor::startPrintingNode(const char *kind, const yy::location &location) {
29+
void JsonVisitor::startPrintingNodeWithoutTrailingComma(const char *kind, const yy::location &location) {
3030
out_.str("");
3131
out_ << "{\"kind\":\"" << kind << "\",\"loc\":";
3232
printLocation(location);
33+
}
34+
35+
void JsonVisitor::startPrintingNode(const char *kind, const yy::location &location) {
36+
startPrintingNodeWithoutTrailingComma(kind, location);
3337
out_ << ',';
34-
return;
3538
}
3639

3740
void JsonVisitor::printChildList(
@@ -349,7 +352,7 @@ void JsonVisitor::endVisitEnumValue(const EnumValue &enumValue) {
349352
}
350353

351354
void JsonVisitor::endVisitNullValue(const NullValue &nullValue) {
352-
startPrintingNode("NullValue", nullValue.getLocation());
355+
startPrintingNodeWithoutTrailingComma("NullValue", nullValue.getLocation());
353356
out_ << '}';
354357
printed_.back().emplace_back(out_.str());
355358
}

JsonVisitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class JsonVisitor : public AstVisitor {
3434

3535
// Print the opening of a new JSON dictionary, the node kind, and
3636
// the node location.
37+
void startPrintingNodeWithoutTrailingComma(const char *kind, const yy::location &location);
38+
39+
// Like startPrintingNodeWithoutTrailingComma, but adds a trailing
40+
// comma for convenience.
3741
void startPrintingNode(const char *kind, const yy::location &location);
3842

3943
void printLocation(const yy::location &location);

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
66

77
FILE(COPY valgrind.supp DESTINATION .)
88

9-
ADD_EXECUTABLE(runTests ParserTests.cpp BuildCAPI.c)
9+
ADD_EXECUTABLE(runTests ParserTests.cpp JsonVisitorTests.cpp BuildCAPI.c)
1010

1111
TARGET_LINK_LIBRARIES(runTests gtest gtest_main)
1212

test/JsonVisitorTests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2016, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#include <gtest/gtest.h>
11+
12+
#include "Ast.h"
13+
#include "GraphQLParser.h"
14+
#include "c/GraphQLAstToJSON.h"
15+
16+
using namespace facebook::graphql;
17+
using namespace facebook::graphql::ast;
18+
19+
TEST(JsonVisitorTests, NullValueEmitsValidJSONWithoutTrailingComma) {
20+
const char *error = nullptr;
21+
auto AST = parseString("{field(arg: null)}", &error);
22+
ASSERT_STREQ(nullptr, error) << "GraphQL parser error: " << error;
23+
const char *json = graphql_ast_to_json((const struct GraphQLAstNode *)AST.get());
24+
25+
EXPECT_STREQ(
26+
json,
27+
"{\"kind\":\"Document\",\"loc\":{\"start\":1,\"end\":19},\"definitions\":[{\"kind\":\"OperationDefinition\",\"loc\":{\"start\":1,\"end\":19},\"operation\":\"query\",\"name\": null,\"variableDefinitions\":null,\"directives\":null,\"selectionSet\":{\"kind\":\"SelectionSet\",\"loc\":{\"start\":1,\"end\":19},\"selections\":[{\"kind\":\"Field\",\"loc\":{\"start\":2,\"end\":18},\"alias\":null,\"name\":{\"kind\":\"Name\",\"loc\":{\"start\":2,\"end\":7},\"value\":\"field\"},\"arguments\":[{\"kind\":\"Argument\",\"loc\":{\"start\":8,\"end\":17},\"name\":{\"kind\":\"Name\",\"loc\":{\"start\":8,\"end\":11},\"value\":\"arg\"},\"value\":{\"kind\":\"NullValue\",\"loc\":{\"start\":13,\"end\":17}}}],\"directives\":null,\"selectionSet\":null}]}}]}");
28+
}

0 commit comments

Comments
 (0)