feat(generator/golang): fold 3.1 const-based scalar oneOf/anyOf into enums - #617
Open
sloudel wants to merge 2 commits into
Open
feat(generator/golang): fold 3.1 const-based scalar oneOf/anyOf into enums#617sloudel wants to merge 2 commits into
sloudel wants to merge 2 commits into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…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.
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.
Problem
OpenAPI 3.1 expresses string/number/boolean enums as
oneOfof scalarconstvalues, e.g.The go model generator routes this through
populateUnionand emits araw
XXXXUnion{Raw json.RawMessage}; the enum values and names are lost,and
WithEnumConstantscan generate nothing.Fix
In
populateUnion, before settlingKindUnion, fold the union into aKindEnumwhen every non-null variant is a scalarconst(new helperconstScalarEnumFromVariants). Null-valued variants becomeNullable,object/array/union/disambiguation variants are left as unions.
Effects
XXXXUnion{Raw json.RawMessage}type PetStatus string+ typed constantsTests
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.