ggicci / httpin

🍡 HTTP Input for Go - HTTP Request from/to Go Struct (Bi-directional Data Binding between Go Struct and http.Request)
https://ggicci.github.io/httpin/
MIT License
315 stars 23 forks source link

Decode parameter struct with default values only works the first time #77

Closed branscha closed 11 months ago

branscha commented 11 months ago

The first time the resolver is stored in a cache. The second time the resolver is loaded from the cache and seems to be modified, it cannot apply the default value anymore.

Second time there is an error message:

invalid field "Page": resolve field "Page (int)" failed: execute directive "default" with args [default] failed: strconv.ParseInt: parsing "default": invalid syntax

Version 014.1 The resolver attempts to parse the value "default" in stead of the value from the struct tag.

Test to reproduce the error:

type ThingWithDefaultValues struct {
    Id      uint `in:"query=id;required"`
    Page    int  `in:"query=page;default=1"`
    PerPage int  `in:"query=page_size;default=127"`
}

func TestDirectiveDefault2(t *testing.T) {

    r, _ := http.NewRequest("GET", "/?id=123", nil)

    expected := &ThingWithDefaultValues{
        Id:      123,
        Page:    1,
        PerPage: 127,
    }

        // First decode works as expected
    xxx := ThingWithDefaultValues{}
    err := httpin.Decode(r, &xxx)
    assert.NoError(t, err)
    assert.Equal(t, expected, &xxx)

        // Second decode generates eror
    err = httpin.Decode(r, &xxx)
    assert.NoError(t, err)
    assert.Equal(t, expected, &xxx)
}
branscha commented 11 months ago

I reverted back to previous version 0.11.0 which solves my problem for the time being.

ggicci commented 11 months ago

Thank you @branscha , will look into this soon :)