diff --git a/package.json b/package.json index 23e7450f..ab99e1e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.1.10", + "version": "0.1.11", "description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", diff --git a/src/openApi/v2/parser/getEnumValues.ts b/src/openApi/v2/parser/getEnumValues.ts deleted file mode 100644 index 348208fe..00000000 --- a/src/openApi/v2/parser/getEnumValues.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Enum } from '../../../client/interfaces/Enum'; - -export function getEnumValues(enumerators: Enum[]): string[] { - // Fetch values from the symbols, just to be sure we filter out - // any double values and finally we sort them to make them easier - // to read when we use them in our generated code. - return enumerators - .map(enumerator => enumerator.value) - .filter((enumerator, index, arr) => { - return arr.indexOf(enumerator) === index; - }) - .sort(); -} diff --git a/src/utils/exportModel.ts b/src/utils/exportModel.ts index 8c49c12b..d4fb9718 100644 --- a/src/utils/exportModel.ts +++ b/src/utils/exportModel.ts @@ -15,28 +15,11 @@ export function exportModel(model: Model): Model { const nameB = b.toLowerCase(); return nameA.localeCompare(nameB); }), - properties: model.properties.sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB); + enums: model.enums.filter((property, index, arr) => { + return arr.findIndex(item => item.name === property.name) === index; + }), + enum: model.enum.filter((enumerator, index, arr) => { + return arr.findIndex(item => item.name === enumerator.name) === index; }), - enums: model.enums - .filter((property, index, arr) => { - return arr.findIndex(item => item.name === property.name) === index; - }) - .sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB); - }), - enum: model.enum - .filter((enumerator, index, arr) => { - return arr.findIndex(item => item.name === enumerator.name) === index; - }) - .sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB); - }), }; } diff --git a/src/utils/exportService.ts b/src/utils/exportService.ts index c51f5061..60c70d91 100644 --- a/src/utils/exportService.ts +++ b/src/utils/exportService.ts @@ -16,20 +16,14 @@ export function exportService(service: Service): Service { const nameB = b.toLowerCase(); return nameA.localeCompare(nameB); }), - operations: service.operations - .map(operation => { - const name = operation.name; - const index = names.get(name) || 0; - if (index > 0) { - operation.name = `${name}${index}`; - } - names.set(name, index + 1); - return operation; - }) - .sort((a, b) => { - const nameA = a.name.toLowerCase(); - const nameB = b.name.toLowerCase(); - return nameA.localeCompare(nameB); - }), + operations: service.operations.map(operation => { + const name = operation.name; + const index = names.get(name) || 0; + if (index > 0) { + operation.name = `${name}${index}`; + } + names.set(name, index + 1); + return operation; + }), }; } diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index 70eed81c..a058c5ce 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -441,6 +441,7 @@ export { ModelWithInteger } from './models/ModelWithInteger'; export { ModelWithLink } from './models/ModelWithLink'; export { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; export { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; +export { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; export { ModelWithProperties } from './models/ModelWithProperties'; export { ModelWithReference } from './models/ModelWithReference'; export { ModelWithString } from './models/ModelWithString'; @@ -479,6 +480,7 @@ export { $ModelWithInteger } from './schemas/$ModelWithInteger'; export { $ModelWithLink } from './schemas/$ModelWithLink'; export { $ModelWithNestedEnums } from './schemas/$ModelWithNestedEnums'; export { $ModelWithNestedProperties } from './schemas/$ModelWithNestedProperties'; +export { $ModelWithOrderedProperties } from './schemas/$ModelWithOrderedProperties'; export { $ModelWithProperties } from './schemas/$ModelWithProperties'; export { $ModelWithReference } from './schemas/$ModelWithReference'; export { $ModelWithString } from './schemas/$ModelWithString'; @@ -678,9 +680,9 @@ exports[`generation v2 file(./test/result/v2/models/EnumFromDescription.ts): ./t * Success=1,Warning=2,Error=3 */ export enum EnumFromDescription { - ERROR = 3, SUCCESS = 1, WARNING = 2, + ERROR = 3, }" `; @@ -712,9 +714,9 @@ exports[`generation v2 file(./test/result/v2/models/EnumWithStrings.ts): ./test/ * This is a simple enum with strings */ export enum EnumWithStrings { - ERROR = 'Error', SUCCESS = 'Success', WARNING = 'Warning', + ERROR = 'Error', }" `; @@ -993,10 +995,10 @@ import { Dictionary } from './Dictionary'; * This is a model with nested enums */ export interface ModelWithNestedEnums { - arrayWithDescription?: Array<(1 | 2 | 3)>; - arrayWithEnum?: Array<('Success' | 'Warning' | 'Error')>; dictionaryWithEnum?: Dictionary<('Success' | 'Warning' | 'Error')>; dictionaryWithEnumFromDescription?: Dictionary<(1 | 2 | 3)>; + arrayWithEnum?: Array<('Success' | 'Warning' | 'Error')>; + arrayWithDescription?: Array<(1 | 2 | 3)>; } " `; @@ -1021,6 +1023,24 @@ export interface ModelWithNestedProperties { " `; +exports[`generation v2 file(./test/result/v2/models/ModelWithOrderedProperties.ts): ./test/result/v2/models/ModelWithOrderedProperties.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + + +/** + * This is a model with ordered properties + */ +export interface ModelWithOrderedProperties { + zebra?: string; + apple?: string; + hawaii?: string; +} +" +`; + exports[`generation v2 file(./test/result/v2/models/ModelWithProperties.ts): ./test/result/v2/models/ModelWithProperties.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1033,12 +1053,12 @@ import { ModelWithString } from './ModelWithString'; * This is a model with one nested property */ export interface ModelWithProperties { - boolean?: boolean; - number?: number; - reference?: ModelWithString; required: string; readonly requiredAndReadOnly: string; string?: string; + number?: number; + boolean?: boolean; + reference?: ModelWithString; } " `; @@ -1528,18 +1548,18 @@ exports[`generation v2 file(./test/result/v2/schemas/$ModelWithNestedEnums.ts): export const $ModelWithNestedEnums = { properties: { - arrayWithDescription: { - type: 'Array', - }, - arrayWithEnum: { - type: 'Array', - }, dictionaryWithEnum: { type: 'Dictionary', }, dictionaryWithEnumFromDescription: { type: 'Dictionary', }, + arrayWithEnum: { + type: 'Array', + }, + arrayWithDescription: { + type: 'Array', + }, }, };" `; @@ -1573,6 +1593,27 @@ export const $ModelWithNestedProperties = { };" `; +exports[`generation v2 file(./test/result/v2/schemas/$ModelWithOrderedProperties.ts): ./test/result/v2/schemas/$ModelWithOrderedProperties.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + +export const $ModelWithOrderedProperties = { + properties: { + zebra: { + type: 'string', + }, + apple: { + type: 'string', + }, + hawaii: { + type: 'string', + }, + }, +};" +`; + exports[`generation v2 file(./test/result/v2/schemas/$ModelWithProperties.ts): ./test/result/v2/schemas/$ModelWithProperties.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1581,15 +1622,6 @@ exports[`generation v2 file(./test/result/v2/schemas/$ModelWithProperties.ts): . export const $ModelWithProperties = { properties: { - boolean: { - type: 'boolean', - }, - number: { - type: 'number', - }, - reference: { - type: 'ModelWithString', - }, required: { type: 'string', isRequired: true, @@ -1602,6 +1634,15 @@ export const $ModelWithProperties = { string: { type: 'string', }, + number: { + type: 'number', + }, + boolean: { + type: 'boolean', + }, + reference: { + type: 'ModelWithString', + }, }, };" `; @@ -1860,6 +1901,22 @@ import { OpenAPI } from '../core/OpenAPI'; export class ResponseService { + /** + * @result ModelWithString Message for default response + * @throws ApiError + */ + public static async callWithResponse(): Promise { + + const result = await __request({ + method: 'get', + path: \`/api/v\${OpenAPI.VERSION}/response\`, + }); + + catchGenericError(result); + + return result.body; + } + /** * @result ModelWithString Message for default response * @throws ApiError @@ -1884,22 +1941,6 @@ export class ResponseService { return result.body; } - /** - * @result ModelWithString Message for default response - * @throws ApiError - */ - public static async callWithResponse(): Promise { - - const result = await __request({ - method: 'get', - path: \`/api/v\${OpenAPI.VERSION}/response\`, - }); - - catchGenericError(result); - - return result.body; - } - /** * @result ModelWithString Message for default response * @result ModelThatExtends Message for 201 response @@ -1941,21 +1982,6 @@ import { OpenAPI } from '../core/OpenAPI'; export class SimpleService { - /** - * @throws ApiError - */ - public static async deleteCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'delete', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - /** * @throws ApiError */ @@ -1974,40 +2000,10 @@ export class SimpleService { /** * @throws ApiError */ - public static async headCallWithoutParametersAndResponse(): Promise { + public static async putCallWithoutParametersAndResponse(): Promise { const result = await __request({ - method: 'head', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - - /** - * @throws ApiError - */ - public static async optionsCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'options', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - - /** - * @throws ApiError - */ - public static async patchCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'patch', + method: 'put', path: \`/api/v\${OpenAPI.VERSION}/simple\`, }); @@ -2034,10 +2030,55 @@ export class SimpleService { /** * @throws ApiError */ - public static async putCallWithoutParametersAndResponse(): Promise { + public static async deleteCallWithoutParametersAndResponse(): Promise { const result = await __request({ - method: 'put', + method: 'delete', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async optionsCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'options', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async headCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'head', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async patchCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'patch', path: \`/api/v\${OpenAPI.VERSION}/simple\`, }); @@ -2551,6 +2592,7 @@ export { ModelWithInteger } from './models/ModelWithInteger'; export { ModelWithLink } from './models/ModelWithLink'; export { ModelWithNestedEnums } from './models/ModelWithNestedEnums'; export { ModelWithNestedProperties } from './models/ModelWithNestedProperties'; +export { ModelWithOrderedProperties } from './models/ModelWithOrderedProperties'; export { ModelWithProperties } from './models/ModelWithProperties'; export { ModelWithReference } from './models/ModelWithReference'; export { ModelWithString } from './models/ModelWithString'; @@ -2589,6 +2631,7 @@ export { $ModelWithInteger } from './schemas/$ModelWithInteger'; export { $ModelWithLink } from './schemas/$ModelWithLink'; export { $ModelWithNestedEnums } from './schemas/$ModelWithNestedEnums'; export { $ModelWithNestedProperties } from './schemas/$ModelWithNestedProperties'; +export { $ModelWithOrderedProperties } from './schemas/$ModelWithOrderedProperties'; export { $ModelWithProperties } from './schemas/$ModelWithProperties'; export { $ModelWithReference } from './schemas/$ModelWithReference'; export { $ModelWithString } from './schemas/$ModelWithString'; @@ -2789,9 +2832,9 @@ exports[`generation v3 file(./test/result/v3/models/EnumFromDescription.ts): ./t * Success=1,Warning=2,Error=3 */ export enum EnumFromDescription { - ERROR = 3, SUCCESS = 1, WARNING = 2, + ERROR = 3, }" `; @@ -2823,9 +2866,9 @@ exports[`generation v3 file(./test/result/v3/models/EnumWithStrings.ts): ./test/ * This is a simple enum with strings */ export enum EnumWithStrings { - ERROR = 'Error', SUCCESS = 'Success', WARNING = 'Warning', + ERROR = 'Error', }" `; @@ -3104,10 +3147,10 @@ import { Dictionary } from './Dictionary'; * This is a model with nested enums */ export interface ModelWithNestedEnums { - arrayWithDescription?: Array<(1 | 2 | 3)>; - arrayWithEnum?: Array<('Success' | 'Warning' | 'Error')>; dictionaryWithEnum?: Dictionary<('Success' | 'Warning' | 'Error')>; dictionaryWithEnumFromDescription?: Dictionary<(1 | 2 | 3)>; + arrayWithEnum?: Array<('Success' | 'Warning' | 'Error')>; + arrayWithDescription?: Array<(1 | 2 | 3)>; } " `; @@ -3132,6 +3175,24 @@ export interface ModelWithNestedProperties { " `; +exports[`generation v3 file(./test/result/v3/models/ModelWithOrderedProperties.ts): ./test/result/v3/models/ModelWithOrderedProperties.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + + +/** + * This is a model with ordered properties + */ +export interface ModelWithOrderedProperties { + zebra?: string; + apple?: string; + hawaii?: string; +} +" +`; + exports[`generation v3 file(./test/result/v3/models/ModelWithProperties.ts): ./test/result/v3/models/ModelWithProperties.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -3144,13 +3205,13 @@ import { ModelWithString } from './ModelWithString'; * This is a model with one nested property */ export interface ModelWithProperties { - boolean?: boolean; - number?: number; - reference?: ModelWithString; required: string; - requiredAndNullable: string | null; readonly requiredAndReadOnly: string; + requiredAndNullable: string | null; string?: string; + number?: number; + boolean?: boolean; + reference?: ModelWithString; } " `; @@ -3640,18 +3701,18 @@ exports[`generation v3 file(./test/result/v3/schemas/$ModelWithNestedEnums.ts): export const $ModelWithNestedEnums = { properties: { - arrayWithDescription: { - type: 'Array', - }, - arrayWithEnum: { - type: 'Array', - }, dictionaryWithEnum: { type: 'Dictionary', }, dictionaryWithEnumFromDescription: { type: 'Dictionary', }, + arrayWithEnum: { + type: 'Array', + }, + arrayWithDescription: { + type: 'Array', + }, }, };" `; @@ -3688,6 +3749,27 @@ export const $ModelWithNestedProperties = { };" `; +exports[`generation v3 file(./test/result/v3/schemas/$ModelWithOrderedProperties.ts): ./test/result/v3/schemas/$ModelWithOrderedProperties.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +/* prettier-ignore */ + +export const $ModelWithOrderedProperties = { + properties: { + zebra: { + type: 'string', + }, + apple: { + type: 'string', + }, + hawaii: { + type: 'string', + }, + }, +};" +`; + exports[`generation v3 file(./test/result/v3/schemas/$ModelWithProperties.ts): ./test/result/v3/schemas/$ModelWithProperties.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -3696,32 +3778,32 @@ exports[`generation v3 file(./test/result/v3/schemas/$ModelWithProperties.ts): . export const $ModelWithProperties = { properties: { - boolean: { - type: 'boolean', - }, - number: { - type: 'number', - }, - reference: { - type: 'ModelWithString', - }, required: { type: 'string', isRequired: true, }, + requiredAndReadOnly: { + type: 'string', + isReadOnly: true, + isRequired: true, + }, requiredAndNullable: { type: 'string', isRequired: true, isNullable: true, }, - requiredAndReadOnly: { - type: 'string', - isReadOnly: true, - isRequired: true, - }, string: { type: 'string', }, + number: { + type: 'number', + }, + boolean: { + type: 'boolean', + }, + reference: { + type: 'ModelWithString', + }, }, };" `; @@ -4023,6 +4105,22 @@ import { OpenAPI } from '../core/OpenAPI'; export class ResponseService { + /** + * @result ModelWithString + * @throws ApiError + */ + public static async callWithResponse(): Promise { + + const result = await __request({ + method: 'get', + path: \`/api/v\${OpenAPI.VERSION}/response\`, + }); + + catchGenericError(result); + + return result.body; + } + /** * @result ModelWithString Message for default response * @throws ApiError @@ -4047,22 +4145,6 @@ export class ResponseService { return result.body; } - /** - * @result ModelWithString - * @throws ApiError - */ - public static async callWithResponse(): Promise { - - const result = await __request({ - method: 'get', - path: \`/api/v\${OpenAPI.VERSION}/response\`, - }); - - catchGenericError(result); - - return result.body; - } - /** * @result ModelWithString Message for default response * @result ModelThatExtends Message for 201 response @@ -4104,21 +4186,6 @@ import { OpenAPI } from '../core/OpenAPI'; export class SimpleService { - /** - * @throws ApiError - */ - public static async deleteCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'delete', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - /** * @throws ApiError */ @@ -4137,40 +4204,10 @@ export class SimpleService { /** * @throws ApiError */ - public static async headCallWithoutParametersAndResponse(): Promise { + public static async putCallWithoutParametersAndResponse(): Promise { const result = await __request({ - method: 'head', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - - /** - * @throws ApiError - */ - public static async optionsCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'options', - path: \`/api/v\${OpenAPI.VERSION}/simple\`, - }); - - catchGenericError(result); - - return result.body; - } - - /** - * @throws ApiError - */ - public static async patchCallWithoutParametersAndResponse(): Promise { - - const result = await __request({ - method: 'patch', + method: 'put', path: \`/api/v\${OpenAPI.VERSION}/simple\`, }); @@ -4197,10 +4234,55 @@ export class SimpleService { /** * @throws ApiError */ - public static async putCallWithoutParametersAndResponse(): Promise { + public static async deleteCallWithoutParametersAndResponse(): Promise { const result = await __request({ - method: 'put', + method: 'delete', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async optionsCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'options', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async headCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'head', + path: \`/api/v\${OpenAPI.VERSION}/simple\`, + }); + + catchGenericError(result); + + return result.body; + } + + /** + * @throws ApiError + */ + public static async patchCallWithoutParametersAndResponse(): Promise { + + const result = await __request({ + method: 'patch', path: \`/api/v\${OpenAPI.VERSION}/simple\`, }); diff --git a/test/mock/v2/spec.json b/test/mock/v2/spec.json index 5cae0125..0499b244 100644 --- a/test/mock/v2/spec.json +++ b/test/mock/v2/spec.json @@ -813,6 +813,21 @@ } } }, + "ModelWithOrderedProperties": { + "description": "This is a model with ordered properties", + "type": "object", + "properties": { + "zebra": { + "type": "string" + }, + "apple": { + "type": "string" + }, + "hawaii": { + "type": "string" + } + } + }, "ModelWithDuplicateImports": { "description": "This is a model with duplicated imports", "type": "object", diff --git a/test/mock/v3/spec.json b/test/mock/v3/spec.json index 3bf6300f..e9a510f8 100644 --- a/test/mock/v3/spec.json +++ b/test/mock/v3/spec.json @@ -985,6 +985,21 @@ } } }, + "ModelWithOrderedProperties": { + "description": "This is a model with ordered properties", + "type": "object", + "properties": { + "zebra": { + "type": "string" + }, + "apple": { + "type": "string" + }, + "hawaii": { + "type": "string" + } + } + }, "ModelWithDuplicateImports": { "description": "This is a model with duplicated imports", "type": "object",