Skip to content

Commit

Permalink
Merge pull request #139 from Luxoft/anklimenko/oneof-support
Browse files Browse the repository at this point in the history
allof, oneof, discriminator support
  • Loading branch information
KlimenkoAV authored May 24, 2024
2 parents 36a27ef + e5f5367 commit 217cd9e
Show file tree
Hide file tree
Showing 25 changed files with 965 additions and 294 deletions.
120 changes: 116 additions & 4 deletions .snapshot/all/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,40 @@ import { Guid } from './Guid';
import { toDateIn, toDateOut } from './date-converters';
import type * as $types from './types';

export enum CategoryUnionTypes {
CategoryElectronicsDto = '1',
CategoryMotorsDto = '2'
}

export enum ProductStatus {
InStock = 0,
OutOfStock = -1,
UnderTheOrder = 1
}

export type CategoryUnion = Category | CategoryElectronicsDto | CategoryMotorsDto;
export type ICategoryUnion = ICategory | ICategoryElectronicsDto | ICategoryMotorsDto;

export interface ICategory {
name: $types.TypeOrUndefinedNullable<string>;
type: $types.TypeOrUndefined<string>;
}

interface ICategoryElectronicsDtoBaseInterface {
syntheticTest: $types.TypeOrUndefinedNullable<number>;
}

export type ICategoryElectronicsDto = ICategoryElectronicsDtoBaseInterface & ICategory;

interface ICategoryMotorsDtoBaseInterface {
volume: $types.TypeOrUndefinedNullable<number>;
}

export type ICategoryMotorsDto = ICategoryMotorsDtoBaseInterface & ICategory;

export interface IProduct {
category: $types.TypeOrUndefinedNullable<ICategory>;
categories: $types.TypeOrUndefined<ICategoryUnion[]>;
category: $types.TypeOrUndefined<ICategoryUnion>;
colors: $types.TypeOrUndefined<string[]>;
expireDate: $types.TypeOrUndefined<string>;
externalId: $types.TypeOrUndefinedNullable<string>;
Expand All @@ -27,6 +49,44 @@ export interface IProductIdentityDTO {
id: $types.TypeOrUndefined<string>;
}

export class CategoryUnionClass {
public static fromDTO(dto: ICategoryUnion): CategoryUnion {
if (this.isCategoryElectronicsDto(dto)) {
return CategoryElectronicsDto.fromDTO(dto);
}
if (this.isCategoryMotorsDto(dto)) {
return CategoryMotorsDto.fromDTO(dto);
}
return Category.fromDTO(dto);
}

public static toDTO(model: CategoryUnion): ICategoryUnion {
if (this.isICategoryElectronicsDto(model)) {
return CategoryElectronicsDto.toDTO(model);
}
if (this.isICategoryMotorsDto(model)) {
return CategoryMotorsDto.toDTO(model);
}
return Category.toDTO(model);
}

private static isCategoryElectronicsDto(dto: ICategoryUnion): dto is ICategoryElectronicsDto {
return dto.type === CategoryUnionTypes.CategoryElectronicsDto;
}

private static isCategoryMotorsDto(dto: ICategoryUnion): dto is ICategoryMotorsDto {
return dto.type === CategoryUnionTypes.CategoryMotorsDto;
}

private static isICategoryElectronicsDto(dto: CategoryUnion): dto is CategoryElectronicsDto {
return dto.type === CategoryUnionTypes.CategoryElectronicsDto;
}

private static isICategoryMotorsDto(dto: CategoryUnion): dto is CategoryMotorsDto {
return dto.type === CategoryUnionTypes.CategoryMotorsDto;
}
}

export class ProductIdentityDTO {
public id: Guid;
private __productIdentityDTO!: string;
Expand All @@ -42,23 +102,73 @@ export class ProductIdentityDTO {

export class Category {
public name: $types.TypeOrUndefinedNullable<string> = undefined;
public type: $types.TypeOrUndefined<string> = undefined;
private __category!: string;

public static toDTO(model: Partial<Category>): ICategory {
return {
name: model.name,
type: model.type,
};
}

public static fromDTO(dto: ICategory): Category {
const model = new Category();
model.name = dto.name;
model.type = dto.type;
return model;
}
}

export class CategoryElectronicsDto {
public name: $types.TypeOrUndefinedNullable<string> = undefined;
public type: $types.TypeOrUndefined<string> = undefined;
public syntheticTest: $types.TypeOrUndefinedNullable<number> = undefined;
private __categoryElectronicsDto!: string;

public static toDTO(model: Partial<CategoryElectronicsDto>): ICategoryElectronicsDto {
return {
syntheticTest: model.syntheticTest,
name: model.name,
type: model.type,
};
}

public static fromDTO(dto: ICategoryElectronicsDto): CategoryElectronicsDto {
const model = new CategoryElectronicsDto();
model.syntheticTest = dto.syntheticTest;
model.name = dto.name;
model.type = dto.type;
return model;
}
}

export class CategoryMotorsDto {
public name: $types.TypeOrUndefinedNullable<string> = undefined;
public type: $types.TypeOrUndefined<string> = undefined;
public volume: $types.TypeOrUndefinedNullable<number> = undefined;
private __categoryMotorsDto!: string;

public static toDTO(model: Partial<CategoryMotorsDto>): ICategoryMotorsDto {
return {
volume: model.volume,
name: model.name,
type: model.type,
};
}

public static fromDTO(dto: ICategoryMotorsDto): CategoryMotorsDto {
const model = new CategoryMotorsDto();
model.volume = dto.volume;
model.name = dto.name;
model.type = dto.type;
return model;
}
}

export class Product {
public category: $types.TypeOrUndefinedNullable<Category> = undefined;
public categories: CategoryUnion[] = [];
public category: $types.TypeOrUndefined<CategoryUnion> = undefined;
public colors: string[] = [];
public expireDate: $types.TypeOrUndefined<Date> = undefined;
public externalId: $types.TypeOrUndefinedNullable<Guid> = undefined;
Expand All @@ -70,7 +180,8 @@ export class Product {

public static toDTO(model: Partial<Product>): IProduct {
return {
category: model.category ? Category.toDTO(model.category) : undefined,
categories: model.categories ? model.categories.map(x => CategoryUnionClass.toDTO(x)) : undefined,
category: model.category ? CategoryUnionClass.toDTO(model.category) : undefined,
colors: model.colors,
expireDate: toDateOut(model.expireDate),
externalId: model.externalId ? model.externalId.toString() : null,
Expand All @@ -83,7 +194,8 @@ export class Product {

public static fromDTO(dto: IProduct): Product {
const model = new Product();
model.category = dto.category ? Category.fromDTO(dto.category) : undefined;
model.categories = dto.categories ? dto.categories.map(x => CategoryUnionClass.fromDTO(x)) : [];
model.category = dto.category ? CategoryUnionClass.fromDTO(dto.category) : undefined;
model.colors = dto.colors ? dto.colors : [];
model.expireDate = toDateIn(dto.expireDate);
model.externalId = dto.externalId ? new Guid(dto.externalId) : null;
Expand Down
4 changes: 3 additions & 1 deletion __tests__/services/ModelMappingService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MockOpenAPIService } from '../../__mocks__/MockOpenAPIService';
import { defaultOptions } from '../../src/options';
import { ModelMappingService } from '../../src/services/ModelMappingService';
import { NameService } from '../../src/services/NameService';
import { TypesService } from '../../src/services/TypesService';
import { OpenAPITypesGuard } from '../../src/swagger/OpenAPITypesGuard';
import { OpenAPI3SchemaContainer } from '../../src/swagger/v3/schemas/schema';
Expand All @@ -11,8 +12,9 @@ describe('ModelMappingService tests', () => {

beforeEach(() => {
const guard = new OpenAPITypesGuard();
const nameService = new NameService();
const openAPIService = new MockOpenAPIService(guard);
service = new ModelMappingService(openAPIService, guard, new TypesService(guard, defaultOptions));
service = new ModelMappingService(openAPIService, guard, new TypesService(guard, defaultOptions), nameService);
});

describe('toModelsContainer', () => {
Expand Down
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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luxbss/gengen",
"version": "1.2.7",
"version": "1.2.8",
"description": "Tool for generating models and Angular services based on OpenAPIs and Swagger's JSON",
"bin": {
"gengen": "./bin/index.js"
Expand All @@ -14,6 +14,7 @@
"g:withRequestOptions": "node ./bin/index.js g --all --file=./swagger.json --output=./.output/withRequestOptions --withRequestOptions",
"g:alias": "node ./bin/index.js g --file=./swagger.json --aliasName alias --output=./.output/selected",
"e2e": "npm run g && npm run g:withRequestOptions && ts-node ./e2e/e2e.ts",
"g:b": "npm run build && npm run g",
"test": "jest",
"test:w": "jest --watch",
"coverage": "jest --coverage",
Expand Down
Loading

0 comments on commit 217cd9e

Please sign in to comment.