fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🐛 [Bug]: Incorrect Parsing of Slice by `QueryParser()` with Embedded Structs

Open ecoderat opened this issue 1 year ago • 20 comments

Bug Description

When the "Products" field is within the "Person" struct, the QueryParser() correctly parses it as a slice. However, when I separate the "Products" field into another struct and embed it, the QueryParser() incorrectly parses it as a single string instead of a slice.

How to Reproduce

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
)

type Person struct {
	Name     string   `query:"name"`
	Pass     string   `query:"pass"`
	A
}

type A struct {
	Products []string `query:"products"`
}


func main() {
	app := fiber.New(fiber.Config{
			EnableSplittingOnParsers: true,
		})

	app.Get("/", func(c *fiber.Ctx) error {
		p := new(Person)

		if err := c.QueryParser(p); err != nil {
			return err
		}

		log.Println(p.Name)
		log.Println(p.Pass)
		log.Println(p.Products)

		return c.JSON(p)
	})

	app.Listen(":3000")
}
$ curl "http://localhost:3000/?name=john&pass=doe&products=shoe,hat" | jq

{
    "Name": "john",
    "Pass": "doe",
    "Products": [
        "shoe,hat"
    ]
}

Expected Behavior

$ curl "http://localhost:3000/?name=john&pass=doe&products=shoe,hat" | jq

{
    "Name": "john",
    "Pass": "doe",
    "Products": [
        "shoe",
        "hat"
    ]
}

Fiber Version

v2.49.2

Code Snippet (optional)

No response

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my problem prior to opening this one.
  • [X] I understand that improperly formatted bug reports may be closed without explanation.

ecoderat avatar Feb 14 '24 08:02 ecoderat

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Feb 14 '24 08:02 welcome[bot]

We will look into it

ReneWerner87 avatar Feb 14 '24 10:02 ReneWerner87

how do i reproduce the app. *I am a beginner

shahkashish avatar Mar 18 '24 12:03 shahkashish