diff --git a/package.json b/package.json index 8e92529a..aea54f86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.7.2", + "version": "0.8.0", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", diff --git a/samples/spec/v2.json b/samples/spec/v2.json index 0869e8d2..46b4183a 100644 --- a/samples/spec/v2.json +++ b/samples/spec/v2.json @@ -978,6 +978,13 @@ "Order": { "type": "object", "properties": { + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] + }, "id": { "type": "integer", "format": "int64" diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index be285179..650228bf 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -18,7 +18,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { }; } return { - name: value + name: String(value) .replace(/\W+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 4c34ecb4..d5cc866d 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -53,7 +53,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti return model; } - if (definition.enum) { + if (definition.enum && definition.type !== 'boolean') { const enumerators = getEnum(definition.enum); const extendedEnumerators = extendEnum(enumerators, definition); if (extendedEnumerators.length) { diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index be285179..650228bf 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -18,7 +18,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { }; } return { - name: value + name: String(value) .replace(/\W+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 4a563389..7be4180f 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -55,7 +55,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti return model; } - if (definition.enum) { + if (definition.enum && definition.type !== 'boolean') { const enumerators = getEnum(definition.enum); const extendedEnumerators = extendEnum(enumerators, definition); if (extendedEnumerators.length) { diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index d3c6ad3a..fa3d295b 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -381,6 +381,7 @@ export { $SimpleReference } from './schemas/$SimpleReference'; export { $SimpleString } from './schemas/$SimpleString'; export { $SimpleStringWithPattern } from './schemas/$SimpleStringWithPattern'; +export { CollectionFormatService } from './services/CollectionFormatService'; export { ComplexService } from './services/ComplexService'; export { DefaultsService } from './services/DefaultsService'; export { DuplicateService } from './services/DuplicateService'; @@ -756,6 +757,10 @@ export type ModelWithEnum = { * These are the HTTP error code enums */ statusCode?: ModelWithEnum.statusCode; + /** + * Simple boolean enum + */ + bool?: boolean; } export namespace ModelWithEnum { @@ -1422,6 +1427,9 @@ export const $ModelWithEnum = { statusCode: { type: 'Enum', }, + bool: { + type: 'boolean', + }, }, };" `; @@ -1722,6 +1730,47 @@ export const $SimpleStringWithPattern = { };" `; +exports[`v2 should generate: ./test/generated/v2/services/CollectionFormatService.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import { request as __request } from '../core/request'; +import { OpenAPI } from '../core/OpenAPI'; + +export class CollectionFormatService { + + /** + * @param parameterArrayCsv This is an array parameter that is send as csv format (comma-separated values) + * @param parameterArraySsv This is an array parameter that is send as ssv format (space-separated values) + * @param parameterArrayTsv This is an array parameter that is send as tsv format (tab-separated values) + * @param parameterArrayMulti This is an array parameter that is send as pipes format (pipe-separated values) + * @param parameterArrayMulti This is an array parameter that is send as multi format (multiple parameter instances) + * @throws ApiError + */ + public static async collectionFormat( + parameterArrayCsv: Array, + parameterArraySsv: Array, + parameterArrayTsv: Array, + parameterArrayMulti: Array, + parameterArrayMulti: Array, + ): Promise { + const result = await __request({ + method: 'GET', + path: \`/api/v\${OpenAPI.VERSION}/collectionFormat\`, + query: { + 'parameterArrayCSV': parameterArrayCsv, + 'parameterArraySSV': parameterArraySsv, + 'parameterArrayTSV': parameterArrayTsv, + 'parameterArrayMulti': parameterArrayMulti, + 'parameterArrayMulti': parameterArrayMulti, + }, + }); + return result.body; + } + +}" +`; + exports[`v2 should generate: ./test/generated/v2/services/ComplexService.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -2639,6 +2688,7 @@ export { $SimpleReference } from './schemas/$SimpleReference'; export { $SimpleString } from './schemas/$SimpleString'; export { $SimpleStringWithPattern } from './schemas/$SimpleStringWithPattern'; +export { CollectionFormatService } from './services/CollectionFormatService'; export { ComplexService } from './services/ComplexService'; export { DefaultsService } from './services/DefaultsService'; export { DuplicateService } from './services/DuplicateService'; @@ -3136,6 +3186,10 @@ export type ModelWithEnum = { * These are the HTTP error code enums */ statusCode?: ModelWithEnum.statusCode; + /** + * Simple boolean enum + */ + bool?: boolean; } export namespace ModelWithEnum { @@ -3948,6 +4002,9 @@ export const $ModelWithEnum = { statusCode: { type: 'Enum', }, + bool: { + type: 'boolean', + }, }, };" `; @@ -4238,6 +4295,47 @@ export const $SimpleStringWithPattern = { };" `; +exports[`v3 should generate: ./test/generated/v3/services/CollectionFormatService.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import { request as __request } from '../core/request'; +import { OpenAPI } from '../core/OpenAPI'; + +export class CollectionFormatService { + + /** + * @param parameterArrayCsv This is an array parameter that is send as csv format (comma-separated values) + * @param parameterArraySsv This is an array parameter that is send as ssv format (space-separated values) + * @param parameterArrayTsv This is an array parameter that is send as tsv format (tab-separated values) + * @param parameterArrayMulti This is an array parameter that is send as pipes format (pipe-separated values) + * @param parameterArrayMulti This is an array parameter that is send as multi format (multiple parameter instances) + * @throws ApiError + */ + public static async collectionFormat( + parameterArrayCsv: Array | null, + parameterArraySsv: Array | null, + parameterArrayTsv: Array | null, + parameterArrayMulti: Array | null, + parameterArrayMulti: Array | null, + ): Promise { + const result = await __request({ + method: 'GET', + path: \`/api/v\${OpenAPI.VERSION}/collectionFormat\`, + query: { + 'parameterArrayCSV': parameterArrayCsv, + 'parameterArraySSV': parameterArraySsv, + 'parameterArrayTSV': parameterArrayTsv, + 'parameterArrayMulti': parameterArrayMulti, + 'parameterArrayMulti': parameterArrayMulti, + }, + }); + return result.body; + } + +}" +`; + exports[`v3 should generate: ./test/generated/v3/services/ComplexService.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ diff --git a/test/spec/v2.json b/test/spec/v2.json index dc5f53dd..35dba559 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -490,6 +490,71 @@ } } }, + "/api/v{api-version}/collectionFormat": { + "get": { + "tags": [ + "CollectionFormat" + ], + "operationId": "CollectionFormat", + "parameters": [ + { + "description": "This is an array parameter that is send as csv format (comma-separated values)", + "name": "parameterArrayCSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv" + }, + { + "description": "This is an array parameter that is send as ssv format (space-separated values)", + "name": "parameterArraySSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "ssv" + }, + { + "description": "This is an array parameter that is send as tsv format (tab-separated values)", + "name": "parameterArrayTSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "tsv" + }, + { + "description": "This is an array parameter that is send as pipes format (pipe-separated values)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "pipes" + }, + { + "description": "This is an array parameter that is send as multi format (multiple parameter instances)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "multi" + } + ] + } + }, "/api/v{api-version}/types": { "get": { "tags": [ @@ -932,6 +997,13 @@ "500 foo.bar", "600 foo&bar" ] + }, + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] } } }, diff --git a/test/spec/v3.json b/test/spec/v3.json index fb605ac3..bc2b1e41 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -712,6 +712,86 @@ } } }, + "/api/v{api-version}/collectionFormat": { + "get": { + "tags": [ + "CollectionFormat" + ], + "operationId": "CollectionFormat", + "parameters": [ + { + "description": "This is an array parameter that is send as csv format (comma-separated values)", + "name": "parameterArrayCSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "csv" + }, + { + "description": "This is an array parameter that is send as ssv format (space-separated values)", + "name": "parameterArraySSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "ssv" + }, + { + "description": "This is an array parameter that is send as tsv format (tab-separated values)", + "name": "parameterArrayTSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "tsv" + }, + { + "description": "This is an array parameter that is send as pipes format (pipe-separated values)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "pipes" + }, + { + "description": "This is an array parameter that is send as multi format (multiple parameter instances)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "multi" + } + ] + } + }, "/api/v{api-version}/types": { "get": { "tags": [ @@ -1390,6 +1470,13 @@ "500 foo.bar", "600 foo&bar" ] + }, + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] } } },