Skip to content

Movie & MovieListItem Encodable conformance is incorrect #223

@gannonprudhomme

Description

@gannonprudhomme

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions