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

Fix LeafContext.application Casting Failure #235

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Leaf/Application+Leaf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Application {

public var renderer: LeafRenderer {
var userInfo = self.userInfo
userInfo["application"] = self
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the crux of the issue. self here is of type Application.Leaf, but we want to assign an instance of Application for this key.

userInfo["application"] = self.application

var cache = self.cache
if self.application.environment == .development {
Expand Down
13 changes: 10 additions & 3 deletions Tests/LeafTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ final class LeafTests: XCTestCase {
func testContextUserInfo() throws {
var test = TestFiles()
test.files["/foo.leaf"] = """
Hello #custom()! @ #source()
Hello #custom()! @ #source() app nil? #application()
"""

struct CustomTag: LeafTag {
Expand All @@ -133,6 +133,12 @@ final class LeafTests: XCTestCase {
.string(ctx.request?.url.path ?? "application")
}
}

struct ApplicationTag: LeafTag {
func render(_ ctx: LeafContext) throws -> LeafData {
.string(ctx.application != nil ? "non-nil app" : "nil app")
}
}

let app = Application(.testing)
defer { app.shutdown() }
Expand All @@ -142,6 +148,7 @@ final class LeafTests: XCTestCase {
app.leaf.cache.isEnabled = false
app.leaf.tags["custom"] = CustomTag()
app.leaf.tags["source"] = SourceTag()
app.leaf.tags["application"] = ApplicationTag()
app.leaf.sources = .singleSource(test)
app.leaf.userInfo["info"] = "World"

Expand All @@ -152,7 +159,7 @@ final class LeafTests: XCTestCase {
try app.test(.GET, "test-file") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.contentType, .html)
XCTAssertEqual(res.body.string, "Hello World! @ /test-file")
XCTAssertEqual(res.body.string, "Hello World! @ /test-file app nil? non-nil app")
}

app.get("test-file-with-application-renderer") { req in
Expand All @@ -162,7 +169,7 @@ final class LeafTests: XCTestCase {
try app.test(.GET, "test-file-with-application-renderer") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.contentType, .html)
XCTAssertEqual(res.body.string, "Hello World! @ application")
XCTAssertEqual(res.body.string, "Hello World! @ application app nil? non-nil app")
}
}

Expand Down