Skip to content

Clients send optional header array parameters when prefer-skip-optional-pointer-on-container-types is true, even when no value is specified #2234

@DMRobertsonOctopus

Description

@DMRobertsonOctopus

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: true but that seemed not to change how the query parameters were built.
  • I tried turning adding x-go-type-skip-optional-pointer: false to the query parameter's schema, per the comment
    // 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!

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