Skip to content
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

Updateable protocol issue with null values #33

Closed
ipodishima opened this issue Aug 11, 2017 · 1 comment
Closed

Updateable protocol issue with null values #33

ipodishima opened this issue Aug 11, 2017 · 1 comment

Comments

@ipodishima
Copy link

ipodishima commented Aug 11, 2017

Vapor Toolbox: 2.0.3
Vapor Framework: 2.1.2

Given the following code, Updateable protocol can not convert null to nil for the string as expected:

final class Reminder: Model {

    var calendarItemIdentifier: String?

 struct Properties {
        static let idKey                     = "id"
        static let calendarItemIdentifierKey = "calendar_item_identifier"
    }

 init(row: Row) throws {
        calendarItemIdentifier = try? row.get(Properties.calendarItemIdentifierKey)
}

 func makeRow() throws -> Row {
        var row = Row()
        try row.set(Properties.calendarItemIdentifierKey, calendarItemIdentifier)
}

extension Reminder: Preparation {
    static func prepare(_ database: Database) throws {
        try database.create(self) { reminder in
            reminder.id()
            reminder.string(Properties.calendarItemIdentifierKey, optional: true)
        }
    }
    
    static func revert(_ database: Database) throws {
        try database.delete(self)
    }
}

extension Reminder: Updateable {
    public static var updateableKeys: [UpdateableKey<Reminder>] {
        return [
            UpdateableKey(Properties.calendarItemIdentifierKey, String.self) { object, calendarItemIdentifier in
                object.calendarItemIdentifier = calendarItemIdentifier
            }
        ]
    }
}

Attempting to patch through

    func update(req: Request, reminder: Reminder) throws -> ResponseRepresentable {
        try reminder.update(for: req)
        
        try reminder.save()
        return try reminder
    }

With

{
  "calendar_item_identifier": null
}

Fluent is throwing an error

capture d ecran 2017-08-11 a 09 56 26
capture d ecran 2017-08-11 a 09 56 43

@tanner0101
Copy link
Contributor

You need to use String? instead of String if you want to support nil.

UpdateableKey(Properties.calendarItemIdentifierKey, String?.self)

Feel free to reopen if this doesn't fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants