pointfreeco/vapor-routing

More detailed JSON Decoding error message

nashysolutions opened this issue · 3 comments

The feedback for corrupt or unexpected data is lacking.

struct EditPoll: Codable, Equatable {
    let name: String
    let description: String
    let duration: Int
    let roomId: UUID
}

Route(.case(PollsRoute.create)) {
    Method.post
    Body(.json(EditPoll.self))
}

error: The data couldn’t be read because it isn’t in the correct format.

I think the following would print more specific information.

print(String(describing: jsonDecodingError))
``
saroar commented

hi did you fixed it same issue I am getting some work some not

@nashysolutions @saroar Can you flesh this issue out if you think you're experiencing a bug? A specific reproduction would be helpful for us to work with.

Sorry for late response @stephencelis

For any given body

let usersRoute = OneOf {
        
    Route(.case(UsersRoute.create)) {
        Method.post
        Body(.json(UserCandidate.self))
    }

If UserCandidate does not match the expected payload exactly, and decoding fails.

public struct UserCandidate: Codable {
    
    public let firstName: String // should be name, not firstName
    public let username: String
    public let password: String

Then the response is unhelpful.

error: The data couldn’t be read because it is missing.