-
Notifications
You must be signed in to change notification settings - Fork 45
Open
Description
The bug
Describe the bug
Movie & MovieListItem fails when encoded then decoded, throwing the error:
Caught error: .typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "releaseDate", intValue: nil)], debugDescription: "Expected to decode String but found number instead.", underlyingError: nil))(error is identical for both of them)
To Reproduce
Run this test:
@Test("Encode then decode MovieListItem")
func symmetricCoding_movieListItem() throws { // aside: I call encode + decode tests "symmetric coding tests"
let item = MovieListItem.bulletTrain /// Your mock from MovieListItem+Mocks (as long as it has a `releaseDate` it'll fail)
let encoded = try JSONEncoder().encode(item)
let decoded = try JSONDecoder().decode(MovieListItem.self, from: encoded) // Throws an error here
#expect(item == decoded)
}
@Test("Encode then decode Movie")
func symmetricCoding_movie() throws {
let item = Movie.bulletTrain
let encoded = try JSONEncoder().encode(item)
let decoded = try JSONDecoder().decode(Movie.self, from: encoded) // Throws an error here
#expect(item == decoded)
}Expected behavior
Encoding & decoding a Movie, MovieListItem, etc, should succeed.
Discussion
Pretty straight-forward: MovieListItem.releaseDate & Movie.releaseDate are decoded as Strings, but because there isn't a custom encoding for them so it attempts to encode & store it as an Int. (presumably b/c of Date's default encoding?) Then on decode it tries to parse it as a String and fails.
For context, I'm caching MovieListItem so I can restore a task in case the user leaves in the middle of it, and I just tested Movie to see if it failed too and it did.
Metadata
Metadata
Assignees
Labels
No labels