fluent icon indicating copy to clipboard operation
fluent copied to clipboard

Compilation error with ModelTokenAuthenticatable conformance

Open matthewcheok opened this issue 1 year ago • 5 comments

Describe the issue

I'm getting a compilation error after upgrading to Swift 6.0.1 and Vapor 4.106

Vapor version

4.106.0

Operating system and version

macOS 15.1

Swift version

Swift 6.0.1

Steps to reproduce

Here's the model for my Token entity.

final class Token: Model, @unchecked Sendable {

  init() { }

  init(
    id: UUID? = nil,
    userID: UserModel.IDValue,
    token: String,
    expiresAt: Date?)
  {
    self.id = id
    $user.id = userID
    value = token
    self.expiresAt = expiresAt
  }

  // MARK: Internal

  static let schema = "tokens"

  @ID var id: UUID?

  @Parent(key: "user_id")
  var user: UserModel

  @Field(key: "value")
  var value: String

  @Field(key: "expires_at")
  var expiresAt: Date?

  @Timestamp(key: "created_at", on: .create)
  var createdAt: Date?

}

// Error: Type 'Token' does not conform to protocol 'ModelTokenAuthenticatable'
extension Token: ModelTokenAuthenticatable {
  // Candidate has non-matching type 'any KeyPath<Token, FieldProperty<Token, String>> & Sendable' [with User = <<error type>>]
  static let valueKey = \Token.$value
  static let userKey = \Token.$user

  var isValid: Bool {
    guard let expiryDate = expiresAt else {
      return true
    }

    return expiryDate > Date()
  }
}

Outcome

Errors inline

Additional notes

No response

matthewcheok avatar Oct 05 '24 19:10 matthewcheok

@matthewcheok does it work if you change the key paths to

nonisolated(unsafe) static let valueKey = \Token.$value
nonisolated(unsafe) static let userKey = \Token.$user

0xTim avatar Oct 05 '24 23:10 0xTim

Unfortunately that does not work.

Screenshot 2024-10-05 at 7 22 10 PM

matthewcheok avatar Oct 05 '24 23:10 matthewcheok

I'm assuming UserModel is a sendable model as well? What does the fixit suggest?

0xTim avatar Oct 05 '24 23:10 0xTim

Yes it is sendable. It simply suggests removing the nonisolated(unsafe) attribute.

On Sat, Oct 5, 2024 at 7:34 PM Tim Condon @.***> wrote:

I'm assuming UserModel is a sendable model as well? What does the fixit suggest?

— Reply to this email directly, view it on GitHub https://github.com/vapor/fluent/issues/780#issuecomment-2395226296, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABCR6LCKVIBIG2HM2P25MPTZ2BZO3AVCNFSM6AAAAABPNSTHZCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJVGIZDMMRZGY . You are receiving this because you were mentioned.Message ID: @.***>

matthewcheok avatar Oct 05 '24 23:10 matthewcheok

This does look like an issue with ModelTokenAuthenticatable - I'll see if I can come up with a fix

0xTim avatar Oct 17 '24 18:10 0xTim

any updates here?

matthewcheok avatar Oct 26 '24 16:10 matthewcheok

If I declare these as computed properties, it seems to compile:

static var usernameKey: KeyPath<User, Field<String>> { \.$email }
static var passwordHashKey: KeyPath<User, Field<String>> { \.$password }

tonyarnold avatar Dec 31 '24 02:12 tonyarnold

@tonyarnold thanks for this!

@matthewcheok sorry for the delay, the solution Tony posted using computed properties is the correct one. You can see an example at https://github.com/0xTim/VaporAuthExample/blob/main/Sources/App/Models/Token.swift#L7-L8

0xTim avatar Jan 01 '25 17:01 0xTim