Skip to content

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

Closed
@pwli0755

Description

@pwli0755

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions