Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Float unmarshaled to integer without failure #225

@edisonspencer

Description

@edisonspencer

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 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions