-
-
Notifications
You must be signed in to change notification settings - Fork 421
feat: Support generating erasable syntax enum-style types #1278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
bjohnso5
wants to merge
1
commit into
acacode:main
from
bjohnso5:feat/1040-implement-erasable-syntax-object-enums
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
112 changes: 112 additions & 0 deletions
112
tests/spec/enumsErasableSyntax/__snapshots__/basic.test.ts.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`basic > enumsErasableSyntax 1`] = ` | ||
| "/* eslint-disable */ | ||
| /* tslint:disable */ | ||
| // @ts-nocheck | ||
| /* | ||
| * --------------------------------------------------------------- | ||
| * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## | ||
| * ## ## | ||
| * ## AUTHOR: acacode ## | ||
| * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## | ||
| * --------------------------------------------------------------- | ||
| */ | ||
|
|
||
| /** @format int32 */ | ||
| export type SomeInterestEnum = { | ||
| Bla: 6; | ||
| Blabla: 2; | ||
| Boiler: 1; | ||
| Bbabab: 67; | ||
| Nowadays: 88; | ||
| FAIL: 122; | ||
| Vvvvv: 88; | ||
| ASdasAS: 0; | ||
| ASDsacZX: 213; | ||
| Zook: 12378; | ||
| EnumMm: 123125; | ||
| VCsa: 32452; | ||
| Yuuu: 1111; | ||
| ASddd: 66666; | ||
| ASdsdsa: "ASdsdsa"; | ||
| ASDds: "ASDds"; | ||
| HSDFDS: "HSDFDS"; | ||
| }; | ||
|
|
||
| /** @format int32 */ | ||
| export type EnumWithMoreNames = { | ||
| Bla: 1; | ||
| Blabla: "Blabla"; | ||
| Boiler: "Boiler"; | ||
| }; | ||
|
|
||
| export type StringCompleteEnums = { | ||
| Bla: "foo"; | ||
| Blabla: "bar"; | ||
| Boiler: "baz"; | ||
| }; | ||
|
|
||
| export type StringEnums = { | ||
| Bla: "foo"; | ||
| Blabla: "bar"; | ||
| Boiler: "Boiler"; | ||
| }; | ||
|
|
||
| export type SimpleEnumNonNullable = { | ||
| Value0: 0; | ||
| Value1: 1; | ||
| Value2: 2; | ||
| Value3: 3; | ||
| Value4: 4; | ||
| Value5: 5; | ||
| }; | ||
|
|
||
| export type XNullableEnum = { | ||
| Value0: 0; | ||
| Value1: 1; | ||
| Value2: 2; | ||
| Value3: 3; | ||
| Value4: 4; | ||
| Value5: 5; | ||
| }; | ||
|
|
||
| export type Currency = { | ||
| USDollar: "USD"; | ||
| Euro: "EUR"; | ||
| RussianRuble: "RUB"; | ||
| }; | ||
|
|
||
| export interface ObjWithEnum { | ||
| "prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null; | ||
| "prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5; | ||
| } | ||
|
|
||
| export type OnlyEnumNames = { | ||
| Bla: "Bla"; | ||
| Blabla: "Blabla"; | ||
| Boiler: "Boiler"; | ||
| }; | ||
|
|
||
| export type StringOnlyEnumNames = { | ||
| Bla: "Bla"; | ||
| Blabla: "Blabla"; | ||
| Boiler: "Boiler"; | ||
| }; | ||
|
|
||
| /** @format int32 */ | ||
| export type EmptyEnum = { | ||
| Bla: "Bla"; | ||
| Blabla: "Blabla"; | ||
| Boiler: "Boiler"; | ||
| }; | ||
|
|
||
| export interface PostFooPayload { | ||
| someTypeId?: 1 | 2 | 3 | 4 | 5; | ||
| } | ||
|
|
||
| export interface PostFooParams { | ||
| testKek: 1 | 2 | 3 | 4 | 5; | ||
| } | ||
| " | ||
| `; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import * as fs from "node:fs/promises"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
|
|
||
| import { afterAll, beforeAll, describe, expect, test } from "vitest"; | ||
|
|
||
| import { generateApi } from "../../../src/index.js"; | ||
|
|
||
| describe("basic", async () => { | ||
| let tmpdir = ""; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api")); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await fs.rm(tmpdir, { recursive: true }); | ||
| }); | ||
|
|
||
| test("enumsErasableSyntax", async () => { | ||
| await generateApi({ | ||
| fileName: "schema", | ||
| input: path.resolve(import.meta.dirname, "schema.json"), | ||
| output: tmpdir, | ||
| silent: true, | ||
| extractRequestParams: true, | ||
| extractRequestBody: true, | ||
| extractResponseBody: true, | ||
| extractResponseError: true, | ||
| generateClient: false, | ||
| generateErasableSyntaxEnums: true, | ||
| }); | ||
|
|
||
| const content = await fs.readFile(path.join(tmpdir, "schema.ts"), { | ||
| encoding: "utf8", | ||
| }); | ||
|
|
||
| expect(content).toMatchSnapshot(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| { | ||
| "swagger": "2.0", | ||
| "schemes": ["https"], | ||
| "host": "ffff.com", | ||
| "basePath": "/", | ||
| "info": {}, | ||
| "definitions": { | ||
| "objWithEnum": { | ||
| "type": "object", | ||
| "properties": { | ||
| "prop-enum-nullable": { | ||
| "type": "integer", | ||
| "x-nullable": true, | ||
| "enum": [0, 1, 2, 3, 4, 5] | ||
| }, | ||
| "prop-enum": { | ||
| "type": "integer", | ||
| "enum": [0, 1, 2, 3, 4, 5] | ||
| } | ||
| } | ||
| }, | ||
| "currency": { | ||
| "type": "string", | ||
| "enum": ["USD", "EUR", "RUB"], | ||
| "x-enumNames": ["US Dollar", "Euro", "Russian Ruble"] | ||
| }, | ||
| "x-nullable-enum": { | ||
| "type": "integer", | ||
| "x-nullable": true, | ||
| "enum": [0, 1, 2, 3, 4, 5] | ||
| }, | ||
| "simple-enum-non-nullable": { | ||
| "type": "integer", | ||
| "enum": [0, 1, 2, 3, 4, 5] | ||
| }, | ||
| "OnlyEnumNames": { | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "StringOnlyEnumNames": { | ||
| "type": "int32", | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "StringEnums": { | ||
| "type": "int32", | ||
| "enum": ["foo", "bar"], | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "StringCompleteEnums": { | ||
| "type": "int32", | ||
| "enum": ["foo", "bar", "baz"], | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "EmptyEnum": { | ||
| "format": "int32", | ||
| "type": "integer", | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "EnumWithMoreNames": { | ||
| "format": "int32", | ||
| "type": "integer", | ||
| "enum": [1], | ||
| "x-enumNames": ["Bla", "Blabla", "Boiler"] | ||
| }, | ||
| "SomeInterestEnum": { | ||
| "format": "int32", | ||
| "enum": [ | ||
| 6, 2, 1, 67, 88, 122, 88, 0, 213, 12378, 123125, 32452, 1111, 66666 | ||
| ], | ||
| "type": "integer", | ||
| "x-enumNames": [ | ||
| "Bla", | ||
| "Blabla", | ||
| "Boiler", | ||
| "Bbabab", | ||
| "Nowadays", | ||
| "FAIL", | ||
| "Vvvvv", | ||
| "ASdasAS", | ||
| "ASDsacZX", | ||
| "Zook", | ||
| "EnumMm", | ||
| "VCsa", | ||
| "Yuuu", | ||
| "ASddd", | ||
| "ASdsdsa", | ||
| "ASDds", | ||
| "HSDFDS" | ||
| ] | ||
| } | ||
| }, | ||
| "paths": { | ||
| "/foo": { | ||
| "post": { | ||
| "operationId": "postFoo", | ||
| "parameters": [ | ||
| { | ||
| "in": "query", | ||
| "name": "testKek", | ||
| "required": true, | ||
| "type": "integer", | ||
| "enum": [1, 2, 3, 4, 5] | ||
| } | ||
| ], | ||
| "requestBody": { | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "someTypeId": { | ||
| "type": "integer", | ||
| "enum": [1, 2, 3, 4, 5] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "responses": {} | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wraps every enum value in quotes, turning numeric values into strings. You should render numeric literals without quotes (e.g., check typeof value or use separate formatting for numbers vs. strings) to preserve original types.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Copilot's catch here is worth looking at. Stringly typed enums might be a regression in some use cases.
Looking at the test cases you added though, seems to be working as intended? 🤔