Skip to content

Commit

Permalink
fix Date type (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Leshchev <artem.leshchev@dxc.com>
  • Loading branch information
aleshchev and Artem Leshchev authored Nov 10, 2023
1 parent 2528ab5 commit 2164424
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Build & Test

on:
pull_request:
branches: [ master, develop ]
branches: [ master ]

jobs:
build:
Expand Down
12 changes: 8 additions & 4 deletions __tests__/services/UriBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('UriBuilder tests', () => {
type: {
dtoType: 'IProduct',
kind: PropertyKind.Object,
type: 'Product'
type: 'Product',
isNullable: true
}
},
originUri: 'get/{id}'
Expand Down Expand Up @@ -67,7 +68,8 @@ describe('UriBuilder tests', () => {
type: {
dtoType: 'IProduct',
kind: PropertyKind.Object,
type: 'Product'
type: 'Product',
isNullable: true
}
},
originUri: 'get'
Expand Down Expand Up @@ -109,7 +111,8 @@ describe('UriBuilder tests', () => {
type: {
dtoType: 'IProduct',
kind: PropertyKind.Object,
type: 'Product'
type: 'Product',
isNullable: true
}
},
originUri: 'get/{id}'
Expand Down Expand Up @@ -157,7 +160,8 @@ describe('UriBuilder tests', () => {
type: {
dtoType: 'IProduct',
kind: PropertyKind.Object,
type: 'Product'
type: 'Product',
isNullable: true
}
},
originUri: 'getByCustomer/{customer}/type/{type}'
Expand Down
8 changes: 6 additions & 2 deletions libs/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ export function mapCollection<TDTO, TModel>(dtoMapper: { fromDTO(value: TDTO): T
}

export function mapSingle<TDTO, TModel>(dtoMapper: { fromDTO(value: TDTO): TModel }): OperatorFunction<TypeOrUndefined<TDTO>, TypeOrUndefined<TModel>> {
return map((z: TypeOrUndefined<TDTO>) => (z ? dtoMapper.fromDTO(z) : undefined));
return map((x: TypeOrUndefined<TDTO>) => (x ? dtoMapper.fromDTO(x) : undefined));
}

export function mapIdentityCollection<TModel>(identity: { new (id: TypeOrUndefined<string>): TModel }): OperatorFunction<DTOIdentityType[], TModel[]> {
return map((z: DTOIdentityType[]) => z.map((x) => new identity(x.id)));
}

export function mapIdentitySingle<TModel>(identity: { new (id: TypeOrUndefined<string>): TModel }): OperatorFunction<TypeOrUndefined<DTOIdentityType>, TypeOrUndefined<TModel>> {
return map((z: TypeOrUndefined<DTOIdentityType>) => (z ? new identity(z.id) : undefined));
return map((x: TypeOrUndefined<DTOIdentityType>) => (x ? new identity(x.id) : undefined));
}

export function mapDate(): OperatorFunction<string, TypeOrUndefined<Date>> {
return map(toDateIn);
}

export function mapDateStrict(): OperatorFunction<string, Date> {
return map((x) => toDateIn(x)!);
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luxbss/gengen",
"version": "1.2.2",
"version": "1.2.3",
"description": "Tool for generating models and Angular services based on OpenAPIs and Swagger's JSON",
"bin": {
"gengen": "./bin/index.js"
Expand Down
3 changes: 2 additions & 1 deletion src/generators/angular/AngularServicesMethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ export class AngularServicesMethodGenerator {

return [PropertyKind.Object, PropertyKind.Identity, PropertyKind.Guid, PropertyKind.Date].includes(returnType.type.kind);
}

protected createPipe(returnType: ITypeInfo): string {
if (returnType.type.kind === PropertyKind.Guid) {
return 'mapGuid()';
}

if (returnType.type.kind === PropertyKind.Date) {
return `${MAPPERS_NAMESPACE}.mapDate()`;
return returnType.type.isNullable ? `${MAPPERS_NAMESPACE}.mapDate()` : `${MAPPERS_NAMESPACE}.mapDateStrict()`;
}

const type = `${MODELS_NAMESPACE}.${returnType.type.type}`;
Expand Down
1 change: 1 addition & 0 deletions src/models/EnumModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IEnumModel {
name: string;
isNullable: boolean;
items: { key: string; value: number }[];
}
1 change: 1 addition & 0 deletions src/models/IdentityModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IObjectPropertyModel } from './ObjectModel';

export interface IIdentityModel {
name: string;
isNullable: boolean;
dtoType: string;
property: IObjectPropertyModel;
}
1 change: 1 addition & 0 deletions src/models/ObjectModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export interface IObjectPropertyModel extends IType {
export interface IObjectModel {
name: string;
dtoType: string;
isNullable: boolean;
properties: IObjectPropertyModel[];
}
1 change: 1 addition & 0 deletions src/models/TypeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface IType {
kind: PropertyKind;
type: string;
dtoType: string;
isNullable: boolean;
}
11 changes: 7 additions & 4 deletions src/services/ModelFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ import { IOpenAPI3Reference } from '../swagger/v3/reference';
import { IModel } from './ServiceMappingService';

export class ModelFinder {
public constructor(private readonly openAPIService: OpenAPIService, private readonly models: IModelsContainer) {}
public constructor(
private readonly openAPIService: OpenAPIService,
private readonly models: IModelsContainer
) {}

public find(ref: IOpenAPI3Reference): IModel | undefined {
const name = this.openAPIService.getSchemaKey(ref);

const objectModel = this.models.objects.find((x) => x.name === name);
if (objectModel) {
return { kind: PropertyKind.Object, name, dtoType: objectModel.dtoType };
return { kind: PropertyKind.Object, name, dtoType: objectModel.dtoType, isNullable: objectModel.isNullable };
}

const identityModel = this.models.identities.find((x) => x.name === name);
if (identityModel) {
return { kind: PropertyKind.Identity, name, dtoType: identityModel.dtoType };
return { kind: PropertyKind.Identity, name, dtoType: identityModel.dtoType, isNullable: identityModel.isNullable };
}

const enumModel = this.models.enums.find((x) => x.name === name);
if (enumModel) {
return { kind: PropertyKind.Enum, name, dtoType: name };
return { kind: PropertyKind.Enum, name, dtoType: name, isNullable: enumModel.isNullable };
}

return undefined;
Expand Down
4 changes: 3 additions & 1 deletion src/services/ModelMappingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class ModelMappingService {
if (this.isIdentity(schema)) {
identities.push({
name,
isNullable: false,
dtoType: this.getInterfaceName(name),
property: {
...this.typesService.getSimpleType(schema.properties['id'] as IOpenAPI3GuidSchema),
Expand All @@ -63,6 +64,7 @@ export class ModelMappingService {
private toEnumModel(name: string, schema: IOpenAPI3EnumSchema): IEnumModel {
return {
name,
isNullable: schema.nullable ?? false,
items: schema.enum
.map((value, index) => ({
key: schema['x-enumNames'][index],
Expand All @@ -73,7 +75,7 @@ export class ModelMappingService {
}

private toObjectModel(name: string, schema: IOpenAPI3ObjectSchema): IObjectModel {
const model: IObjectModel = { name, dtoType: this.getInterfaceName(name), properties: [] };
const model: IObjectModel = { name, isNullable: schema.nullable ?? false, dtoType: this.getInterfaceName(name), properties: [] };
if (!schema.properties) {
return model;
}
Expand Down
9 changes: 7 additions & 2 deletions src/services/ServiceMappingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { TypesService } from './TypesService';
export interface IModel {
name: string;
dtoType: string;
isNullable: boolean;
kind: PropertyKind;
}

Expand Down Expand Up @@ -132,7 +133,7 @@ export class ServiceMappingService {
model.returnType = {
isCollection: false,
isModel: false,
type: { kind: PropertyKind.Object, type: 'IDownloadResult', dtoType: 'IDownloadResult' }
type: { kind: PropertyKind.Object, type: 'IDownloadResult', dtoType: 'IDownloadResult', isNullable: false }
};

model.parameters.push({
Expand Down Expand Up @@ -220,7 +221,11 @@ export class ServiceMappingService {
return undefined;
}

return { isCollection, isModel: true, type: { kind: model.kind, dtoType: model.dtoType, type: model.name } };
return {
isCollection,
isModel: true,
type: { kind: model.kind, dtoType: model.dtoType, type: model.name, isNullable: model.isNullable }
};
}

private hasDownloadResponse(operation: IOpenAPI3Operation): boolean {
Expand Down
16 changes: 10 additions & 6 deletions src/services/TypesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ import { OpenAPITypesGuard } from '../swagger/OpenAPITypesGuard';
import { OpenAPI3SimpleSchema } from '../swagger/v3/schemas/schema';

export class TypesService {
constructor(private readonly typesGuard: OpenAPITypesGuard, private readonly settings: IOptions) {}
constructor(
private readonly typesGuard: OpenAPITypesGuard,
private readonly settings: IOptions
) {}

public getSimpleType(schema: OpenAPI3SimpleSchema): IType {
const isNullable = schema.nullable ?? false;
if (!this.settings.unstrictId && this.typesGuard.isGuid(schema)) {
return { kind: PropertyKind.Guid, type: 'Guid', dtoType: 'string' };
return { kind: PropertyKind.Guid, type: 'Guid', dtoType: 'string', isNullable: isNullable };
}

if (this.typesGuard.isDate(schema)) {
return { kind: PropertyKind.Date, type: 'Date', dtoType: 'string' };
return { kind: PropertyKind.Date, type: 'Date', dtoType: 'string', isNullable: isNullable };
}

if (this.typesGuard.isBoolean(schema)) {
return { kind: PropertyKind.None, type: 'boolean', dtoType: 'boolean' };
return { kind: PropertyKind.None, type: 'boolean', dtoType: 'boolean', isNullable: isNullable };
}

if (this.typesGuard.isNumber(schema)) {
return { kind: PropertyKind.None, type: 'number', dtoType: 'number' };
return { kind: PropertyKind.None, type: 'number', dtoType: 'number', isNullable: isNullable };
}

return { kind: PropertyKind.None, type: 'string', dtoType: 'string' };
return { kind: PropertyKind.None, type: 'string', dtoType: 'string', isNullable: isNullable };
}
}

0 comments on commit 2164424

Please sign in to comment.