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

In Version 1.2, arrays of Union Types don't get set properly in mocks #3043

Closed
scottasoutherland opened this issue May 25, 2023 · 2 comments · Fixed by #3089
Closed

In Version 1.2, arrays of Union Types don't get set properly in mocks #3043

scottasoutherland opened this issue May 25, 2023 · 2 comments · Fixed by #3089
Assignees
Labels
bug Generally incorrect behavior needs investigation

Comments

@scottasoutherland
Copy link

scottasoutherland commented May 25, 2023

Summary

In version 1.1, we had code like

let itemOne = Mock<TypeOne>(propertyOne: "Hello World")
let itemTwo = Mock<TypeTwo>(propertyTwo: "Goodbye World")
let mock = Mock<OuterItemType>(
     description: "This is my outer type",
     items: [ itemOne, itemTwo]
)

Note that in this example, itemOne and itemTwo are two different types that are of the same Union type. So for example, our mock would have it defined as as something like

@Field<[MyUnionType]>("items") public var items but the initializer would take
items: [AnyMock]? = nil,

In 1.1.1 this worked fine. However in 1.2 after initializing the mock with items, the items property is still nil. And it is not set in the internal dictionary of the object. i.e if we print itemCollection._data it will only show __typename and description. I'm not 100% sure if the union type is relevant, but i believe it is. Because ultimately it is calling

  public func _set<T: MockFieldValue>(
    _ value: T.MockValueCollectionType.Element?,
    for keyPath: KeyPath<O.MockFields, Field<T>>
  ) {
    let field = O._mockFields[keyPath: keyPath]
    _data[field.key.description] = (value as? AnyHashable)
  }

but in this case value as? AnyHashable is nil. ( value as? [AnyHashable] however would have worked fine ). But i do see there are _set functions here for arrays, but i don't think they work because the typing is based on generics whereas we're passing [AnyMock].

Version

1.2.0

Steps to reproduce the behavior

  1. Create a schema that has a type with a property that is a nested union type of at least two different types.
  2. Generate the code for these types.
  3. Attempt to initialize a mock for the other type with an array of two different union types.
  4. Print the items property, note it is nil even though it was passed to the initializer.

Logs

No response

Anything else?

No response

@scottasoutherland scottasoutherland added bug Generally incorrect behavior needs investigation labels May 25, 2023
@AnthonyMDev
Copy link
Contributor

Thanks for this detailed report! We'll look into it as soon as we can!

@BobaFetters
Copy link
Member

The fix for this has been merged into main and will go out with our next patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior needs investigation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants