Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Setting a
defaulton 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:ogen maps
TagListto a Go alias tyand generates the followingsetDefaults:The generated package fails to build
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/setfor the alias target and converts the result to the alias type:For
Opt/OptNilof such aliases, the existing generic branch now handles them and delegates to thealias branch above.
Tests
Added
_testdata/positive/alias_array_defaults.ymland the integration testTestAliasArrayDefaults, covering:$refto a named string arrayanyOf: [$ref, {type: 'null'}]The test verifies that:
{}applies all defaultsnulloverridWith the previous template, the generated test package fails to build. Running
go generate ./...produces no diff in the existing gen