Skip to content

feat(generator/golang): fold 3.1 const-based scalar oneOf/anyOf into enums - #617

Open
sloudel wants to merge 2 commits into
pb33f:mainfrom
sloudel:fix/const-scalar-enum
Open

feat(generator/golang): fold 3.1 const-based scalar oneOf/anyOf into enums#617
sloudel wants to merge 2 commits into
pb33f:mainfrom
sloudel:fix/const-scalar-enum

Conversation

@sloudel

@sloudel sloudel commented Aug 14, 2026

Copy link
Copy Markdown

Problem

OpenAPI 3.1 expresses string/number/boolean enums as oneOf of scalar
const values, e.g.

PetStatus:
  oneOf:
    - const: available
      title: Available
    - const: pending
    - const: sold

The go model generator routes this through populateUnion and emits a
raw XXXXUnion{Raw json.RawMessage}; the enum values and names are lost,
and WithEnumConstants can generate nothing.

Fix

In populateUnion, before settling KindUnion, fold the union into a
KindEnum when every non-null variant is a scalar const (new helper
constScalarEnumFromVariants). Null-valued variants become Nullable,
object/array/union/disambiguation variants are left as unions.

Effects

before after
XXXXUnion{Raw json.RawMessage} type PetStatus string + typed constants

Tests

  • TestJSONSchema202012ConstScalarEnumVariants — const enum, string/int,
    anyOf, nullable, mixed->any, compile-check.
  • TestJSONSchemaSchemaObjectUnionNotEnum — object variants stay unions.

Verified: go build ./generator/..., go vet ./generator/golang/,
go test ./generator/golang/ — all green.

…enums

OpenAPI 3.1 expresses string/number/boolean enums as `oneOf` of scalar
`const` values (e.g. oneOf: [ {const: available}, {const: pending} ]).
The model generator treated those as raw json.RawMessage unions,
discarding the values, so enum constants were never emitted.

Recognize a union whose non-null variants are all scalar consts and build
it as a KindEnum instead (honouring null-of variants as nullable and mixed
value types as `any`), so `WithEnumConstants` can render typed aliases and
constants. Object/array/union/disambiguation variants that carry consts as
properties are left as unions.

Adds coverage for const enums, anyOf const, integer const enums, nullable
const enums, mixed const types, and guards the object union from being
collapsed into an enum.
@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.78%. Comparing base (ca6ca73) to head (0ee2731).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #617   +/-   ##
=======================================
  Coverage   99.78%   99.78%           
=======================================
  Files         283      283           
  Lines       34456    34489   +33     
=======================================
+ Hits        34382    34415   +33     
  Misses         46       46           
  Partials       28       28           
Flag Coverage Δ
unittests 99.78% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…aths

Add a table-driven unit test that hits every veto branch of
constScalarEnumFromVariants: nil/empty variant lists, a nil member, a
variant without a const, declared object/array types, , nested
composition keywords, explicit enum, properties/patternProperties, items,
prefixItems, and a null-only list. Raises the function to 100% coverage so
the codecov patch gate stays green.
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.

1 participant