ggicci/httpin

Decode parameter struct with default values only works the first time

branscha opened this issue · 2 comments

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)
}

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

ggicci commented

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