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

Float unmarshaled to integer without failure #225

Open
edisonspencer opened this issue Jan 14, 2025 · 0 comments
Open

Float unmarshaled to integer without failure #225

edisonspencer opened this issue Jan 14, 2025 · 0 comments

Comments

@edisonspencer
Copy link

edisonspencer commented Jan 14, 2025

jsonapi.UnmarshalPayload seems to cast a float value to integer rather than failing. Keep in mind that the struct it will be unmarshaled into is expecting the value to be an integer rather than a float. Is this expected behaviour?

To illustrate what I mean, here is a working example:

package main

import (
	"fmt"
	"strings"

	"github.com/google/jsonapi"
)

type MyStruct struct {
	ID int `jsonapi:"attr,id"`
}

func main() {
	jsonStr := `{
		"data": {
			"attributes": {
				"id": 10.5
			}
		}
	}`
	var s MyStruct
	if err := jsonapi.UnmarshalPayload(strings.NewReader(jsonStr), &s); err != nil {
		fmt.Println("Error:", err)
	}
	fmt.Println("ID:", s.ID)
}

The output shown is:

ID: 10

I was expecting that jsonapi.UnmarshalPayload would instead return an error. Is there a way to enforce that the accepted type must be integer?

Many thanks in advance 🙏

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

1 participant