fix(massimo-cli): quote enum members by value type - #174
Open
fgiova wants to merge 1 commit into
Open
Conversation
getType decided enum quoting from the schema `type`, so a valid
untyped enum (`{ enum: ['a', 'b'] }`) emitted bare identifiers
(`a | b`) instead of string literals, producing non-compilable
.d.ts. A literal `null` in an enum also stringified to an empty
member (`'a' | `).
Quote per value by its runtime type instead. The pre-existing
early return for `type === undefined` + the string `'null'`
(nullable encoding) is untouched.
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.
Fixes #173
Problem
getType()decided whether to quote enum members from the schema'stypekeyword.typeis optional in JSON Schema / OpenAPI 3.x — theenumalready constrains the value — so a spec likefell into the
elsebranch and emitted bare identifiers:making the generated
index.d.mtsnon-compilable. The same enum with"type": "string"was generated correctly.A literal
nullinside anenumwas also stringified to an empty union member ('a' |), another syntax error.Change
Quote each member by its own runtime type instead of by the schema
type:The pre-existing early return above it (
type === undefined+ an enum containing the string'null'→null) is left untouched: that is the existing encoding for nullable types and would otherwise start emitting'null'.Behaviour
{ enum: ['a','b'] }a | b❌'a' | 'b'{ type: 'string', enum: ['a','b'] }'a' | 'b'{ enum: [1,2] }1 | 2{ enum: [true,false] }true | false{ enum: ["it's"] }it's❌'it\'s'{ enum: ['a', null] }'a' |❌'a' | nullTests
Added
support enum without typetopackages/massimo-cli/test/get-type.test.js, covering the table above plus an untyped enum nested in an object.test/get-type.test.jspasses (21/21), as dotest/cli-openapi*.test.jsandtest/frontend-openapi.test.js(81/81). Lint is clean.