-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Description
Suppose we have an endpoint with an optional query parameter called tags of type array. If we turn on prefer-skip-optional-pointer-on-container-types: true in the generator config, the generated client seems to always emit a query parameter for tags even when none is required.
Example
I've put together a failing test case in a fork which should be self-complete. But let me try to illustrate the problem here. The generator builds the following query parameter struct.
// GetRootParams defines parameters for GetRoot.
type GetRootParams struct {
Id *string `form:"id,omitempty" json:"id,omitempty"`
Tags []string `form:"tags,omitempty" json:"tags,omitempty"`
}If I then try to use the generated client to make a query specifying only ID, leaving tags unspecified:
_, _ = client.GetRoot(ctx, &issuedmrtemp.GetRootParams{
Id: ptr("some-id"),
})then I expect the client to make a http request to e.g. https://example.com/api/path?id=some-id. But what we actually see is https://example.com/api/path?id=some-id&tags= with an additional &tags=. I found that surprising enough to think "this must be a bug!"; hopefully you agree too.
Attempted workarounds
- I tried turning on
prefer-skip-optional-pointer-with-omitzero: truebut that seemed not to change how the query parameters were built. - I tried turning adding
x-go-type-skip-optional-pointer: falseto the query parameter's schema, per the comment
oapi-codegen/pkg/codegen/schema.go
Lines 920 to 922 in b72bb2a
// setSkipOptionalPointerForContainerType ensures that the "optional pointer" is skipped on container types (such as a slice or a map). // This is controlled using the `prefer-skip-optional-pointer-on-container-types` Output Option // NOTE that it is still possible to override this on a per-field basis with `x-go-type-skip-optional-pointer`
but again this didn't seem to affect the query parameter generation.
P.S.
Should have mentioned earlier, but I'd be happy to attempt a fix and submit a PR if the maintainers agree this is a bug. I, err, might need pointing in the the right direction of where/how to go about fixing this though!