Skip to content

fix: pointer skipping for client query params#2330

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2329
Apr 29, 2026
Merged

fix: pointer skipping for client query params#2330
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2329

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2329

Parameter-level x-go-type-skip-optional-pointer: true on a map- or slice-typed query parameter produced a struct field without a pointer (correct) but a client builder that still dereferenced it (*params.Field), so the generated package failed to compile.

The params-struct path in GenStructFromSchema already read the parameter-level extension; DescribeParameters did not, leaving Schema.SkipOptionalPointer false on the ParameterDefinition that backs the client template. Mirror the override there. Also extend ZeroValueIsNil to recognize map[K]V so the emitted nil-check is not dropped when the optional pointer is skipped.

Adds a regression fixture under internal/test/issues/issue-2329 covering both the deepObject map and form-style slice cases.

Closes: oapi-codegen#2329

Parameter-level x-go-type-skip-optional-pointer: true on a map- or
slice-typed query parameter produced a struct field without a pointer
(correct) but a client builder that still dereferenced it
(*params.Field), so the generated package failed to compile.

The params-struct path in GenStructFromSchema already read the
parameter-level extension; DescribeParameters did not, leaving
Schema.SkipOptionalPointer false on the ParameterDefinition that
backs the client template. Mirror the override there. Also extend
ZeroValueIsNil to recognize map[K]V so the emitted nil-check is not
dropped when the optional pointer is skipped.

Adds a regression fixture under internal/test/issues/issue-2329
covering both the deepObject map and form-style slice cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mromaszewicz mromaszewicz requested a review from a team as a code owner April 21, 2026 16:10
@mromaszewicz

Copy link
Copy Markdown
Member Author

@greptileai

@greptile-apps

greptile-apps Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a compile error introduced when x-go-type-skip-optional-pointer: true is applied at the parameter level (rather than the schema level) on a map- or slice-typed query parameter: DescribeParameters did not propagate the extension to the ParameterDefinition, so the client template still emitted *params.Field against a non-pointer field. The fix mirrors the override into ParameterDefinition.Schema.SkipOptionalPointer and extends ZeroValueIsNil in both Property and ParameterDefinition to recognise map[K]V Go types, ensuring the nil-guard is still emitted when the optional pointer is skipped. A regression test fixture covering the deepObject map and form-style slice cases is added under internal/test/issues/issue-2329.

Confidence Score: 5/5

Safe to merge — the fix is narrowly scoped, includes a compile-verified regression test, and the generated fixture diff is consistent with the stated change.

No P0/P1 issues found. The two-part fix (propagating the extension in DescribeParameters, extending ZeroValueIsNil for map types) directly addresses the root cause. The components.gen.go behavioral change (nil map omitted rather than serialized as null) is a deliberate consequence acknowledged by the author. The heuristic strings.HasPrefix(GoType, "map[") is a pragmatic approach consistent with how the rest of the codebase treats GoType strings.

No files require special attention.

Important Files Changed

Filename Overview
pkg/codegen/operations.go Core fix: propagates x-go-type-skip-optional-pointer extension from parameter level to ParameterDefinition.Schema; also extends ZeroValueIsNil to recognize map[K]V Go types
pkg/codegen/schema.go Parallel fix: Property.ZeroValueIsNil extended to return true for map-typed properties, fixing nil-guard emission in custom MarshalJSON templates
internal/test/issues/issue-2329/issue2329.gen.go Regression fixture: generated client now uses value types for Tags map[string]string and Labels []string params, guarded by != nil checks instead of pointer derefs
internal/test/components/components.gen.go Regenerated: BodyWithAddPropsJSONBody.MarshalJSON now guards Inner map[string]int with a nil-check; nil map omitted instead of serialized as null
internal/test/issues/issue-2329/issue2329_test.go Well-structured regression tests covering nil params, map serialization (deepObject), slice serialization (form), and custom MarshalJSON nil-omission
internal/test/issues/issue-2329/openapi.yaml New OpenAPI fixture exercising both deepObject map and form-style array query params with x-go-type-skip-optional-pointer: true, plus a body schema with the same extension on properties
internal/test/issues/issue-2329/config.yaml Standard codegen config enabling models+client generation for the regression fixture
internal/test/issues/issue-2329/generate.go Standard go:generate directive for the regression fixture
pkg/codegen/schema_test.go Adds test case for map-typed object schema returning true from ZeroValueIsNil, exercising the new GoType heuristic

Reviews (3): Last reviewed commit: "fix: pointer skipping for body schema ma..." | Re-trigger Greptile

Comment thread pkg/codegen/operations.go
@mromaszewicz mromaszewicz added this to the v2.7.0 milestone Apr 21, 2026
mromaszewicz and others added 3 commits April 29, 2026 09:32
Property.ZeroValueIsNil mirrored ParameterDefinition.ZeroValueIsNil only
for arrays. A map-typed property on a body schema with a custom
MarshalJSON (i.e. one with additionalProperties or in oneOf/anyOf) and
x-go-type-skip-optional-pointer: true emitted no nil-check, so an unset
map field serialised as `"field":null` while an unset slice field with
the same flag was omitted.

Mirror the map-prefix check from operations.go so both kinds of zero
value are treated consistently in the additional-properties / union /
union-and-additional-properties templates.

Extends the issue-2329 fixture with a Thing body schema covering both
map and slice properties; the regression test marshals a zero-value
Thing and asserts neither field appears in the JSON object.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mromaszewicz mromaszewicz merged commit e8a39e1 into oapi-codegen:main Apr 29, 2026
18 checks passed
@mromaszewicz mromaszewicz deleted the fix/issue-2329 branch April 29, 2026 16:48
@mromaszewicz mromaszewicz added the bug Something isn't working label Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

x-go-type-skip-optional-pointer not respected for optional map/slice-typed query parameters

1 participant