GraphQlClientGenerator icon indicating copy to clipboard operation
GraphQlClientGenerator copied to clipboard

#137 Escape Newlines in GraphQL String Values

Open kevinpjones opened this issue 2 years ago • 3 comments

There's probably a more elegant way to accomplish this, but it's working for me.
I don't see any test coverage for the BaseClasses resource, but point me in that direction if I'm wrong.

kevinpjones avatar May 11 '23 23:05 kevinpjones

You can use JsonEncodedText.Encode instead. If there is a new line in GraphQL values it should be

"Value" : "Line1\nLine2" or "Value" : """Line1 Line2"""

As is the generator, generate it "Value": "Line1 Line2"

chernobil avatar May 12 '23 10:05 chernobil

JsonEncodedText.Encode(...) from System.Text.Json? I didn't think there was an assumed dependency on that package. JsonConvert.ToString could be used, but the regex approach would remain in the case where GRAPHQL_GENERATOR_DISABLE_NEWTONSOFT_JSON is set.

Given the above, I'm inclined to switch to the triple-quote approach. It seems best not to have different string-escape behavior between Netwonsoft being enabled or not.

kevinpjones avatar May 12 '23 15:05 kevinpjones

I updated the PR to use Block Strings, which required updating a bunch of the tests. It's still not perfect, as the spec points out that non-printable ASCII characters can't be used with Block Strings. I'm good with that tradeoff, but interested to hear @Husqvik's take.

I'm also working on a Mac, which required normalizing line endings in the string comparisons being done throughout the tests. The only test that I couldn't fully get passing locally was MultipleFileGeneration: it compares file sizes, which will be platform dependent because of line endings. It passes in AppVeyor though 🤷‍♂️.

The generation could be updated to force line endings, but I'm not prepared to go that far right now.

kevinpjones avatar May 12 '23 18:05 kevinpjones

Fixed in https://github.com/Husqvik/GraphQlClientGenerator/commit/628b7aa012d5924a7035c82c282cd5078e4f4c68

Husqvik avatar Apr 06 '24 22:04 Husqvik