Skip to content

feat(v3): apply schema default values via generated SetDefaults (#138) - #326

Merged
lerenn merged 1 commit into
mainfrom
feat/138-default-values
Jun 12, 2026
Merged

lerenn merged 1 commit into
mainfrom
feat/138-default-values

Conversation

@lerenn

@lerenn lerenn commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Issue

Fixes #138default values in the specification were parsed into the schema but never used in the generated code.

Change

Object schemas now get a SetDefaults() method when at least one optional (pointer) property declares a scalar default. Calling it fills unset (nil) fields with their default and leaves already-set fields untouched:

func (t *SettingsSchema) SetDefaults() {
    if t.Retries == nil {
        v := int64(3)
        t.Retries = &v
    }
    // ...
}

Supported default types: string, boolean, integer (int32/int64) and number (float32/float64), matching the field type produced by the schema-name template. Date/time, object and array defaults are intentionally out of scope. Two new v3 template helpers (hasScalarDefault, defaultLiteral) drive the generation.

Test

test/v3/issues/138 defines a Settings schema with integer/number/boolean/string defaults; the suite asserts SetDefaults() fills unset fields and preserves set ones. Before the change the method is not generated, so the package fails to compile; after, it passes.

test/v3/issues/224's golden file is regenerated (its margin property gains a SetDefaults). go generate ./... is otherwise clean.

🤖 Generated with Claude Code

Default values declared in the specification were parsed but never used.
Object schemas now get a SetDefaults() method that fills optional
(pointer) scalar fields with their default when unset, leaving
already-set fields untouched. Supports string, boolean, integer and
number defaults.

Fixes #138

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lerenn
lerenn merged commit 87b1089 into main Jun 12, 2026
5 checks passed
@lerenn
lerenn deleted the feat/138-default-values branch June 12, 2026 15:18
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.

AsyncAPI v3: support default values

1 participant