Skip to content

Commit ac6643d

Browse files
eshelyaronJanWielemaker
authored andcommitted
FIXED: Also escape line terminators when writing graphql strings...
...as per https://spec.graphql.org/draft/#StringCharacter
1 parent 00583a8 commit ac6643d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

graphql.pl

+12
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,10 @@
11821182
graphql_write_value(V, Options).
11831183

11841184

1185+
%! graphql_write_string(+Codes, +Options)// is det.
1186+
%
1187+
% Generates Codes, except that codes in Codes that are not allowed in
1188+
% GraphQL string values are replaced by their escape sequences.
11851189
graphql_write_string([], _Options) --> !, [].
11861190
graphql_write_string([0'\"|T], Options) -->
11871191
!,
@@ -1191,6 +1195,14 @@
11911195
!,
11921196
"\\\\",
11931197
graphql_write_string(T, Options).
1198+
graphql_write_string([0'\n|T], Options) -->
1199+
!,
1200+
"\\n",
1201+
graphql_write_string(T, Options).
1202+
graphql_write_string([0'\r|T], Options) -->
1203+
!,
1204+
"\\r",
1205+
graphql_write_string(T, Options).
11941206
graphql_write_string([H|T], Options) -->
11951207
[H],
11961208
graphql_write_string(T, Options).

test_graphql.pl

+21
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,24 @@
5454
graphql_read_document(codes(Codes), Doc, []).
5555

5656
:- end_tests(graphql_round_trip).
57+
58+
59+
:- begin_tests(graphql_strings).
60+
61+
test(escape_line_terminators) :-
62+
String = "Hello,\r\nnewline!",
63+
graphql_document_to_string(
64+
{| graphql(String) ||
65+
query { foo(bar: <String>
66+
baz: """
67+
Hello,
68+
multi-
69+
lines!
70+
"""
71+
) } |},
72+
Text,
73+
[]
74+
),
75+
string_lines(Text, [_]).
76+
77+
:- end_tests(graphql_strings).

0 commit comments

Comments
 (0)