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

json.Unmarshaler interface not considered for nested types #93

Closed
peterstace opened this issue May 7, 2024 · 1 comment · Fixed by #94
Closed

json.Unmarshaler interface not considered for nested types #93

peterstace opened this issue May 7, 2024 · 1 comment · Fixed by #94

Comments

@peterstace
Copy link

I believe I may have found a false positive. The following minimum reproduction shows the false positive:

package main

import (
    "encoding/json"
    "fmt"
)

type Outer struct {
    Inner Inner `json:"inner"`
}

type Inner struct {
    String string
}

func (i *Inner) UnmarshalJSON(p []byte) error {
    var s string
    if err := json.Unmarshal(p, &s); err != nil {
        return err
    }
    i.String = s
    return nil
}

func main() {
    buf := []byte(`
        {
            "inner": "my string"
        }
    `)
    var outer Outer
    if err := json.Unmarshal(buf, &outer); err != nil {
        panic(err)
    }
    fmt.Println(outer)
}

The output of the program is:

{{my string}}

See https://go.dev/play/p/HEcyBYmfO_Y

However, musttag with the latest tag (v0.12.1) shows a false positive:

main.go:32:32: the given struct  should be annotated with the `json` tag

It doesn't seem to ignore the absence of JSON tags on the Inner type, even though the Inner type implements the jsonUnmarshaler interface.

@tmzane
Copy link
Member

tmzane commented May 8, 2024

Hi, thank you for the detailed report!

Looks like we only check if a type implements a (un)marshaler interface, but should also do it for a pointer-type.

I'll push the fix soon.

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

Successfully merging a pull request may close this issue.

2 participants