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

Generated test Mock code produces compilation error when object contains property named "hash" #2950

Closed
renanbdias opened this issue Apr 11, 2023 · 4 comments · Fixed by #2965
Assignees
Labels
bug Generally incorrect behavior codegen Issues related to or arising from code generation planned-next Slated to be included in the next release

Comments

@renanbdias
Copy link

Summary

Bug report

Currently, there's a compilation error in generated mock code when the object contains a property named "hash". The compiler throws an error inside the convenience initializer of the mock object, when it tries to assign a value to the "hash" property.

This is the the error message: 'let' property 'hash' may not be initialized directly; use "self.init(...)" or "self = ..." instead

Versions

  • apollo-ios SDK version: 1.1.1
  • apollo-ios-cli version: 1.1.1
  • Xcode version: 14.3
  • Swift version: 5.8

Steps to reproduce

  1. Execute code generation command using apollo-ios-cli using a schema with a response object that has a property named hash.

Further details

Here's the generated code (all other properties have been redacted) that it generates to mock a response object of an operation:

// @generated
// This file was automatically generated and should not be edited.

import ApolloTestSupport
import ApplinGraphQL

public class SomeGenerated__ApolloType: MockObject {
  public static let objectType: Object = ApplinGraphQL.Objects.SomeGenerated__ApolloType
  public static let _mockFields = MockFields()
  public typealias MockValueCollectionType = Array<Mock<SomeGenerated__ApolloType>>

  public struct MockFields {
    // beginning of properties...

    @Field<String>("hash") public var hash

    // rest of the properies...
  }
}

public extension Mock where O == SomeGenerated__ApolloType {
  convenience init(
    // params...

    hash: String? = nil,

    // rest of params...
  ) {
    self.init()
    // properties...

    self.hash = hash  // This causes "'let' property 'hash' may not be initialized directly; use "self.init(...)" or "self = ..." instead"

    // rest of properties...
  }
}

Manually editing and renaming this property to anything -- say, _hash -- already solves the problem. But this is obviously not a solution as the file will be overwritten every time you run the code generation script.

Version

1.1.1

Steps to reproduce the behavior

  1. Execute code generation command using apollo-ios-cli using a schema with a response object that has a property named hash

Logs

No response

Anything else?

No response

@renanbdias renanbdias added bug Generally incorrect behavior needs investigation labels Apr 11, 2023
@calvincestari
Copy link
Member

calvincestari commented Apr 11, 2023

Thanks for reporting this @renanbdias. It doesn't look like hash is in our list of reserved keywords that will be escaped in generated code. I'll do some more digging into this - thanks.

@calvincestari calvincestari added the codegen Issues related to or arising from code generation label Apr 11, 2023
@calvincestari calvincestari self-assigned this Apr 11, 2023
@calvincestari calvincestari added needs investigation planned-next Slated to be included in the next release and removed needs investigation labels Apr 11, 2023
@calvincestari calvincestari added this to the Patch Releases (1.1.x) milestone Apr 14, 2023
@calvincestari
Copy link
Member

After doing some investigation today I've figured out it's got nothing to do with hash being a reserved word. It's not in our list of reserved words because it doesn't need to be backticked, hence why the generated model with a property named hash builds without error.

The root cause here is Mock conforming to Hashable and then a subsequent conflict between the hash(into:) function and any property named hash. I'm not 100% sure why that's a conflict, it may have something to do with the fact that we're using @dynamicMemberLookup for the property but either way we've got a solution that I'm working on at the moment. Hopefully I'll have it ready to go out with the next patch release.

@renanbdias
Copy link
Author

That sounds awesome, @calvincestari
Thank you so much.

@calvincestari
Copy link
Member

The fix for this is merged into main and will 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 planned-next Slated to be included in the next release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants