Skip to content

Commit

Permalink
update providing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Klimenko committed May 24, 2024
1 parent 6559d93 commit e5f5367
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
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
13 changes: 7 additions & 6 deletions src/generators/ModelsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export const TO_DTO_METHOD = 'toDTO';
export const FROM_DTO_METHOD = 'fromDTO';

export class ModelsGenerator {
private interfaceGenerator = new InterfacesGenerator();
private unionGenerator = new UnionGenerator();
private identitiesGenerator = new IdentitiesGenerator(this.settings);
private objectGenerator = new ObjectGenerator();

private pathBuilder = new PathBuilder();

constructor(private settings: IOptions) {}
constructor(
private settings: IOptions,
private readonly interfaceGenerator: InterfacesGenerator,
private readonly unionGenerator: UnionGenerator,
private readonly identitiesGenerator: IdentitiesGenerator,
private readonly objectGenerator: ObjectGenerator
) {}

public getModelsCodeStructure(models: IModelsContainer): StatementStructures[] {
return [
Expand Down
6 changes: 4 additions & 2 deletions src/generators/models-generator/IdentitiesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import { TypeSerializer } from '../utils/TypeSerializer';
import { PropertiesGenerator } from './PropertiesGenerator';

export class IdentitiesGenerator {
private propertiesGenerator = new PropertiesGenerator();
constructor(
private readonly propertiesGenerator: PropertiesGenerator,
private readonly settings: IOptions
) {}

constructor(private settings: IOptions) {}
public getIdentities(identities: IIdentityModel[], interfaces: InterfaceModel[]): StatementStructures[] {
return identities.map(
(z): ClassDeclarationStructure => ({
Expand Down
2 changes: 1 addition & 1 deletion src/generators/models-generator/InterfacesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PropertiesGenerator } from './PropertiesGenerator';
type InterfacesStructures = InterfaceDeclarationStructure | TypeAliasDeclarationStructure;

export class InterfacesGenerator {
private propertiesGenerator = new PropertiesGenerator();
constructor(private readonly propertiesGenerator: PropertiesGenerator) {}

public getCodeStructure(interfaces: InterfaceModel[], unions: IUnionModel[]): InterfacesStructures[] {
const unionStructures = unions.flatMap((curr) => this.propertiesGenerator.getUnionTypeAliases(curr));
Expand Down
8 changes: 5 additions & 3 deletions src/generators/models-generator/ObjectGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { ClassDeclarationStructure, CodeBlockWriter, PropertyDeclarationStructur

import { PropertyKind } from '../../models/kinds/PropertyKind';
import { IExtendedObjectModel, IObjectPropertyModel, ObjectModel } from '../../models/ObjectModel';
import { NameService } from '../../swagger/nameService';
import { NameService } from '../../services/NameService';
import { FROM_DTO_METHOD, TO_DTO_METHOD } from '../ModelsGenerator';
import { ARRAY_STRING, NULL_STRING, UNDEFINED_STRING } from '../utils/consts';
import { TypeSerializer } from '../utils/TypeSerializer';
import { PropertiesGenerator } from './PropertiesGenerator';

export class ObjectGenerator {
private nameService = new NameService();
private propertiesGenerator = new PropertiesGenerator();
constructor(
private readonly nameService: NameService,
private readonly propertiesGenerator: PropertiesGenerator
) {}

public getObjects(objects: ObjectModel[]): ClassDeclarationStructure[] {
return objects.map((z) => ({
Expand Down
5 changes: 2 additions & 3 deletions src/generators/models-generator/PropertiesGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import {
import { IExtendedInterfaceModel, IInterfaceModel, IInterfacePropertyModel } from '../../models/InterfaceModel';
import { IObjectPropertyModel } from '../../models/ObjectModel';
import { IUnionModel } from '../../models/UnionModel';
import { NameService } from '../../swagger/nameService';
import { NameService } from '../../services/NameService';
import { lowerFirst } from '../../utils';
import { TypeSerializer } from '../utils/TypeSerializer';

export class PropertiesGenerator {
private nameService = new NameService();

constructor(private readonly nameService: NameService) {}
public getGuardProperty(name: string): PropertyDeclarationStructure {
return {
kind: StructureKind.Property,
Expand Down
5 changes: 3 additions & 2 deletions src/generators/models-generator/UnionsGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ClassDeclarationStructure, CodeBlockWriter, Scope, StructureKind } from 'ts-morph';

import { IUnionModel } from '../../models/UnionModel';
import { NameService } from '../../swagger/nameService';
import { NameService } from '../../services/NameService';
import { FROM_DTO_METHOD, TO_DTO_METHOD } from '../ModelsGenerator';

export class UnionGenerator {
private nameService = new NameService();
constructor(private readonly nameService: NameService) {}

public getUnionObjects(objects: IUnionModel[]): ClassDeclarationStructure[] {
return objects.map((z) => ({
kind: StructureKind.Class,
Expand Down
32 changes: 29 additions & 3 deletions src/gengen/GenGenCodeGenInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { UriBuilder } from '../services/UriBuilder';
import { OpenAPIService } from '../swagger/OpenAPIService';
import { OpenAPITypesGuard } from '../swagger/OpenAPITypesGuard';
import { IOpenAPI3 } from '../swagger/v3/open-api';
import { NameService } from '../services/NameService';
import { InterfacesGenerator } from '../generators/models-generator/InterfacesGenerator';
import { UnionGenerator } from '../generators/models-generator/UnionsGenerator';
import { IdentitiesGenerator } from '../generators/models-generator/IdentitiesGenerator';
import { ObjectGenerator } from '../generators/models-generator/ObjectGenerator';
import { PropertiesGenerator } from '../generators/models-generator/PropertiesGenerator';

export abstract class EndpointsToken {
public abstract getEndpoints(): Promise<Set<string>> | Set<string>;
Expand Down Expand Up @@ -61,7 +67,17 @@ export class GenGenCodeGenInjector {
)
.provide(OpenAPITypesGuard)
.provide(EndpointNameResolver)
.provide(ModelsGenerator, () => new ModelsGenerator(this.options))
.provide(
ModelsGenerator,
(x) =>
new ModelsGenerator(
this.options,
x.get(InterfacesGenerator),
x.get(UnionGenerator),
x.get(IdentitiesGenerator),
x.get(ObjectGenerator)
)
)
.provide(UriBuilder)
.provide(ServicesMethodGeneratorToken, (x) => new AngularServicesMethodGenerator(x.get(UriBuilder)))
.provide(AliasResolver, () => new AliasResolver(this.options))
Expand All @@ -85,10 +101,20 @@ export class GenGenCodeGenInjector {
)
.provide(
ModelMappingService,
(x) => new ModelMappingService(x.get(OpenAPIService), x.get(OpenAPITypesGuard), x.get(TypesService))
(x) => new ModelMappingService(x.get(OpenAPIService), x.get(OpenAPITypesGuard), x.get(TypesService), x.get(NameService))
)
.provide(TypesService, (x) => new TypesService(x.get(OpenAPITypesGuard), this.options))
.provide(OpenAPIService, (x) => new OpenAPIService(this.spec, x.get(OpenAPITypesGuard)))

.provide(InterfacesGenerator, (x) => new InterfacesGenerator(x.get(PropertiesGenerator)))
.provide(ObjectGenerator, (x) => new ObjectGenerator(x.get(NameService), x.get(PropertiesGenerator)))
.provide(UnionGenerator, (x) => new UnionGenerator(x.get(NameService)))
.provide(IdentitiesGenerator, (x) => new IdentitiesGenerator(x.get(PropertiesGenerator), this.options))
.provide(PropertiesGenerator, (x) => new PropertiesGenerator(x.get(NameService)))
.provide(NameService)
);
constructor(private options: IOptions, private spec: IOpenAPI3) {}
constructor(
private options: IOptions,
private spec: IOpenAPI3
) {}
}
6 changes: 3 additions & 3 deletions src/services/ModelMappingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PropertyKind } from '../models/kinds/PropertyKind';
import { IModelsContainer } from '../models/ModelsContainer';
import { IExtendedObjectModel, IObjectModel, IObjectPropertyModel, ObjectModel } from '../models/ObjectModel';
import { IUnionModel } from '../models/UnionModel';
import { NameService } from '../swagger/nameService';
import { NameService } from './NameService';
import { OpenAPIService } from '../swagger/OpenAPIService';
import { OpenAPITypesGuard } from '../swagger/OpenAPITypesGuard';
import { IOpenAPI3Reference } from '../swagger/v3/reference';
Expand All @@ -23,12 +23,12 @@ const IGNORE_PROPERTIES = ['startRow', 'rowCount'];
export class ModelMappingService {
public additionalEnums: IEnumModel[] = [];
public unions: IUnionModel[] = [];
private nameService = new NameService();

constructor(
private readonly openAPIService: OpenAPIService,
private readonly typesGuard: OpenAPITypesGuard,
private readonly typesService: TypesService
private readonly typesService: TypesService,
private readonly nameService: NameService
) {}

public toModelsContainer(schemas: OpenAPI3SchemaContainer): IModelsContainer {
Expand Down
File renamed without changes.

0 comments on commit e5f5367

Please sign in to comment.