vapor / leaf

🍃 An expressive, performant, and extensible templating language built for Swift.
https://docs.vapor.codes/4.0/leaf/getting-started
MIT License
439 stars 81 forks source link

Fix behavior of Leaf context encoding and improve Leaf errors #212

Closed gwynne closed 2 years ago

gwynne commented 2 years ago

Types which use superEncoder(forKey:) in their Codable conformance (such as Fluent models) can now safely be provided to Leaf views as context objects. In general, handling of context encoding is significantly improved.

LeafError now conforms to AbortError and DebuggableError for improved error UI/UX.

dev-xdyang commented 2 years ago

this pull request is great, but I found a bug.

1: add a route like this, and pass empty list to todoList parameter

func routes(_ app: Application) throws {
    app.get { req async throws -> View in
        let context = DemoModel(title: "Demo", todoList: [])
        return try await req.view.render("demo", context)
    }
}

struct DemoModel: Encodable {
    let title: String
    let todoList: [String]
}

2: in leaf template file, use #count() tag to check the todoList count

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">

  <title>#(title)</title>
</head>

<body>
    #if(count(todoList) > 0):
          <h1>#(title)</h1>
    #else:
          <h1>#(title)</h1>
    #endif
</body>
</html>

when visit this page on browser will encounter error:

{
  "error": true,
  "reason": "Unable to convert count parameter to LeafData collection"
}

and I tested leaf 4.2.0, it work right!

0xTim commented 2 years ago

cc @gwynne looks like some behaviour has changed here

gwynne commented 2 years ago

Yeah, to say the least... I'll add some more tests to cover whatever this turns out to be.

gwynne commented 2 years ago

Nope, this is a bug. I'll be pushing a fix for it shortly.

gwynne commented 2 years ago

That should do it 🤞

VaporBot commented 2 years ago

These changes are now available in 4.2.1