You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
regression in v2.5.0: marshalling optional array property emits null instead of omitting the array #2031
The changes in #1981 causes nil optional array properties to be marshaled as null (and thus not valid according to the schema) in some cases due to the changes in pkg/codegen/templates/additional-properties.tmpl, pkg/codegen/templates/union-and-additional-properties.tmpl, and pkg/codegen/templates/union.tmpl.
What seems to be required to trigger this regression is:
The array property is part of a union or other structure to trigger one of the above templates
The array property is optional (not in the required list)
The array property has x-go-type-skip-optional-pointer: true
This is common (at least for me) from prior versions because slices already can store nil, so an extra pointer layer is unnecessary
The change in the generated code looks something like this:
Change in this behavior is causing exactly the same regression here.
To provide more context, I'm seeing a problem related to #2021, where the setting of SkipOptionalPointer: globalState.options.OutputOptions.PreferSkipOptionalPointer, for referenced schema types is using the global setting, not that of the reference.
@magnetikonline While related, I think your issue is different from mine. In my case, many of the properties in question already had an explicit x-go-type-skip-optional-pointer: true, since the extra pointer layer was not needed to represent nil for a map or slice type.
My problem is that the codegen now always emits those fields, even if nil, instead of obeying their "not required ⇒ don't emit if nil" behavior from before.
I very much do not want to make them all pointers again, as that will be a messy refactor of the consuming code, and make that code a lot more janky.
My problem is that the codegen now always emits those fields, even if nil, instead of obeying their "not required ⇒ don't emit if nil" behavior from before.
The other two variants exhibit the same basic behavior, they are just going through other templates where unions (anyOf) are involved to force generation of a custom MarshalJSON instead of using additionalProperties to trigger that. I can expand the failing tests to encompass those if it helps.
I generally stand by the changes we made in #1981, but absolutely see that we should be taking into account the type we're operating on and whether that's a type that can be nullable i.e. whether it's an array/slice - I'm working on the fix now
Activity
magnetikonline commentedon Jul 22, 2025
Change in this behavior is causing exactly the same regression here.
To provide more context, I'm seeing a problem related to #2021, where the setting of
SkipOptionalPointer: globalState.options.OutputOptions.PreferSkipOptionalPointer,for referenced schema types is using the global setting, not that of the reference.magnetikonline commentedon Jul 23, 2025
👋 @mgabeler-lee-6rs - I dug more into my issue/regression and have landed what I think is a fix.
#2042
I'd be curious to see if this does indeed fix the problem for you and if it's related.
mgabeler-lee-6rs commentedon Jul 23, 2025
@magnetikonline While related, I think your issue is different from mine. In my case, many of the properties in question already had an explicit
x-go-type-skip-optional-pointer: true, since the extra pointer layer was not needed to representnilfor a map or slice type.My problem is that the codegen now always emits those fields, even if nil, instead of obeying their "not required ⇒ don't emit if nil" behavior from before.
I very much do not want to make them all pointers again, as that will be a messy refactor of the consuming code, and make that code a lot more janky.
magnetikonline commentedon Jul 24, 2025
thanks @mgabeler-lee-6rs for the clarification. 👍
jamietanna commentedon Sep 3, 2025
Sorry for the delay, and regression, taking a look hopefully today 👀
jamietanna commentedon Sep 3, 2025
@mgabeler-lee-6rs can you provide a minimal OpenAPI spec (snippet) for what this is triggering for?
fix: demonstrate additional-properties variant of oapi-codegen#2031
mgabeler-lee-6rs commentedon Sep 3, 2025
@jamietanna Here's a failing test for the first of the three variants noted in the issue description: https://github.com/mgabeler-lee-6rs/oapi-codegen/tree/issue-2031-failing-test/internal/test/issues/issue-2031
The other two variants exhibit the same basic behavior, they are just going through other templates where unions (
anyOf) are involved to force generation of a customMarshalJSONinstead of usingadditionalPropertiesto trigger that. I can expand the failing tests to encompass those if it helps.jamietanna commentedon Nov 1, 2025
Apologies for the delay - taking a look now. Thanks for the failing test + minimal example, that's super helpful 🙇
jamietanna commentedon Nov 1, 2025
I generally stand by the changes we made in #1981, but absolutely see that we should be taking into account the type we're operating on and whether that's a type that can be nullable i.e. whether it's an array/slice - I'm working on the fix now
chore(templates): add `Property.ZeroValueIsNil` helper method
chore(templates): add `Property.RequiresPointerCheck` helper method
fix(templates): correctly nil check arrays
chore(templates): add `Property.ZeroValueIsNil` helper method
chore(templates): add `Property.RequiresPointerCheck` helper method
fix(templates): correctly nil check arrays
jamietanna commentedon Nov 1, 2025
#2118 will be the fix 👍
chore(templates): add `Property.ZeroValueIsNil` helper method
chore(templates): add `Property.RequiresPointerCheck` helper method
fix(templates): correctly nil check arrays