diff --git a/src/generators/MethodGenerator.test.ts b/src/generators/MethodGenerator.test.ts index df3346ee..5ed8c99e 100644 --- a/src/generators/MethodGenerator.test.ts +++ b/src/generators/MethodGenerator.test.ts @@ -123,7 +123,7 @@ describe('MethodGenerator', () => { expect(methodDefinition.normalizedUrl).toBe('/identity-providers'); expect(methodDefinition.parameterMethod).toBe('postById'); expect(methodDefinition.pathParameters[0]).toEqual({name: 'id', type: 'any'}); - expect(methodDefinition.bodyParameters[0]).toEqual({name: 'body', required: false, type: '{ user: string }'}); + expect(methodDefinition.bodyParameters[0]).toEqual({name: 'body', required: false, type: '{ user?: string }'}); }); }); }); diff --git a/src/generators/MethodGenerator.ts b/src/generators/MethodGenerator.ts index e660420e..fd908f08 100644 --- a/src/generators/MethodGenerator.ts +++ b/src/generators/MethodGenerator.ts @@ -5,6 +5,7 @@ import * as StringUtil from '../util/StringUtil'; export enum SwaggerType { ARRAY = 'array', + BOOLEAN = 'boolean', INTEGER = 'integer', NUMBER = 'number', OBJECT = 'object', @@ -14,6 +15,7 @@ export enum SwaggerType { export enum TypeScriptType { ANY = 'any', ARRAY = 'Array', + BOOLEAN = 'boolean', EMPTY_OBJECT = '{}', NUMBER = 'number', STRING = 'string', @@ -240,9 +242,11 @@ export class MethodGenerator { const schema: Record = {}; - for (const property of Object.keys(properties)) { - const propertyName = requiredProperties && !requiredProperties.includes(property) ? `${property}?` : property; - schema[propertyName] = this.buildTypeFromSchema(properties[property], `${schemaName}/${property}`); + for (const [property, propertyOptons] of Object.entries(properties)) { + const isRequired = requiredProperties && requiredProperties.includes(property); + const isReadOnly = propertyOptons.readOnly; + const propertyName = `${isReadOnly ? 'readonly ' : ''}${property}${isRequired ? '' : '?'}`; + schema[propertyName] = this.buildTypeFromSchema(propertyOptons, `${schemaName}/${property}`); } return inspect(schema, {breakLength: Infinity, depth: Infinity}) @@ -277,6 +281,9 @@ export class MethodGenerator { case SwaggerType.STRING: { return TypeScriptType.STRING; } + case SwaggerType.BOOLEAN: { + return TypeScriptType.BOOLEAN; + } case SwaggerType.NUMBER: case SwaggerType.INTEGER: { return TypeScriptType.NUMBER; diff --git a/src/test/snapshots/1-query-param-description.ts.fixture b/src/test/snapshots/1-query-param-description.ts.fixture index 55cbd1d8..05068ba5 100644 --- a/src/test/snapshots/1-query-param-description.ts.fixture +++ b/src/test/snapshots/1-query-param-description.ts.fixture @@ -19,8 +19,8 @@ export class ArchiveService { */ archiveConversation = async ( instanceId: string, - body: {archive: {}; conversationId: string} - ): Promise<{instanceId: string; name: string}> => { + body: {archive?: boolean; conversationId?: string} + ): Promise<{instanceId?: string; name?: string}> => { const config: AxiosRequestConfig = { data: { ...body, @@ -29,8 +29,8 @@ export class ArchiveService { url: `/instance/${instanceId}/archive`, }; const response = await this.apiClient.request<{ - instanceId: string; - name: string; + instanceId?: string; + name?: string; }>(config); return response.data; };