Skip to content

fix(gen): support defaults for aliases of non-primitive types - #1765

Open
highpon wants to merge 1 commit into
ogen-go:mainfrom
highpon:fix/default-alias-array
Open

highpon wants to merge 1 commit into
ogen-go:mainfrom
highpon:fix/default-alias-array

Conversation

@highpon

@highpon highpon commented Sep 26, 2026

Copy link
Copy Markdown

Summary

Setting a default on a property that references a named array schema produces generated code that does not compile. This PR makes aliases of arrays, structs and maps render their defaults correctly.

Problem

Consider a named array schema referenced from an object property with a default:

components:
  schemas:
    TagList:
      type: array
      items:
        type: string
    Req:
      type: object
      properties:
        tags:
          $ref: '#/components/schemas/TagList'
          default: [a, b]
        nullableTags:
          anyOf:
            - $ref: '#/components/schemas/TagList'
            - type: 'null'
          default: [c]

ogen maps TagList to a Go alias tyand generates the following
setDefaults:

val := []string([]interface{}{"a", "s.Tags = TagList(val)

The generated package fails to build

cannot convert []interface{}{…} (valtype []string

The same happens for the nullable vafor aliases of enum arrays and object
arrays.

Fix

When the alias target is a primitive or an enum, the existing path is kept as is. Otherwise, the
template recurses into defaults/set for the alias target and converts the result to the alias type:

var defaultVal0 []string
var defaultVal1 []string
{
      var defaultVal1Elem string
      val := string("a")
      defaultVal1Elem = val
      defaultVal1 = append(defaultVal1, defaultVal1Elem)
}
// ...
defaultVal0 = defaultVal1
s.Tags = TagList(defaultVal0)

For Opt/OptNil of such aliases, the existing generic branch now handles them and delegates to the
alias branch above.

Tests

Added _testdata/positive/alias_array_defaults.yml and the integration test TestAliasArrayDefaults, covering:

  • $ref to a named string array
  • anyOf: [$ref, {type: 'null'}]
  • aliases of an enum array and an ob
  • an array whose items are a named a

The test verifies that:

  • decoding {} applies all defaults
  • explicit values and null overrid
  • a zero-value request sent through the generated client gets defaults applied on the server

With the previous template, the generated test package fails to build. Running go generate ./...
produces no diff in the existing gen

Defaults on a property that references a named array schema (e.g.
`$ref: TagList` or `anyOf: [$ref TagList, null]`, as emitted by TypeSpec
for `model TagList is Array<string>`) generated uncompilable code:

    val := []string([]interface{}{"a", "b"})

The alias branches of defaults/set always rendered the value via
defaults/val, which only handles primitives. Now aliases of arrays,
structs and maps recurse into defaults/set and convert the result to
the alias type.
@ernado ernado closed this Nov 13, 2026
@ernado ernado reopened this Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants