vapor / fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
https://docs.vapor.codes/4.0/fluent/overview/
MIT License
1.32k stars 172 forks source link

Compilation error with ModelTokenAuthenticatable conformance #780

Open matthewcheok opened 1 week ago

matthewcheok commented 1 week ago

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

0xTim commented 1 week ago

@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
matthewcheok commented 1 week ago

Unfortunately that does not work.

Screenshot 2024-10-05 at 7 22 10 PM
0xTim commented 1 week ago

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

matthewcheok commented 1 week ago

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: @.***>

0xTim commented 9 hours ago

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