Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Removing newline characters from operation id hashes #3163

Merged
merged 3 commits into from
Aug 1, 2023

Conversation

BobaFetters
Copy link
Member

  • Updating operation/fragment definition handling to remove newline characters

Closes #3162

- Updating operation/fragment definition handling to remove newline characters
@netlify
Copy link

netlify bot commented Jul 31, 2023

Deploy Preview for apollo-ios-docs canceled.

Name Link
🔨 Latest commit e31729c
🔍 Latest deploy log https://app.netlify.com/sites/apollo-ios-docs/deploys/64c953a6892f9e000853be73

@@ -14,7 +14,7 @@ struct OperationManifestItem {

var source = operation.definition.source.convertedToSingleLine()
for fragment in operation.referencedFragments {
source += #"\n\#(fragment.definition.source.convertedToSingleLine())"#
source += #" \#(fragment.definition.source.convertedToSingleLine())"#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recent change here was to make this a raw string with the # character for Swift. I think the root issue here is that because it is now a raw string the hash computation sees the raw two characters instead of when it was a regular string it would have been escaped down to an actual newline (single) character.

I don't know if it's 100% correct here to be replacing this with a string unless that's what we'll actually be sending - given that we're modifying the source it looks like it will be. Question then - does this break any backwards compatibility with older generated fragments or do we not need to care about that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, the raw string could be contributing to the issue here. I would lean towards still removing the newline character as I don't remember there being any specific reason to have it other than we thought we should keep it before each fragment. This route would bring us inline with how Kotlin is handling this as well.

As for backwards compatibility I don't think this should be an issue because the original change was very recent for persisted queries and with this bug operations containing fragments aren't working correctly to begin with.

@AnthonyMDev thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the new persisted queries, we should be changing to this in order to align with Kotlin. But for the legacy APQs, this is going to break backwards compatibility with existing registered APQs...

The bigger issue is that we can't just change this here. We need to also change the formatting of the operation document that is actually sent when we do a network request. And if we do that, we'll need it to format it differently for APQs vs new PQs... that's where this can start to get messy and really error prone.

I'm not sure how to address this well without breaking existing APQs...

Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are changing this, we definitely need to also change the way we send the document over the wire. We may need to chat about this further.

@@ -14,7 +14,7 @@ struct OperationManifestItem {

var source = operation.definition.source.convertedToSingleLine()
for fragment in operation.referencedFragments {
source += #"\n\#(fragment.definition.source.convertedToSingleLine())"#
source += #" \#(fragment.definition.source.convertedToSingleLine())"#
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the new persisted queries, we should be changing to this in order to align with Kotlin. But for the legacy APQs, this is going to break backwards compatibility with existing registered APQs...

The bigger issue is that we can't just change this here. We need to also change the formatting of the operation document that is actually sent when we do a network request. And if we do that, we'll need it to format it differently for APQs vs new PQs... that's where this can start to get messy and really error prone.

I'm not sure how to address this well without breaking existing APQs...

@@ -169,7 +169,7 @@ class IR {

var newline: String
for fragment in referencedFragments {
newline = #"\n"#
newline = "\n"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok so did a little bit of testing (thanks @calvincestari for calling out the raw string potentially being an issue) and believe the issue is the operation string in the json that is getting processed with the \n treated as an actual newline, but because we switched this piece of code in hash generation to a raw string it was being processed as the actual characters. After discussion with @AnthonyMDev this should fix the actual issue while maintaining backward compatibility for legacy apqs

@AnthonyMDev AnthonyMDev merged commit 2289679 into main Aug 1, 2023
4 checks passed
@AnthonyMDev AnthonyMDev deleted the fix/operation-id-hash branch August 1, 2023 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1.3.2 - Wrong \n hash with persistent queries
3 participants