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

🐛 [Bug]: Unclear "json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64" #3025

Closed
3 tasks done
kokizzu opened this issue Jun 2, 2024 · 9 comments

Comments

@kokizzu
Copy link

kokizzu commented Jun 2, 2024

Bug Description

Currently it doesn't show which struct have that are uint64 that cannot be parsed by BodyParser

How to Reproduce

Steps to reproduce the behavior:

  1. Create a struct with multiple field like this: uint64 json:"fieldNameXX,string" field, an endpoint, call BodyParser with payload unquoted an quoted
  2. Got those error

Expected Behavior

It would show which field failed to be parsed

for quality of life it should be more permissive between quoted and unquoted number

Fiber Version

v2.52.4

Code Snippet (optional)

package main

import (
	"log"

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

func main() {
	app := fiber.New()

	type Foo struct {
		A uint64 `json:"a,string"`
		B int64  `json:"b,string"`
		C uint64 `json:"c"`
		D int64  `json:"d"`
	}

	app.Post("/", func(c *fiber.Ctx) error {
		var f Foo
		err := c.BodyParser(&f)
		if err != nil {
			return err
		}
		return c.JSON(f)
	})

	log.Fatal(app.Listen(":3000"))
}

/*
curl localhost:3000 -X POST --data '{"a":123,"b":"123","c":"123","d":123}' -H "Content-Type: application/json"
json: invalid use of ,string struct tag, trying to unmarshal unquoted value into int64%
*/

Checklist:

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

welcome bot commented Jun 2, 2024

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

@renanbastos93
Copy link
Member

renanbastos93 commented Jun 2, 2024

@kokizzu It doesn't sound like a bug because your JSON is wrong. I've tested using json.Unmarshal from Go.
see below

package main

import (
	"encoding/json"
	"fmt"
)

type Foo struct {
	A uint64 `json:"a,string"`
	B int64  `json:"b,string"`
	C uint64 `json:"c"`
	D int64  `json:"d"`
}

func main() {
	var f Foo
	// json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64 {0 123 0 123}
	err := json.Unmarshal([]byte(`{"a":123,"b":"123","c":"123","d":123}`), &f)
	fmt.Println(err, f)

	// success
	err = json.Unmarshal([]byte(`{"a":"123","b":"123","c":123,"d":123}`), &f)
	fmt.Println(err, f)
}

Try using this CURL: curl localhost:3000 -X POST --data '{"a":"123","b":"123","c":123,"d":123}' -H "Content-Type: application/json"

@kokizzu
Copy link
Author

kokizzu commented Jun 2, 2024

yes, what i'm asking is clearer error message

@kokizzu
Copy link
Author

kokizzu commented Jun 2, 2024

I mean if i have 24 fields, i didn't know which one is the issue, this one is just minimal example of 4 fields, i know for sure what's wrong for this one, but if there's 24 fields, that's pain

@ReneWerner87
Copy link
Member

@kokizzu We use the encoder or decoder of the standard packet internally
This could be improved
Can you create an issue report in the golang main repository

@kokizzu
Copy link
Author

kokizzu commented Jun 2, 2024

apparently this related
golang/go#34472
golang/go#22816

@renanbastos93
Copy link
Member

@kokizzu Of course, but it isn't a bug from Fiber. I've been trying to understand where this error message is concatenated, but I haven't found it yet.
According to @ReneWerner87 , this problem is on Golang's main repository.

@renanbastos93
Copy link
Member

Hey @kokizzu, I apologize if my communication hasn't been very clear. I think I understood your suggestion. If you need help contributing to the Golang main repository, just let me know.

@kokizzu
Copy link
Author

kokizzu commented Jun 2, 2024

no problem, end up using json5 decoder with number-string patch instead

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

No branches or pull requests

3 participants