Skip to content

json.Unmarshaler interface not considered for nested types #93

@peterstace

Description

@peterstace

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions