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

requireBody() and requireNoBody() not working as expected #123

Open
ptoffy opened this issue Apr 20, 2023 · 0 comments
Open

requireBody() and requireNoBody() not working as expected #123

ptoffy opened this issue Apr 20, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@ptoffy
Copy link
Member

ptoffy commented Apr 20, 2023

Describe the bug

Whilst creating a custom UnsafeUnescapedLeafTag, context.requireNoBody() always throws even if no body is provided, while context.requireBody() never throws even when no body is provided, rather returning an empty [Syntax].

While looking through the tests I noticed these methods are not tested anywhere and I think simply checking for the array being empty in addition to checking for it being != nil could solve the issue. The other option would be searching where the body of the tag is passed in and setting it to nil if it's an empty array

To Reproduce

  1. Create a simple project just using Leaf 4
  2. Create
struct BodyRequiringTag: UnsafeUnescapedLeafTag {
    func render(_ ctx: LeafContext) throws -> LeafData {
        _ = try ctx.requireBody()
        
        return .string("Hello there")
    }
}

and

struct NoBodyRequiringTag: UnsafeUnescapedLeafTag {
    func render(_ ctx: LeafContext) throws -> LeafData {
        try ctx.requireNoBody()
        
        return .string("General Kenobi")
    }
}
  1. Add the tags to the test project
app.leaf.tags["bodytag"] = BodyRequiringTag()
app.leaf.tags["nobodytag"] = NoBodyRequiringTag()
  1. Adding the first tag to the project
#bodytag:#endbodytag

or

#bodytag()

This returns Hello there, which is just the return type of the tag, while it should be returning {error: true, reason: "Missing body"

  1. Adding the other tag
#nobodytag

This returns {error: true, reason: "Extraneous body"}
The only way to make this error go away is to set the template as

#nobodytag:#endnobodytag

Which is, I think, not that intuitive

Environment

  • Vapor Framework version: 4.76.0
  • Vapor Toolbox version: 18.6.0
  • OS version: MacOS Ventura 13.0
  • Xcode version: 14.1 (14B47b)
@ptoffy ptoffy added the bug Something isn't working label Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant