-
Notifications
You must be signed in to change notification settings - Fork 19
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
chore: allow absent data
in ToManyRelationship
#110
base: main
Are you sure you want to change the base?
Conversation
6bce13f
to
0491598
Compare
0491598
to
9b6392a
Compare
Would the |
Unfortunately, it will not work. When I specify |
The same list request but with additional parameters returns the same JSON as above + data 😥. Inventing "data" : [ {
"type" : "bundleIds",
"id" : "bundleId_id",
"attributes" : {
"some" : "attributes"
},
"relationships" : {
"bundleIdCapabilities" : {
"meta" : {
"meta": "data"
},
"links" : {
"related" : "https://api.appstoreconnect.apple.com/v1/bundleIds/bundleId_id/bundleIdCapabilities"
}
"data" : [ {
"type" : "bundleIdCapabilities",
"id" : "id2"
}, {
"type" : "bundleIdCapabilities",
"id" : "id2"
} ], |
I'm not totally ruling out the option you've coded up here, because it does feel like a very appropriate use-case, but I am hesitant to make this change because it means that in situations where I want to assert that I expect all relationships to be returned in the response Maybe some way to make the handling you've introduced in this PR optional, so I can choose between turning on "handle omitted data as empty array" or not (the default being to error out)? |
I’ve got your point. I can think of two options:
|
I can think of two other options:
I think I probably lean toward one of The nice thing about the protocol idea is that it targets individual relationships which could be useful compared to toggling the behavior for a whole project. |
7b22017
to
33ee3fa
Compare
I've got a mix 😅. You can't access the previous coding key while decoding an exact relationship object, so there will be no access to a static on Another impossible thing is generic type determination while decoding. So, I've got a flag protocol with a static variable inside. Tesource description object must confirm this protocol. Made inherited conformance in the The solution is dynamic, so any time you can turn on/off this behavior for the same relationship type 🎉. And added default conformance of |
@@ -222,7 +222,7 @@ extension Optional: JSONAPIIdentifiable, OptionalRelatable, JSONTyped where Wrap | |||
} | |||
|
|||
// MARK: Codable | |||
private enum ResourceLinkageCodingKeys: String, CodingKey { | |||
enum ResourceLinkageCodingKeys: String, CodingKey { |
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 for test_ToManyRelationshipWithMetaNoDataNotOmittable
to check the exact thrown error.
…ithOptionalDataInRelationships
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 looks like a good way to get what you need. I'd like to make the requested change to the test, but other than that I approve.
I may want to tweak the name of the protocol or method when I go to write documentation for this before cutting the next release of the library, but that can wait until I am actually sitting there writing the docs because that's when I'll probably decide what sounds most intuitive. The bones of this implementation make sense.
do { | ||
_ = try decodedThrows(type: ToManyWithMeta.self, | ||
data: to_many_relationship_with_meta_no_data) | ||
XCTFail("Expected decoding to fail.") | ||
} catch let error as DecodingError { | ||
switch error { | ||
case .keyNotFound(ResourceLinkageCodingKeys.data, _): | ||
break | ||
default: | ||
XCTFail("Expected error to be DecodingError.keyNotFound(.data), but got \(error)") | ||
} | ||
} catch { | ||
XCTFail("Expected to have DecodingError.keyNotFound(.data), but got \(error)") | ||
} |
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 would prefer to keep the coding keys private, not do an @testable import
, and test the description of the error instead of the exact coding key it outputs. This is just a preference of mine, I don't think there's a right or wrong way to go about testing this.
The test could use the XCTAssertThrowsError
to clean up a bit of the XCTFail
work done above and it would look something like:
XCTAssertThrowsError(
try decodedThrows(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta_no_data)
) { error in
XCTAssertEqual(error.localizedDescription, "The data couldn’t be read because it is missing.")
}
Getting back to this because I'd like to get it merged. I realized my last comment may have been ambiguous (did I want you to make the change or would I be making it?) -- apologies if you were waiting on me! I've addressed my feedback now. After giving this another full read-through, I am noticing that because I may do some thinking on that here today. If you have thoughts on it, please let me know! I fully understand if you've moved on from this PR after so long, though. |
…e localized error are not so good
476c7ec
to
74c9230
Compare
Changes
Allow ToManyRelationship to have no
data
.Make an early exit early from the initializer by setting an empty array to
idsWithMeta
if the container has nodata
key.Reasoning
I've just started working with App Store Connect API. Their API conforms to JSON:API.
For starters, I want to list Bundle IDs. That API doesn't list
data
for relationships unless they are specified in the requestinclude
. But even specifyinginclude
is limited to one element only. Yes, it's an array, I didn't check it with Bundle ID yet, but I am sure about this behavior in Profiles API).So, there is no
data
inrelationships
, but there are useful links to fetch relationships for each element.Sample JSON: