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

SelectionSet Generated Initializers Don't Compile with self Parameter #3330

Closed
grantjbutler opened this issue Feb 1, 2024 · 5 comments · Fixed by apollographql/apollo-ios-dev#257
Assignees
Labels
bug Generally incorrect behavior codegen Issues related to or arising from code generation

Comments

@grantjbutler
Copy link

Summary

If a schema has a field named self, and you turn on the selectionSetInitializers option for codegen, the code that's generated will not compile, as the parameter generated for the self field in the initializer will take precedence over the self keyword that refers to the instance that conforms to SelectionSet that's being created. The compiler emits the error Incorrect argument label in call (have '_dataDict:', expected '_fieldData:') for the call to self.init(_dataDict: ...) that's generated.

Version

1.8

Steps to reproduce the behavior

  1. Have a schema with some model with a field named self, and reference that field in the structure of a response to query in some way.
  2. Turn on the selectionSetInitializers option for code gen, to generate initializers for all data types from the schema.
  3. Run the code generator.
  4. Build the target with the generated code.

Logs

No response

Anything else?

The code generator that shipped with the 0.x set of releases properly handled this scenario, if you're looking for a reference implementation to adapt. That code would generate the parameter as _self while keeping the argument label as self to keep the naming correct.

parametersForProperties created the list of arguments for a generated initializer. To determine what to call the parameters, it would call internalParameterName for each argument, which in turn would call isValidParameterName to check to see if using the field's name would cause issues and would need special handling. It's this function that says "if the field is named self, then that's going to cause issues", with would tell internalParameterName to prefix the name of the parameter with an underscore to avoid conflicts.

@grantjbutler grantjbutler added bug Generally incorrect behavior needs investigation labels Feb 1, 2024
@calvincestari
Copy link
Member

Thanks for raising the issue @grantjbutler, it looks like a particularly tricky one to resolve. 🤔

@calvincestari
Copy link
Member

@grantjbutler I believe I have a solution for this but it won't make it into the release today. We'll issue a patch release after that to include this fix.

@calvincestari calvincestari added codegen Issues related to or arising from code generation and removed needs investigation labels Feb 2, 2024
@grantjbutler
Copy link
Author

Sounds good. Thanks for looking into this so quickly and coming up with a fix for this bug!

Copy link
Contributor

github-actions bot commented Feb 5, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

@calvincestari
Copy link
Member

calvincestari commented Feb 5, 2024

@grantjbutler - this is fixed and merged. It's available in the main branch if you can target that otherwise it'll go out in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior codegen Issues related to or arising from code generation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants