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

Cannot use context bind on a struct with "primitive.ObjectID" #1664

Closed
norx85 opened this issue Nov 3, 2020 · 10 comments
Closed

Cannot use context bind on a struct with "primitive.ObjectID" #1664

norx85 opened this issue Nov 3, 2020 · 10 comments

Comments

@norx85
Copy link

norx85 commented Nov 3, 2020

Issue Description

It's impossible to bind the body of a request on a struct with primitive.ObjectID from the official mongodb mongo-driver

Expected behaviour

The string value of the id correctly mapped to primitive.ObjectID and no errors, as it was on older releases (this problem happens to me after an upgrade to v4.1.17)

Actual behaviour

It gives this error:

code=400, message=unknown type, internal=unknown type

Steps to reproduce

Just bind to a struct with a primitive.ObjectID

Working code to debug

import (
	"github.com/labstack/echo"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

type User struct {
	ID                 primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
        ...
}

func handleUpdate(c echo.Context) error {
    item := new(User)
    err := c.Bind(item)
    if err != nil {
        log.Println(err.Error())
    }
    ...
}

Version/commit

v4.1.17

@pierremarsotlyon1
Copy link

Up !
Same problem

@iambenkay
Copy link
Contributor

iambenkay commented Dec 15, 2020

Hy @pierremarsotlyon1 and @norx85

If you have worked with a version of echo that does not have this issue could you also tell the version?

@pierremarsotlyon1
Copy link

Hi @iambenkay

I downgrade echo version to v3.3.10-dev.
Issue seems fixed.

I have this issue with echo version v4.1.17

@iambenkay
Copy link
Contributor

@pierremarsotlyon1 I can't seem to recreate this issue using v4.1.17
works perfectly.

here is a snippet https://repl.it/@iambenkay/Golang-Mongo-ID-bug

If possible tweak this to recreate the issue you're facing so the community can help

@pierremarsotlyon1
Copy link

You can reproduce with this snippet : https://repl.it/@PierreMarsot/Golang-Mongo-ID-bug
In fact, it's when we have a param in url

@iambenkay
Copy link
Contributor

You need to add the param tag to the struct field in order for it to be parsed as a query param. Here's a snippet

type User struct {
  ID primitive.ObjectID `param:"id,omitempty"`
}

the param type helps echo serialize the parameter from the url to the struct field.

@pierremarsotlyon1
Copy link

pierremarsotlyon1 commented Dec 15, 2020

With version 3, we don't need to do that

My struct :

type User struct {
	Id       primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
}

My url :
api.PUT("/:id", userController.Update)

I get my id param like this :
idUserToUpdate := c.Param("id")

It is outside of my struct

Basically, i can put a param in my url and don't have it in my struct

@iambenkay
Copy link
Contributor

iambenkay commented Dec 15, 2020

okay. well that was a breaking change between v3 and v4. The bind was improved to automatically bind the url params, the request body and the query params into the struct. Although there have been a few issues on the restrictive nature of using one bind call for all the data units in the request and a PR is in the works at #1681 to separate the bind functions for each of them.

I think this feature is currently reserved for v5

@aldas
Copy link
Contributor

aldas commented Dec 15, 2020

@pierremarsotlyon1 if you want only to handle json POST, avoid c.Bind() and just decode body directly to struct

type User struct {
	Id       primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
}
item := new(User)
if err := json.NewDecoder(c.Request().Body).Decode(item); err != nil {
	return err
}

and get path variable with c.Param("id").

@pierremarsotlyon1
Copy link

Sorry for delay
Ok thanks @aldas ! :)

@aldas aldas closed this as completed Feb 27, 2021
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

No branches or pull requests

4 participants