Skip to content

regression in v2.5.0: marshalling optional array property emits null instead of omitting the array #2031

Closed

Description

@mgabeler-lee-6rs

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:

-       if t.Foo != nil {
-               object["foo"], err = json.Marshal(t.Foo)
-               if err != nil {
-                       return nil, fmt.Errorf("error marshaling 'foo': %w", err)
-               }
+       object["foo"], err = json.Marshal(t.Foo)
+       if err != nil {
+               return nil, fmt.Errorf("error marshaling 'foo': %w", err)
        }

I consider this a regression because it causes it to emit a schema-invalid JSON document after parsing a valid one.

Thinking through the logic, I find:

  • v2.4.0:
    • Skip emitting a property if it is not required and is nil
  • v2.5.0:
    • Skip emitting a property if it has a pointer layer and is nil
  • What I think it should do:
    • Skip emitting a property if either:
    • It has an optional pointer and is nil (implies it is not required)
    • It is not required and is an inherently nillable type (slice, map) and is nil

Activity

  1. magnetikonline commented on Jul 22, 2025

    @magnetikonline
    Contributor

    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.

  2. magnetikonline commented on Jul 23, 2025

    @magnetikonline
    Contributor

    👋 @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.

  3. mgabeler-lee-6rs commented on Jul 23, 2025

    @mgabeler-lee-6rs
    ContributorAuthor

    @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.

  4. magnetikonline commented on Jul 24, 2025

    @magnetikonline
    Contributor

    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.

    thanks @mgabeler-lee-6rs for the clarification. 👍

  5. jamietanna commented on Sep 3, 2025

    @jamietanna
    Member

    Sorry for the delay, and regression, taking a look hopefully today 👀

  6. jamietanna commented on Sep 3, 2025

    @jamietanna
    Member

    @mgabeler-lee-6rs can you provide a minimal OpenAPI spec (snippet) for what this is triggering for?

  7. mgabeler-lee-6rs commented on Sep 3, 2025

    @mgabeler-lee-6rs
    ContributorAuthor

    @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 custom MarshalJSON instead of using additionalProperties to trigger that. I can expand the failing tests to encompass those if it helps.

  8. jamietanna commented on Nov 1, 2025

    @jamietanna
    Member

    Apologies for the delay - taking a look now. Thanks for the failing test + minimal example, that's super helpful 🙇

  9. jamietanna commented on Nov 1, 2025

    @jamietanna
    Member

    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

  10. jamietanna commented on Nov 1, 2025

    @jamietanna
    Member

    #2118 will be the fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions