Skip to content

echo.Bind not working while binding query params to a struct with another nested anonymous struct's pointer in it #1858

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

Closed
3 tasks done
pwli0755 opened this issue Apr 23, 2021 · 2 comments · Fixed by #1861

Comments

@pwli0755
Copy link
Contributor

pwli0755 commented Apr 23, 2021

Issue Description

bind the query params into target struct's anonymous filed which is a pointer to struct,

note that if the payload in http body, it seems bind works fine,

issue happens only when bind query params with a struct with an anonymous filed which is a pointer to struct

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

curl 'http://localhost:8000/test?limit=1' 

{"limit":1,"offset":0}

Actual behaviour

bind skips the anonymous filed

curl 'http://localhost:8000/test?limit=1' 

{"limit":0,"offset":0}

Steps to reproduce

curl 'http://localhost:8000/test?limit=1' 

Working code to debug

package main

import (
	"fmt"
	"github.com/labstack/echo/v4"
	"net/http"
)

type PaginationParas struct {
	Limit  int `json:"limit" query:"limit"`
	Offset int `json:"offset" query:"offset"`
}
type Foo struct {
	*PaginationParas
}

func main() {
	e := echo.New()
	e.Debug = true
	e.GET("/test", func(ctx echo.Context) error {
		var f = Foo{PaginationParas: &PaginationParas{}}
		if err := ctx.Bind(&f); err != nil {
			fmt.Println(err)
			return err
		}
		return ctx.JSON(http.StatusOK, f)
	})
	e.Logger.Fatal(e.Start(":8000"))
}

Version/commit

v4.2.3-0.20210417194748-3b07058a1d8f

@pwli0755 pwli0755 changed the title echo.Bind not working while the target is a struct with another nested anonymous struct pointer in it echo.Bind not working while binding query params to a struct with another nested anonymous struct's pointer in it Apr 23, 2021
pwli0755 pushed a commit to pwli0755/echo that referenced this issue Apr 25, 2021
@pr1s10n3r
Copy link

I'm having kinda the same issue in version 4.12.0.

Expected behaviour

curl --request POST http://localhost:8080/buy?product=card
{ "Product": "card" }

Actual behaviour

{ "Product": "" }

Working code to debug

package main

import (
	"log"
	"net/http"

	"github.com/labstack/echo/v4"
)

type Request struct {
	Product string `query:"product"`
}

func main() {
	e := echo.New()

	e.POST("/buy", func(c echo.Context) error {
		req := Request{}
		if err := c.Bind(&req); err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
		return c.JSON(http.StatusOK, req)
	})

	if err := e.Start(":8080"); err != nil {
		log.Fatal(err)
	}
}

@aldas
Copy link
Contributor

aldas commented May 9, 2024

Query params are not bound for POST method.

echo/bind.go

Lines 122 to 131 in 88c379f

// Only bind query parameters for GET/DELETE/HEAD to avoid unexpected behavior with destination struct binding from body.
// For example a request URL `&id=1&lang=en` with body `{"id":100,"lang":"de"}` would lead to precedence issues.
// The HTTP method check restores pre-v4.1.11 behavior to avoid these problems (see issue #1670)
method := c.Request().Method
if method == http.MethodGet || method == http.MethodDelete || method == http.MethodHead {
if err = b.BindQueryParams(c, i); err != nil {
return err
}
}
return b.BindBody(c, i)

Something like that would work

	binder := echo.DefaultBinder{}
	e.POST("/buy", func(c echo.Context) error {
		req := Request{}
		if err := binder.BindQueryParams(c, &req); err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
		return c.JSON(http.StatusOK, req)
	})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants