From 1870ccdc1f63f5843d2323c04a7d75ad1e587c74 Mon Sep 17 00:00:00 2001 From: Julien ROUSSEAU Date: Mon, 7 Apr 2025 15:49:12 +0200 Subject: [PATCH 1/2] fix: Ensure enums are at the top of the components to avoid issue on recursive schema parsing --- .changeset/mean-ghosts-post.md | 5 + src/code-gen-process.ts | 2 + src/schema-components-map.ts | 9 + .../__snapshots__/basic.test.ts.snap | 24 +- .../__snapshots__/basic.test.ts.snap | 64 ++-- .../__snapshots__/basic.test.ts.snap | 318 ++++++++++++++++++ .../enumNotFirstInComponents/basic.test.ts | 35 ++ .../spec/enumNotFirstInComponents/schema.json | 83 +++++ .../__snapshots__/basic.test.ts.snap | 90 ++--- .../__snapshots__/basic.test.ts.snap | 42 +-- .../__snapshots__/basic.test.ts.snap | 42 +-- .../__snapshots__/basic.test.ts.snap | 42 +-- .../__snapshots__/basic.test.ts.snap | 26 +- .../__snapshots__/basic.test.ts.snap | 26 +- .../__snapshots__/basic.test.ts.snap | 12 +- 15 files changed, 636 insertions(+), 184 deletions(-) create mode 100644 .changeset/mean-ghosts-post.md create mode 100644 tests/spec/enumNotFirstInComponents/__snapshots__/basic.test.ts.snap create mode 100644 tests/spec/enumNotFirstInComponents/basic.test.ts create mode 100644 tests/spec/enumNotFirstInComponents/schema.json diff --git a/.changeset/mean-ghosts-post.md b/.changeset/mean-ghosts-post.md new file mode 100644 index 00000000..795a901c --- /dev/null +++ b/.changeset/mean-ghosts-post.md @@ -0,0 +1,5 @@ +--- +"swagger-typescript-api": patch +--- + +Ensure enums are at the top of the components to avoid issue on recursive schema parsing diff --git a/src/code-gen-process.ts b/src/code-gen-process.ts index 8f22e9fa..320273f8 100644 --- a/src/code-gen-process.ts +++ b/src/code-gen-process.ts @@ -127,6 +127,8 @@ export class CodeGenProcess { }), ); + this.schemaComponentsMap.enumsFirst(); + const componentsToParse: SchemaComponent[] = this.schemaComponentsMap.filter( lodash.compact([ diff --git a/src/schema-components-map.ts b/src/schema-components-map.ts index 308a14e9..6ddc4dcc 100644 --- a/src/schema-components-map.ts +++ b/src/schema-components-map.ts @@ -68,4 +68,13 @@ export class SchemaComponentsMap { get($ref: string) { return this._data.find((c) => c.$ref === $ref) || null; } + + // Ensure enums are at the top of components list + enumsFirst() { + this._data.sort((a, b) => { + if (Object.keys(a.rawTypeData || {}).includes("enum")) return -1; + if (Object.keys(b.rawTypeData || {}).includes("enum")) return 1; + return 0; + }); + } } diff --git a/tests/spec/discriminator/__snapshots__/basic.test.ts.snap b/tests/spec/discriminator/__snapshots__/basic.test.ts.snap index 18a9f445..252d9436 100644 --- a/tests/spec/discriminator/__snapshots__/basic.test.ts.snap +++ b/tests/spec/discriminator/__snapshots__/basic.test.ts.snap @@ -13,6 +13,18 @@ exports[`basic > discriminator 1`] = ` * --------------------------------------------------------------- */ +export enum PetEnum { + Dog = "dog", + Lizard = "lizard", + Cat = "cat", +} + +export enum BlockDTOEnum { + Csv = "csv", + File = "file", + Kek = "kek", +} + export type SimpleDiscriminator = SimpleObject | ComplexObject; export interface SimpleObject { @@ -23,12 +35,6 @@ export interface ComplexObject { objectType: string; } -export enum BlockDTOEnum { - Csv = "csv", - File = "file", - Kek = "kek", -} - export type BlockDTOWithEnum = BaseBlockDtoWithEnum & ( | BaseBlockDtoWithEnumTypeMapping @@ -93,12 +99,6 @@ export type Lizard = BasePet & { lovesRocks?: boolean; }; -export enum PetEnum { - Dog = "dog", - Lizard = "lizard", - Cat = "cat", -} - export type PetWithEnum = BasePetWithEnum & ( | BasePetWithEnumPetTypeMapping diff --git a/tests/spec/enumNamesAsValues/__snapshots__/basic.test.ts.snap b/tests/spec/enumNamesAsValues/__snapshots__/basic.test.ts.snap index 12814a14..578ced2f 100644 --- a/tests/spec/enumNamesAsValues/__snapshots__/basic.test.ts.snap +++ b/tests/spec/enumNamesAsValues/__snapshots__/basic.test.ts.snap @@ -13,21 +13,26 @@ exports[`basic > --enum-names-as-values 1`] = ` * --------------------------------------------------------------- */ -export type TestAllOfDc = (FooBarBaz & FooBar) & { - prop?: string; -}; - -export type TestAllOfDc2 = FooBarBaz & { - prop?: string; -}; +export enum JobKind { + COMPANY = "COMPANY", + PERSONAL = "PERSONAL", + FREELANCE = "FREELANCE", + OPEN_SOURCE = "OPEN_SOURCE", +} -export type TestAnyOfDc = (FooBarBaz | FooBar) & { - prop?: string; -}; +export enum PetIdsWithWrongEnum { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} -export type TestOneOfDc = (FooBarBaz | FooBar) & { - prop?: string; -}; +export enum PetIds { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} /** * FooBar @@ -46,19 +51,21 @@ export enum IntEnumWithNames { BooFar = "BooFar", } -export enum PetIds { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} +export type TestAllOfDc = (FooBarBaz & FooBar) & { + prop?: string; +}; -export enum PetIdsWithWrongEnum { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} +export type TestAllOfDc2 = FooBarBaz & { + prop?: string; +}; + +export type TestAnyOfDc = (FooBarBaz | FooBar) & { + prop?: string; +}; + +export type TestOneOfDc = (FooBarBaz | FooBar) & { + prop?: string; +}; /** Information about job */ export interface FooBarBaz { @@ -91,13 +98,6 @@ export type OmitIdUserType = OmitUserTypeIdOrId; export type AuthUserType = OmitIdUserType; -export enum JobKind { - COMPANY = "COMPANY", - PERSONAL = "PERSONAL", - FREELANCE = "FREELANCE", - OPEN_SOURCE = "OPEN_SOURCE", -} - /** Information about job */ export interface JobType { id: string; diff --git a/tests/spec/enumNotFirstInComponents/__snapshots__/basic.test.ts.snap b/tests/spec/enumNotFirstInComponents/__snapshots__/basic.test.ts.snap new file mode 100644 index 00000000..3a69d716 --- /dev/null +++ b/tests/spec/enumNotFirstInComponents/__snapshots__/basic.test.ts.snap @@ -0,0 +1,318 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`basic > enum not first in components 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 ## + * --------------------------------------------------------------- + */ + +export enum ExampleEnum { + Example1 = "Example1", + Example2 = "Example2", +} + +export interface DtoExample1 { + name: string; + age: number; + toto?: DtoExample2; +} + +export interface DtoExample2 { + title: string; + description: string; +} + +export type ExampleObject = BaseExampleObject & + ( + | BaseExampleObjectTypeMapping + | BaseExampleObjectTypeMapping + ); + +interface BaseExampleObject { + type?: ExampleEnum; +} + +type BaseExampleObjectTypeMapping = { + type: Key; +} & Type; + +export type QueryParamsType = Record; +export type ResponseFormat = keyof Omit; + +export interface FullRequestParams extends Omit { + /** set parameter to \`true\` for call \`securityWorker\` for this request */ + secure?: boolean; + /** request path */ + path: string; + /** content type of request body */ + type?: ContentType; + /** query params */ + query?: QueryParamsType; + /** format of response (i.e. response.json() -> format: "json") */ + format?: ResponseFormat; + /** request body */ + body?: unknown; + /** base url */ + baseUrl?: string; + /** request cancellation token */ + cancelToken?: CancelToken; +} + +export type RequestParams = Omit< + FullRequestParams, + "body" | "method" | "query" | "path" +>; + +export interface ApiConfig { + baseUrl?: string; + baseApiParams?: Omit; + securityWorker?: ( + securityData: SecurityDataType | null, + ) => Promise | RequestParams | void; + customFetch?: typeof fetch; +} + +export interface HttpResponse + extends Response { + data: D; + error: E; +} + +type CancelToken = Symbol | string | number; + +export enum ContentType { + Json = "application/json", + FormData = "multipart/form-data", + UrlEncoded = "application/x-www-form-urlencoded", + Text = "text/plain", +} + +export class HttpClient { + public baseUrl: string = "http://localhost:8080/api/v1"; + private securityData: SecurityDataType | null = null; + private securityWorker?: ApiConfig["securityWorker"]; + private abortControllers = new Map(); + private customFetch = (...fetchParams: Parameters) => + fetch(...fetchParams); + + private baseApiParams: RequestParams = { + credentials: "same-origin", + headers: {}, + redirect: "follow", + referrerPolicy: "no-referrer", + }; + + constructor(apiConfig: ApiConfig = {}) { + Object.assign(this, apiConfig); + } + + public setSecurityData = (data: SecurityDataType | null) => { + this.securityData = data; + }; + + protected encodeQueryParam(key: string, value: any) { + const encodedKey = encodeURIComponent(key); + return \`\${encodedKey}=\${encodeURIComponent(typeof value === "number" ? value : \`\${value}\`)}\`; + } + + protected addQueryParam(query: QueryParamsType, key: string) { + return this.encodeQueryParam(key, query[key]); + } + + protected addArrayQueryParam(query: QueryParamsType, key: string) { + const value = query[key]; + return value.map((v: any) => this.encodeQueryParam(key, v)).join("&"); + } + + protected toQueryString(rawQuery?: QueryParamsType): string { + const query = rawQuery || {}; + const keys = Object.keys(query).filter( + (key) => "undefined" !== typeof query[key], + ); + return keys + .map((key) => + Array.isArray(query[key]) + ? this.addArrayQueryParam(query, key) + : this.addQueryParam(query, key), + ) + .join("&"); + } + + protected addQueryParams(rawQuery?: QueryParamsType): string { + const queryString = this.toQueryString(rawQuery); + return queryString ? \`?\${queryString}\` : ""; + } + + private contentFormatters: Record any> = { + [ContentType.Json]: (input: any) => + input !== null && (typeof input === "object" || typeof input === "string") + ? JSON.stringify(input) + : input, + [ContentType.Text]: (input: any) => + input !== null && typeof input !== "string" + ? JSON.stringify(input) + : input, + [ContentType.FormData]: (input: any) => + Object.keys(input || {}).reduce((formData, key) => { + const property = input[key]; + formData.append( + key, + property instanceof Blob + ? property + : typeof property === "object" && property !== null + ? JSON.stringify(property) + : \`\${property}\`, + ); + return formData; + }, new FormData()), + [ContentType.UrlEncoded]: (input: any) => this.toQueryString(input), + }; + + protected mergeRequestParams( + params1: RequestParams, + params2?: RequestParams, + ): RequestParams { + return { + ...this.baseApiParams, + ...params1, + ...(params2 || {}), + headers: { + ...(this.baseApiParams.headers || {}), + ...(params1.headers || {}), + ...((params2 && params2.headers) || {}), + }, + }; + } + + protected createAbortSignal = ( + cancelToken: CancelToken, + ): AbortSignal | undefined => { + if (this.abortControllers.has(cancelToken)) { + const abortController = this.abortControllers.get(cancelToken); + if (abortController) { + return abortController.signal; + } + return void 0; + } + + const abortController = new AbortController(); + this.abortControllers.set(cancelToken, abortController); + return abortController.signal; + }; + + public abortRequest = (cancelToken: CancelToken) => { + const abortController = this.abortControllers.get(cancelToken); + + if (abortController) { + abortController.abort(); + this.abortControllers.delete(cancelToken); + } + }; + + public request = async ({ + body, + secure, + path, + type, + query, + format, + baseUrl, + cancelToken, + ...params + }: FullRequestParams): Promise> => { + const secureParams = + ((typeof secure === "boolean" ? secure : this.baseApiParams.secure) && + this.securityWorker && + (await this.securityWorker(this.securityData))) || + {}; + const requestParams = this.mergeRequestParams(params, secureParams); + const queryString = query && this.toQueryString(query); + const payloadFormatter = this.contentFormatters[type || ContentType.Json]; + const responseFormat = format || requestParams.format; + + return this.customFetch( + \`\${baseUrl || this.baseUrl || ""}\${path}\${queryString ? \`?\${queryString}\` : ""}\`, + { + ...requestParams, + headers: { + ...(requestParams.headers || {}), + ...(type && type !== ContentType.FormData + ? { "Content-Type": type } + : {}), + }, + signal: + (cancelToken + ? this.createAbortSignal(cancelToken) + : requestParams.signal) || null, + body: + typeof body === "undefined" || body === null + ? null + : payloadFormatter(body), + }, + ).then(async (response) => { + const r = response.clone() as HttpResponse; + r.data = null as unknown as T; + r.error = null as unknown as E; + + const data = !responseFormat + ? r + : await response[responseFormat]() + .then((data) => { + if (r.ok) { + r.data = data; + } else { + r.error = data; + } + return r; + }) + .catch((e) => { + r.error = e; + return r; + }); + + if (cancelToken) { + this.abortControllers.delete(cancelToken); + } + + if (!response.ok) throw data; + return data; + }); + }; +} + +/** + * @title example-service + * @version 0.0.1 + * @baseUrl http://localhost:8080/api/v1 + * + * API definition for example service + */ +export class Api< + SecurityDataType extends unknown, +> extends HttpClient { + api = { + /** + * No description + * + * @tags test-api + * @name GetExample + * @request GET:/api/example + */ + getExample: (params: RequestParams = {}) => + this.request({ + path: \`/api/example\`, + method: "GET", + format: "json", + ...params, + }), + }; +} +" +`; diff --git a/tests/spec/enumNotFirstInComponents/basic.test.ts b/tests/spec/enumNotFirstInComponents/basic.test.ts new file mode 100644 index 00000000..7dc10588 --- /dev/null +++ b/tests/spec/enumNotFirstInComponents/basic.test.ts @@ -0,0 +1,35 @@ +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("enum not first in components", async () => { + await generateApi({ + fileName: "schema", + input: path.resolve(import.meta.dirname, "schema.json"), + output: tmpdir, + silent: true, + enumNamesAsValues: true, + }); + + const content = await fs.readFile(path.join(tmpdir, "schema.ts"), { + encoding: "utf8", + }); + + expect(content).toMatchSnapshot(); + }); +}); diff --git a/tests/spec/enumNotFirstInComponents/schema.json b/tests/spec/enumNotFirstInComponents/schema.json new file mode 100644 index 00000000..b2234171 --- /dev/null +++ b/tests/spec/enumNotFirstInComponents/schema.json @@ -0,0 +1,83 @@ +{ + "openapi": "3.0.1", + "info": { + "description": "API definition for example service", + "version": "0.0.1", + "title": "example-service" + }, + "paths": { + "/api/example": { + "get": { + "operationId": "getExample", + "tags": ["test-api"], + "responses": { + "200": { + "description": "Return example", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ExampleObject" + } + } + } + } + } + } + } + }, + "components": { + "examples": {}, + "headers": {}, + "parameters": {}, + "requestBodies": {}, + "responses": {}, + "schemas": { + "DtoExample1": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "age": { + "type": "integer" + }, + "toto": { + "$ref": "#/components/schemas/DtoExample2" + } + }, + "required": ["name", "age"] + }, + "DtoExample2": { + "type": "object", + "properties": { + "title": { + "type": "string" + }, + "description": { + "type": "string" + } + }, + "required": ["title", "description"] + }, + "ExampleObject": { + "properties": { + "type": { + "$ref": "#/components/schemas/ExampleEnum" + } + }, + "discriminator": { + "propertyName": "type", + "mapping": { + "Example1": "#/components/schemas/DtoExample1", + "Example2": "#/components/schemas/DtoExample2" + } + } + }, + "ExampleEnum": { + "type": "string", + "enum": ["Example1", "Example2"] + } + } + }, + "servers": [{ "url": "http://localhost:8080/api/v1" }] +} diff --git a/tests/spec/enums-2.0/__snapshots__/basic.test.ts.snap b/tests/spec/enums-2.0/__snapshots__/basic.test.ts.snap index d3247d69..f0b9d9f1 100644 --- a/tests/spec/enums-2.0/__snapshots__/basic.test.ts.snap +++ b/tests/spec/enums-2.0/__snapshots__/basic.test.ts.snap @@ -13,12 +13,47 @@ exports[`basic > enums-2.0 1`] = ` * --------------------------------------------------------------- */ -export interface ObjWithEnum { - "prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null; - "prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5; +/** @format int32 */ +export enum 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", } -export enum XNullableEnum { +/** @format int32 */ +export enum EnumWithMoreNames { + Bla = 1, + Blabla = "Blabla", + Boiler = "Boiler", +} + +export enum StringCompleteEnums { + Bla = "foo", + Blabla = "bar", + Boiler = "baz", +} + +export enum StringEnums { + Bla = "foo", + Blabla = "bar", + Boiler = "Boiler", +} + +export enum SimpleEnumNonNullable { Value0 = 0, Value1 = 1, Value2 = 2, @@ -27,7 +62,7 @@ export enum XNullableEnum { Value5 = 5, } -export enum SimpleEnumNonNullable { +export enum XNullableEnum { Value0 = 0, Value1 = 1, Value2 = 2, @@ -36,6 +71,11 @@ export enum SimpleEnumNonNullable { Value5 = 5, } +export interface ObjWithEnum { + "prop-enum-nullable"?: 0 | 1 | 2 | 3 | 4 | 5 | null; + "prop-enum"?: 0 | 1 | 2 | 3 | 4 | 5; +} + export enum OnlyEnumNames { Bla = "Bla", Blabla = "Blabla", @@ -48,18 +88,6 @@ export enum StringOnlyEnumNames { Boiler = "Boiler", } -export enum StringEnums { - Bla = "foo", - Blabla = "bar", - Boiler = "Boiler", -} - -export enum StringCompleteEnums { - Bla = "foo", - Blabla = "bar", - Boiler = "baz", -} - /** @format int32 */ export enum EmptyEnum { Bla = "Bla", @@ -67,34 +95,6 @@ export enum EmptyEnum { Boiler = "Boiler", } -/** @format int32 */ -export enum EnumWithMoreNames { - Bla = 1, - Blabla = "Blabla", - Boiler = "Boiler", -} - -/** @format int32 */ -export enum 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", -} - export interface PostFooPayload { someTypeId?: 1 | 2 | 3 | 4 | 5; } diff --git a/tests/spec/extractRequestBody/__snapshots__/basic.test.ts.snap b/tests/spec/extractRequestBody/__snapshots__/basic.test.ts.snap index 56d240df..a0176cfe 100644 --- a/tests/spec/extractRequestBody/__snapshots__/basic.test.ts.snap +++ b/tests/spec/extractRequestBody/__snapshots__/basic.test.ts.snap @@ -13,6 +13,27 @@ exports[`basic > --extract-request-body 1`] = ` * --------------------------------------------------------------- */ +export enum PetIdsWithWrongEnumTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetIdsTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetNamesTTT { + FluffyHero = "Fluffy Hero", + PiggyPo = "Piggy Po", + SwaggerTypescriptApi = "Swagger Typescript Api", + UPPER_CASE = "UPPER_CASE", +} + /** * Pet Order * An order for a pets from the pet store @@ -76,27 +97,6 @@ export interface TagTTT { name?: string; } -export enum PetNamesTTT { - FluffyHero = "Fluffy Hero", - PiggyPo = "Piggy Po", - SwaggerTypescriptApi = "Swagger Typescript Api", - UPPER_CASE = "UPPER_CASE", -} - -export enum PetIdsTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - -export enum PetIdsWithWrongEnumTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - /** * a Pet * A pet for sale in the pet store diff --git a/tests/spec/extractResponseBody/__snapshots__/basic.test.ts.snap b/tests/spec/extractResponseBody/__snapshots__/basic.test.ts.snap index 5e5b57b4..26afc69d 100644 --- a/tests/spec/extractResponseBody/__snapshots__/basic.test.ts.snap +++ b/tests/spec/extractResponseBody/__snapshots__/basic.test.ts.snap @@ -13,6 +13,27 @@ exports[`basic > --extract-response-body 1`] = ` * --------------------------------------------------------------- */ +export enum PetIdsWithWrongEnumTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetIdsTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetNamesTTT { + FluffyHero = "Fluffy Hero", + PiggyPo = "Piggy Po", + SwaggerTypescriptApi = "Swagger Typescript Api", + UPPER_CASE = "UPPER_CASE", +} + /** * Pet Order * An order for a pets from the pet store @@ -76,27 +97,6 @@ export interface TagTTT { name?: string; } -export enum PetNamesTTT { - FluffyHero = "Fluffy Hero", - PiggyPo = "Piggy Po", - SwaggerTypescriptApi = "Swagger Typescript Api", - UPPER_CASE = "UPPER_CASE", -} - -export enum PetIdsTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - -export enum PetIdsWithWrongEnumTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - /** * a Pet * A pet for sale in the pet store diff --git a/tests/spec/extractResponseError/__snapshots__/basic.test.ts.snap b/tests/spec/extractResponseError/__snapshots__/basic.test.ts.snap index ecf1592f..c62ce5e2 100644 --- a/tests/spec/extractResponseError/__snapshots__/basic.test.ts.snap +++ b/tests/spec/extractResponseError/__snapshots__/basic.test.ts.snap @@ -13,6 +13,27 @@ exports[`basic > --extract-response-body 1`] = ` * --------------------------------------------------------------- */ +export enum PetIdsWithWrongEnumTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetIdsTTT { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetNamesTTT { + FluffyHero = "Fluffy Hero", + PiggyPo = "Piggy Po", + SwaggerTypescriptApi = "Swagger Typescript Api", + UPPER_CASE = "UPPER_CASE", +} + /** * Pet Order * An order for a pets from the pet store @@ -76,27 +97,6 @@ export interface TagTTT { name?: string; } -export enum PetNamesTTT { - FluffyHero = "Fluffy Hero", - PiggyPo = "Piggy Po", - SwaggerTypescriptApi = "Swagger Typescript Api", - UPPER_CASE = "UPPER_CASE", -} - -export enum PetIdsTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - -export enum PetIdsWithWrongEnumTTT { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - /** * a Pet * A pet for sale in the pet store diff --git a/tests/spec/moduleNameFirstTag/__snapshots__/basic.test.ts.snap b/tests/spec/moduleNameFirstTag/__snapshots__/basic.test.ts.snap index a327a70d..ea9abdf1 100644 --- a/tests/spec/moduleNameFirstTag/__snapshots__/basic.test.ts.snap +++ b/tests/spec/moduleNameFirstTag/__snapshots__/basic.test.ts.snap @@ -13,6 +13,19 @@ exports[`basic > --module-name-first-tag 1`] = ` * --------------------------------------------------------------- */ +export enum PetIds { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetNames { + FluffyHero = "Fluffy Hero", + PiggyPo = "Piggy Po", + SwaggerTypescriptApi = "Swagger Typescript Api", +} + /** * Pet Order * An order for a pets from the pet store @@ -76,19 +89,6 @@ export interface Tag { name?: string; } -export enum PetNames { - FluffyHero = "Fluffy Hero", - PiggyPo = "Piggy Po", - SwaggerTypescriptApi = "Swagger Typescript Api", -} - -export enum PetIds { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - /** * a Pet * A pet for sale in the pet store diff --git a/tests/spec/moduleNameIndex/__snapshots__/basic.test.ts.snap b/tests/spec/moduleNameIndex/__snapshots__/basic.test.ts.snap index e8973292..d21a2565 100644 --- a/tests/spec/moduleNameIndex/__snapshots__/basic.test.ts.snap +++ b/tests/spec/moduleNameIndex/__snapshots__/basic.test.ts.snap @@ -13,6 +13,19 @@ exports[`basic > --module-name-index 1`] = ` * --------------------------------------------------------------- */ +export enum PetIds { + Value10 = 10, + Value20 = 20, + Value30 = 30, + Value40 = 40, +} + +export enum PetNames { + FluffyHero = "Fluffy Hero", + PiggyPo = "Piggy Po", + SwaggerTypescriptApi = "Swagger Typescript Api", +} + /** * Pet Order * An order for a pets from the pet store @@ -76,19 +89,6 @@ export interface Tag { name?: string; } -export enum PetNames { - FluffyHero = "Fluffy Hero", - PiggyPo = "Piggy Po", - SwaggerTypescriptApi = "Swagger Typescript Api", -} - -export enum PetIds { - Value10 = 10, - Value20 = 20, - Value30 = 30, - Value40 = 40, -} - /** * a Pet * A pet for sale in the pet store diff --git a/tests/spec/unionEnums/__snapshots__/basic.test.ts.snap b/tests/spec/unionEnums/__snapshots__/basic.test.ts.snap index 1c1b52d6..d3f16495 100644 --- a/tests/spec/unionEnums/__snapshots__/basic.test.ts.snap +++ b/tests/spec/unionEnums/__snapshots__/basic.test.ts.snap @@ -13,18 +13,18 @@ exports[`basic > --generate-union-enums 1`] = ` * --------------------------------------------------------------- */ -export type StringEnum = "String1" | "String2" | "String3" | "String4"; - -export type NumberEnum = 1 | 2 | 3 | 4; - -export type BooleanEnum = true | false; - /** * FooBar * @format int32 */ export type IntEnumWithNames = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; +export type BooleanEnum = true | false; + +export type NumberEnum = 1 | 2 | 3 | 4; + +export type StringEnum = "String1" | "String2" | "String3" | "String4"; + export type QueryParamsType = Record; export type ResponseFormat = keyof Omit; From bf95ad834874bb279a881f933bdd31386326e67b Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Mon, 14 Apr 2025 11:09:40 +0900 Subject: [PATCH 2/2] _ Signed-off-by: Sora Morimoto --- .changeset/mean-ghosts-post.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/mean-ghosts-post.md b/.changeset/mean-ghosts-post.md index 795a901c..b0e8d59d 100644 --- a/.changeset/mean-ghosts-post.md +++ b/.changeset/mean-ghosts-post.md @@ -2,4 +2,4 @@ "swagger-typescript-api": patch --- -Ensure enums are at the top of the components to avoid issue on recursive schema parsing +Ensure enums are at the top of the components to avoid issue on recursive schema parsing.