-
Notifications
You must be signed in to change notification settings - Fork 225
fix: DefaultModelProvider return nil from not loaded state #2746
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
Conversation
| switch loadedState { | ||
| case .notLoaded: | ||
| throw CoreError.clientValidation("DefaultModelProvider does not provide loading capabilities", "") | ||
| return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main functional change. If the default model provider is called from a not loaded state, it should simply return nil. The caller (LazyReference) is responsible for returning nil on get() or throwing if require() is called.
| let nilParent = try await savedChild.parent | ||
| XCTAssertNil(nilParent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was what triggered the implementation change in the PR, the child without a parent should be query-able and can be traversed to the parent, even if the parent is nil. Previously, this call was throwing due to the default model provider throwing. Since the parent is an optional association, LazyReference gets nil back from the default model provider, and returns nil on get().
| .hasMany(compositePKParent.implicitChildren, is: .optional, ofType: ImplicitChild.self, associatedWith: ImplicitChild.keys.parent), | ||
| .hasMany(compositePKParent.strangeChildren, is: .optional, ofType: StrangeExplicitChild.self, associatedWith: StrangeExplicitChild.keys.parent), | ||
| .hasMany(compositePKParent.childrenSansBelongsTo, is: .optional, ofType: ChildSansBelongsTo.self, associatedWith: ChildSansBelongsTo.keys.compositePKParentChildrenSansBelongsToCustomId), | ||
| .hasMany(compositePKParent.childrenSansBelongsTo, is: .optional, ofType: ChildSansBelongsTo.self, associatedFields: [ChildSansBelongsTo.keys.compositePKParentChildrenSansBelongsToCustomId, ChildSansBelongsTo.keys.compositePKParentChildrenSansBelongsToContent]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this use case is the uni-directional has-many changes. childrenSansBelongsTo doesn't have a belongs to reference to the parent, and the parent (this schema) has a has-many field to the childrenSansBelongsTo child. With the CPK feature, it should have associatedFields of the CPK fields of the parent on the child.
| self.updatedAt = updatedAt | ||
| } | ||
| public mutating func setParent(parent: CompositePKParent? = nil) { | ||
| public mutating func setParent(_ parent: CompositePKParent? = nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what should be codegenerated (see https://github.com/aws-amplify/amplify-codegen/pull/504/files#r1098000791) and is updated here manually.
| // query child and load the parent - CompositePKChild | ||
| let queriedCompositePKChild = try await query(for: savedCompositePKChild) | ||
| assertLazyReference(queriedCompositePKChild._parent, | ||
| state: .notLoaded(identifiers: [.init(name: "@@primaryKey", value: savedParent.identifier)])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the belongsTo in #2737, the identifiers will be an array of LazyReferenceIdentifier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, i'll rebase 2737 with data-dev-preview and re-run/fix the integration tests
Issue #
Description
When there is no metadata to create a ModelProvider, the lazy reference will be instantiated with
nilidentifiers with a DefaultModelProvider. When this happens,try awaiton the computed property should simply returnnilonget()and throw onrequire().General Checklist
Given When Theninline code documentation and are named accordinglytestThing_condition_expectation()By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.