Skip to content

allof, oneof, discriminator support #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 24, 2024
113 changes: 112 additions & 1 deletion .snapshot/all/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ 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
}

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

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

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

export type ICategoryElectronicsDto = ICategoryElectronicsDtoBaseInterface & ICategory;
export type ICategoryMotorsDto = ICategoryMotorsDtoBaseInterface & ICategory;

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

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

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 +101,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: CategoryUnionClass[] = [];
public category: $types.TypeOrUndefinedNullable<CategoryUnionClass> = undefined;
public colors: string[] = [];
public expireDate: $types.TypeOrUndefined<Date> = undefined;
public externalId: $types.TypeOrUndefinedNullable<Guid> = undefined;
Expand All @@ -70,6 +179,7 @@ export class Product {

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

public static fromDTO(dto: IProduct): Product {
const model = new Product();
model.categories = dto.categories ? dto.categories.map(x => Category.fromDTO(x)) : [];
model.category = dto.category ? Category.fromDTO(dto.category) : undefined;
model.colors = dto.colors ? dto.colors : [];
model.expireDate = toDateIn(dto.expireDate);
Expand Down
6 changes: 3 additions & 3 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.6",
"version": "1.2.7",
"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
Loading