Skip to content

Commit

Permalink
Merge pull request #121 from Luxoft/develop
Browse files Browse the repository at this point in the history
Release candidate 13
  • Loading branch information
sevaru authored Aug 31, 2022
2 parents 93388b2 + e5394b5 commit 8cc9d0f
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 99 deletions.
14 changes: 14 additions & 0 deletions .snapshot/all/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ export class ProductService extends DownloadFileService {
).pipe($mappers.mapCollection($models.Product));
}

public itemsGetById(parameter: string): Observable<$models.Product[]> {
return this.post<$models.IProduct[]>(
`Items/GetById`,
parameter,
).pipe($mappers.mapCollection($models.Product));
}

public itemsGetByIds(parameter: string[]): Observable<$models.Product[]> {
return this.post<$models.IProduct[]>(
`Items/GetByIds`,
parameter,
).pipe($mappers.mapCollection($models.Product));
}

public product(): Observable<$models.Product[]> {
return this.get<$models.IProduct[]>(
`Product`,
Expand Down
16 changes: 16 additions & 0 deletions .snapshot/withRequestOptions/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ export class ProductService extends DownloadFileService {
).pipe($mappers.mapCollection($models.Product));
}

public itemsGetById(parameter: string, options?: $types.TypeOrUndefined<IAngularHttpRequestOptions>): Observable<$models.Product[]> {
return this.post<$models.IProduct[]>(
`Items/GetById`,
parameter,
options,
).pipe($mappers.mapCollection($models.Product));
}

public itemsGetByIds(parameter: string[], options?: $types.TypeOrUndefined<IAngularHttpRequestOptions>): Observable<$models.Product[]> {
return this.post<$models.IProduct[]>(
`Items/GetByIds`,
parameter,
options,
).pipe($mappers.mapCollection($models.Product));
}

public product(options?: $types.TypeOrUndefined<IAngularHttpRequestOptions>): Observable<$models.Product[]> {
return this.get<$models.IProduct[]>(
`Product`,
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.

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.0.0-rc.12",
"version": "1.0.0-rc.13",
"description": "Tool for generating models and Angular services based on OpenAPIs and Swagger's JSON",
"bin": {
"gengen": "./bin/index.js"
Expand Down
8 changes: 4 additions & 4 deletions src/generators/angular/AngularServicesMethodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IBodyParameter } from '../../models/method-parameter/IBodyParameter';
import { IMethodModel } from '../../models/method-parameter/IMethodModel';
import { IPathParameter } from '../../models/method-parameter/IPathParameter';
import { IQueryParameter } from '../../models/method-parameter/IQueryParameter';
import { IReturnType } from '../../models/method-parameter/IReturnType';
import { ITypeInfo } from '../../models/method-parameter/ITypeInfo';
import { UriBuilder } from '../../services/UriBuilder';
import { MAPPERS_NAMESPACE, MODELS_NAMESPACE, UNDEFINED_STRING } from '../utils/consts';
import { TypeSerializer } from '../utils/TypeSerializer';
Expand Down Expand Up @@ -84,7 +84,7 @@ export class AngularServicesMethodGenerator {
return statement;
}

protected getReturnTypeName(returnType: IReturnType | undefined, targetType: string | undefined): string {
protected getReturnTypeName(returnType: ITypeInfo | undefined, targetType: string | undefined): string {
if (!returnType || !targetType) {
return 'void';
}
Expand All @@ -107,14 +107,14 @@ export class AngularServicesMethodGenerator {
}).toString();
}

protected needPipe(returnType: IReturnType | undefined): returnType is IReturnType {
protected needPipe(returnType: ITypeInfo | undefined): returnType is ITypeInfo {
if (!returnType) {
return false;
}

return [PropertyKind.Object, PropertyKind.Identity, PropertyKind.Guid, PropertyKind.Date].includes(returnType.type.kind);
}
protected createPipe(returnType: IReturnType): string {
protected createPipe(returnType: ITypeInfo): string {
if (returnType.type.kind === PropertyKind.Guid) {
return `${MAPPERS_NAMESPACE}.mapGuid()`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/method-parameter/IMethodModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MethodOperation } from '../kinds/MethodOperation';
import { IBodyParameter } from './IBodyParameter';
import { IPathParameter } from './IPathParameter';
import { IQueryParameter } from './IQueryParameter';
import { IReturnType } from './IReturnType';
import { ITypeInfo } from './ITypeInfo';

export type MethodParameter = IPathParameter | IQueryParameter | IBodyParameter;

Expand All @@ -12,6 +12,6 @@ export interface IMethodModel {
operation: MethodOperation;
name: string;
parameters: MethodParameter[];
returnType: IReturnType | undefined;
returnType: ITypeInfo | undefined;
originUri: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IType } from '../TypeModel';

export interface IReturnType {
export interface ITypeInfo {
isCollection: boolean;
type: IType;
isModel: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/models/method-parameter/MethodParameterModelBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export abstract class MethodParameterModelBase implements IParameter {
if (this.typesGuard.isSimple(this.model.schema)) {
this.setupSimple(this.model.schema);
return;
}
}

if (this.typesGuard.isEnum(this.openAPIService.getRefSchema(this.model.schema))) {
this.setupRef(this.model.schema);
return;
Expand Down
5 changes: 3 additions & 2 deletions src/services/EndpointsConfigReader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { EndpointsToken } from '../gengen/GenGenCodeGenInjector';
import { configOptions, IOptions } from '../options';

export class EndpointsConfigReader {
constructor(private readonly options: IOptions) { }
export class EndpointsConfigReader implements EndpointsToken {
constructor(private readonly options: IOptions) {}

public async getEndpoints(): Promise<Set<string>> {
require('ts-node').register({
Expand Down
30 changes: 30 additions & 0 deletions src/services/ModelFinder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PropertyKind } from '../models/kinds/PropertyKind';
import { IModelsContainer } from '../models/ModelsContainer';
import { OpenAPIService } from '../swagger/OpenAPIService';
import { IOpenAPI3Reference } from '../swagger/v3/reference';
import { IModel } from './ServiceMappingService';

export class ModelFinder {
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 };
}

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

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

return undefined;
}
}
61 changes: 17 additions & 44 deletions src/services/ServiceMappingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IBodyParameter } from '../models/method-parameter/IBodyParameter';
import { IMethodModel } from '../models/method-parameter/IMethodModel';
import { IPathParameter } from '../models/method-parameter/IPathParameter';
import { IQueryParameter } from '../models/method-parameter/IQueryParameter';
import { IReturnType } from '../models/method-parameter/IReturnType';
import { ITypeInfo } from '../models/method-parameter/ITypeInfo';
import { PathMethodParameterModel } from '../models/method-parameter/PathMethodParameterModel';
import { QueryMethodParameterModel } from '../models/method-parameter/QueryMethodParameterModel';
import { IModelsContainer } from '../models/ModelsContainer';
Expand All @@ -23,9 +23,10 @@ import { OpenAPI3Schema } from '../swagger/v3/schemas/schema';
import { first, lowerFirst, sortBy } from '../utils';
import { EndpointNameResolver } from './EndpointNameResolver';
import { EndpointsService, IEndpointInfo } from './EndpointsService';
import { ModelFinder } from './ModelFinder';
import { TypesService } from './TypesService';

interface IModel {
export interface IModel {
name: string;
dtoType: string;
kind: PropertyKind;
Expand Down Expand Up @@ -101,16 +102,17 @@ export class ServiceMappingService {
models: IModelsContainer,
originUri: string
): IMethodModel {
const modelFinder = new ModelFinder(this.openAPIService, models);
const model: IMethodModel = {
kind: this.hasDownloadResponse(operation) ? MethodKind.Download : MethodKind.Default,
name: lowerFirst(actionName),
operation: method,
parameters: this.getUriParameters(operation.parameters),
returnType: this.getReturnType(operation.responses[200].content?.['application/json']?.schema, models),
returnType: this.getTypeInfo(operation.responses[200].content?.['application/json']?.schema, modelFinder),
originUri
};

const bodyParameter = this.getBodyParameter(operation.requestBody?.content['application/json']?.schema, models);
const bodyParameter = this.getBodyParameter(operation.requestBody?.content['application/json']?.schema, modelFinder);
if (bodyParameter) {
model.parameters.push(bodyParameter);
}
Expand Down Expand Up @@ -175,32 +177,24 @@ export class ServiceMappingService {

private getBodyParameter(
schema: IOpenAPI3ArraySchema | IOpenAPI3Reference | undefined,
models: IModelsContainer
modelFinder: ModelFinder
): IBodyParameter | undefined {
let model: IModel | undefined;
let isCollection = false;
if (this.typesGuard.isReference(schema)) {
model = this.findModel(models, schema);
} else if (this.typesGuard.isCollection(schema) && this.typesGuard.isReference(schema.items)) {
isCollection = true;
model = this.findModel(models, schema.items);
}

if (!model) {
const typeInfo = this.getTypeInfo(schema, modelFinder);
if (!typeInfo) {
return undefined;
}

return {
name: lowerFirst(model.name),
place: ParameterPlace.Body,
dtoType: typeInfo.type.dtoType,
isCollection: typeInfo.isCollection,
isModel: typeInfo.isModel,
optional: false,
dtoType: model.dtoType,
isCollection,
isModel: true
place: ParameterPlace.Body,
name: typeInfo.isModel ? lowerFirst(typeInfo.type.type) : 'parameter'
};
}

private getReturnType(schema: OpenAPI3Schema | undefined, models: IModelsContainer): IReturnType | undefined {
private getTypeInfo(schema: OpenAPI3Schema | undefined, modelFinder: ModelFinder): ITypeInfo | undefined {
let model: IModel | undefined;
let isCollection = false;

Expand All @@ -209,7 +203,7 @@ export class ServiceMappingService {
}

if (this.typesGuard.isReference(schema)) {
model = this.findModel(models, schema);
model = modelFinder.find(schema);
} else if (this.typesGuard.isCollection(schema)) {
isCollection = true;

Expand All @@ -218,7 +212,7 @@ export class ServiceMappingService {
}

if (this.typesGuard.isReference(schema.items)) {
model = this.findModel(models, schema.items);
model = modelFinder.find(schema.items);
}
}

Expand All @@ -232,25 +226,4 @@ export class ServiceMappingService {
private hasDownloadResponse(operation: IOpenAPI3Operation): boolean {
return Boolean(operation.responses[200].content?.['application/octet-stream']);
}

private findModel(models: IModelsContainer, ref: IOpenAPI3Reference): IModel | undefined {
const name = this.openAPIService.getSchemaKey(ref);

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

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

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

return undefined;
}
}
Loading

0 comments on commit 8cc9d0f

Please sign in to comment.