From 9b017fac0ac22bedfc429c5274c642cbf14fa4a2 Mon Sep 17 00:00:00 2001 From: llotall Date: Mon, 15 Nov 2021 11:16:41 +0300 Subject: [PATCH] feat: Support array of datetimes --- .snapshot/all/models.ts | 4 ++++ __tests__/swagger/OpenAPITypesGuard.spec.ts | 14 ++++++++++++-- package-lock.json | 2 +- package.json | 2 +- src/generators/ModelsGenerator.ts | 8 ++++++++ src/swagger/OpenAPITypesGuard.ts | 10 +++++----- swagger.json | 8 ++++++++ 7 files changed, 39 insertions(+), 9 deletions(-) diff --git a/.snapshot/all/models.ts b/.snapshot/all/models.ts index 444684b..8766559 100644 --- a/.snapshot/all/models.ts +++ b/.snapshot/all/models.ts @@ -17,6 +17,7 @@ export interface IProduct { expireDate: $types.TypeOrUndefined; externalId: $types.TypeOrUndefinedNullable; id: $types.TypeOrUndefined; + modifyDates: $types.TypeOrUndefined; name: $types.TypeOrUndefinedNullable; status: $types.TypeOrUndefined; } @@ -60,6 +61,7 @@ export class Product { public expireDate: $types.TypeOrUndefined = undefined; public externalId: $types.TypeOrUndefinedNullable = undefined; public id: $types.TypeOrUndefined = undefined; + public modifyDates: $types.TypeOrUndefined = undefined; public name: $types.TypeOrUndefinedNullable = undefined; public status: $types.TypeOrUndefined = undefined; private __product!: string; @@ -70,6 +72,7 @@ export class Product { expireDate: toDateOut(model.expireDate), externalId: model.externalId ? model.externalId.toString() : null, id: model.id ? model.id.toString() : Guid.empty.toString(), + modifyDates: model.modifyDates ? model.modifyDates.map(toDateOut) : undefined, name: model.name, status: model.status, }; @@ -81,6 +84,7 @@ export class Product { model.expireDate = toDateIn(dto.expireDate); model.externalId = dto.externalId ? new Guid(dto.externalId) : null; model.id = new Guid(dto.id); + model.modifyDates = dto.modifyDates ? dto.modifyDates.map(toDateIn) : []; model.name = dto.name; model.status = dto.status; return model; diff --git a/__tests__/swagger/OpenAPITypesGuard.spec.ts b/__tests__/swagger/OpenAPITypesGuard.spec.ts index 3b41018..ac0ad6c 100644 --- a/__tests__/swagger/OpenAPITypesGuard.spec.ts +++ b/__tests__/swagger/OpenAPITypesGuard.spec.ts @@ -66,8 +66,18 @@ describe('OpenAPITypesGuard tests', () => { }); }); - test('isString', () => { - expect(guard.isString({ type: 'string' })).toBeTruthy(); + describe('isString', () => { + test('date', () => { + expect(guard.isString({ type: 'string', format: 'date-time' })).toBeFalsy(); + }); + + test('guid', () => { + expect(guard.isString({ type: 'string', format: 'uuid' })).toBeFalsy(); + }); + + test('string', () => { + expect(guard.isString({ type: 'string' })).toBeTruthy(); + }); }); test('isDate', () => { diff --git a/package-lock.json b/package-lock.json index 41394b9..e662f30 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@luxbss/gengen", - "version": "1.0.0-rc.6", + "version": "1.0.0-rc.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0dc20a9..b6c52b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@luxbss/gengen", - "version": "1.0.0-rc.6", + "version": "1.0.0-rc.7", "description": "Tool for generating models and Angular services based on OpenAPIs and Swagger's JSON", "bin": { "gengen": "./bin/index.js" diff --git a/src/generators/ModelsGenerator.ts b/src/generators/ModelsGenerator.ts index 6c1e621..14d7f81 100644 --- a/src/generators/ModelsGenerator.ts +++ b/src/generators/ModelsGenerator.ts @@ -173,6 +173,10 @@ export class ModelsGenerator { switch (property.kind) { case PropertyKind.Date: + if (property.isCollection) { + return `${modelProperty} ? ${modelProperty}.map(toDateOut) : ${UNDEFINED_STRING}`; + } + return `toDateOut(${modelProperty})`; case PropertyKind.Guid: @@ -208,6 +212,10 @@ export class ModelsGenerator { switch (property.kind) { case PropertyKind.Date: + if (property.isCollection) { + return `${dtoProperty} ? ${dtoProperty}.map(toDateIn) : []`; + } + return `toDateIn(${dtoProperty})`; case PropertyKind.Guid: diff --git a/src/swagger/OpenAPITypesGuard.ts b/src/swagger/OpenAPITypesGuard.ts index 2852a55..19008b9 100644 --- a/src/swagger/OpenAPITypesGuard.ts +++ b/src/swagger/OpenAPITypesGuard.ts @@ -25,7 +25,7 @@ export class OpenAPITypesGuard { } public isGuid(schema: SchemaType): schema is IOpenAPI3GuidSchema { - return this.isString(schema) && (schema as IOpenAPI3GuidSchema)?.format === 'uuid'; + return (schema as IOpenAPI3StringSchema)?.type === 'string' && (schema as IOpenAPI3GuidSchema)?.format === 'uuid'; } public isCollection(schema: SchemaType): schema is IOpenAPI3ArraySchema { @@ -56,11 +56,11 @@ export class OpenAPITypesGuard { } public isString(schema: SchemaType): schema is IOpenAPI3StringSchema { - return (schema as IOpenAPI3StringSchema)?.type === 'string'; + return (schema as IOpenAPI3StringSchema)?.type === 'string' && (schema as { format: string | undefined })?.format === undefined; } public isDate(schema: SchemaType): schema is IOpenAPI3DateSchema { - return this.isString(schema) && (schema as IOpenAPI3DateSchema)?.format === 'date-time'; + return (schema as IOpenAPI3StringSchema)?.type === 'string' && (schema as IOpenAPI3DateSchema)?.format === 'date-time'; } public isBoolean(schema: SchemaType): schema is IOpenAPI3BooleanSchema { @@ -68,6 +68,6 @@ export class OpenAPITypesGuard { } public isSimple(schema: SchemaType): schema is OpenAPI3SimpleSchema { - return this.isGuid(schema) || this.isNumber(schema) || this.isString(schema) || this.isDate(schema) || this.isBoolean(schema); + return this.isGuid(schema) || this.isNumber(schema) || this.isDate(schema) || this.isString(schema) || this.isBoolean(schema); } -} +} \ No newline at end of file diff --git a/swagger.json b/swagger.json index 77dea2f..2dd3fad 100644 --- a/swagger.json +++ b/swagger.json @@ -585,6 +585,14 @@ "type": "string", "format": "date-time" }, + "modifyDates": { + "type": "array", + "items": { + "type": "string", + "format": "date-time" + }, + "nullable": true + }, "category": { "$ref": "#/components/schemas/Category" },