gorilla / schema

Package gorilla/schema fills a struct with form values.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
1.39k stars 231 forks source link

[BUG] omitempty in decoder using ignoreunknowkeys option not working #223

Open ehrktia opened 1 month ago

ehrktia commented 1 month ago

Is there an existing issue for this?

Current Behavior

type A struct{
F1  *int64  `schema:"f1"`
F2  string  `schema:"f2"`
}

the decoder option is set as below

d := schema.NewDecoder()
d.IgnoreUnknownKeys(true)
    if err := d.Decode(inp, v); err != nil {
        return err
    }

schema is decoded successfully but empty values are not omitted.

Expected Behavior

inp := new(A)

got result:

{"f1": 0 , "f2" : " "}

expected

{ }

docs - https://pkg.go.dev/github.com/gorilla/schema#Decoder.IgnoreUnknownKeys this suggest is similar to encoding/json behaviour when a un known key is encountered it will be skipped and valid keys will still be decoded. Am i doing something incorrect? is there any other tag i need to add? like omitempty

Steps To Reproduce

No response

Anything else?

docs - https://pkg.go.dev/github.com/gorilla/schema#Decoder.IgnoreUnknownKeys this suggest is similar to encoding/json behaviour when a un known key is encountered it will be skipped and valid keys will still be decoded. Am i doing something incorrect? is there any other tag i need to add? like omitempty