Skip to content
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

allof, oneof, discriminator support #138

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 111 additions & 4 deletions .snapshot/all/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +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 {
category: $types.TypeOrUndefinedNullable<ICategory>;
category: $types.TypeOrUndefinedNullable<ICategory[]>;
colors: $types.TypeOrUndefined<string[]>;
expireDate: $types.TypeOrUndefined<string>;
externalId: $types.TypeOrUndefinedNullable<string>;
Expand All @@ -27,6 +44,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 +100,72 @@ 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 category: CategoryUnion[] = [];
public colors: string[] = [];
public expireDate: $types.TypeOrUndefined<Date> = undefined;
public externalId: $types.TypeOrUndefinedNullable<Guid> = undefined;
Expand All @@ -70,7 +177,7 @@ export class Product {

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

public static fromDTO(dto: IProduct): Product {
const model = new Product();
model.category = dto.category ? Category.fromDTO(dto.category) : undefined;
model.category = dto.category ? dto.category.map(x => CategoryUnionClass.fromDTO(x)) : [];
model.colors = dto.colors ? dto.colors : [];
model.expireDate = toDateIn(dto.expireDate);
model.externalId = dto.externalId ? new Guid(dto.externalId) : null;
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