From 76f9fa54825d98013182524d16290f04e3c9398a Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Mon, 4 Sep 2017 09:32:08 +0800 Subject: [PATCH] support swagger for microservice definition microservice definition follow swagger specification. Issue-ID: SDC-271 Change-Id: I300732cbdc56791981b1aa626522c0f6b0b2a4c7 Signed-off-by: Lvbo163 --- .../microservice-detail.component.ts | 20 +- .../microservice-list.component.ts | 2 +- sdc-workflow-designer-ui/src/app/model/swagger.ts | 255 +++++++++++++++++++++ .../src/app/model/workflow/microservice.ts | 3 +- .../src/app/services/workflow-config.service.ts | 2 +- 5 files changed, 272 insertions(+), 10 deletions(-) create mode 100644 sdc-workflow-designer-ui/src/app/model/swagger.ts diff --git a/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-detail/microservice-detail.component.ts b/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-detail/microservice-detail.component.ts index 90b582c9..3e472175 100644 --- a/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-detail/microservice-detail.component.ts +++ b/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-detail/microservice-detail.component.ts @@ -15,6 +15,7 @@ import { ModalDirective } from 'ngx-bootstrap/modal'; import { Microservice } from '../../../../model/workflow/microservice'; import { WorkflowConfigService } from '../../../../services/workflow-config.service'; +import { Swagger } from "../../../../model/swagger"; /** * toolbar component contains some basic operations(save) and all of the supported workflow nodes. @@ -35,7 +36,7 @@ export class MicroserviceDetailComponent implements OnChanges { public ngOnChanges() { if(this.microservice == null) { - this.microservice = new Microservice('', '', {}, ''); + this.microservice = new Microservice('', '', null, ''); } this.dynamic = this.microservice.definition !== ''; this.parseSwagger2String(); @@ -51,10 +52,15 @@ export class MicroserviceDetailComponent implements OnChanges { public onDetailChanged(detail: string) { try { - const swagger = JSON.parse(detail); - this.detail = detail; - console.log(swagger); - this.microservice.swagger = swagger; + if(detail) { + const swagger = new Swagger(JSON.parse(detail)); + this.detail = detail; + console.log(swagger); + this.microservice.swagger = swagger; + } else { + this.detail = ''; + this.microservice.swagger = null; + } } catch (e) { // if detail is not a json object, then not change the swagger } @@ -62,7 +68,7 @@ export class MicroserviceDetailComponent implements OnChanges { public toggleDynamic(dynamic: boolean) { this.dynamic = dynamic; - this.onDetailChanged('{}'); + this.onDetailChanged(null); if(!dynamic) { this.microservice.definition = null; @@ -73,7 +79,7 @@ export class MicroserviceDetailComponent implements OnChanges { this.configService.loadDynamicInfo(this.microservice.definition) .subscribe(response => { try { - this.microservice.swagger = response; + this.microservice.swagger = new Swagger(response); this.parseSwagger2String(); } catch (e) { console.log('detail transfer error'); diff --git a/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-list/microservice-list.component.ts b/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-list/microservice-list.component.ts index 6dcfe8c6..6e31b2e4 100644 --- a/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-list/microservice-list.component.ts +++ b/sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-list/microservice-list.component.ts @@ -32,7 +32,7 @@ export class MicroserviceListComponent { } public addMicroservice() { - const microservice = new Microservice('new microservice', '', {}, ''); + const microservice = new Microservice('new microservice', '', null, ''); this.microservices.push(microservice); this.onMicroserviceSelected(microservice); diff --git a/sdc-workflow-designer-ui/src/app/model/swagger.ts b/sdc-workflow-designer-ui/src/app/model/swagger.ts new file mode 100644 index 00000000..e90c5e9b --- /dev/null +++ b/sdc-workflow-designer-ui/src/app/model/swagger.ts @@ -0,0 +1,255 @@ +/******************************************************************************* + * Copyright (c) 2017 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial API and implementation and/or initial documentation + *******************************************************************************/ + +export class SwaggerParameter { + public description: string; + public position: string; // in path, query, header, body, form + public name: string; + public required: boolean; + public type: string; + + // if position is body + public schema: SwaggerSchemaObject; + + constructor(options: any) { + this.description = options.description; + this.position = options.in; + this.name = options.name; + this.required = options.required; + this.type = options.type; + if (this.position === 'body') { + this.schema = getSchemaObject(options.schema); + } + } +} + +export class SwaggerHeader { + public description: string; + + constructor(options: any) { + this.description = options.description; + } +} + +export class SwaggerResponse { + public description: string; + public schema: SwaggerSchemaObject; + public headers: any; + + constructor({description, schema, headers}) { + this.description = description; + + if (schema) { + this.schema = getSchemaObject(schema); + } + + if (headers) { + this.headers = {}; + for (const key in headers) { + this.headers[key] = new SwaggerHeader(headers[key]); + } + } + } +} + +export class SwaggerMethod { + public consumes: string[]; + public description: string; + public operationId: string; + public parameters: SwaggerParameter[]; + public produces: string[]; + public responses: any; + public summary: string; + public tags: string[]; + + constructor({ consumes, description, operationId, parameters, produces, responses, summary, tags }) { + this.consumes = consumes; + this.description = description; + this.operationId = operationId; + this.parameters = parameters.map(param => new SwaggerParameter(param)); + this.produces = produces; + this.responses = this.initResponses(responses); + this.summary = summary; + this.tags = tags; + } + + private initResponses(responses: any): any { + const responseObjs = {}; + for (const key in responses) { + responseObjs[key] = new SwaggerResponse(responses[key]); + } + + return responseObjs; + } +} + +export class SwaggerInfo { + public title: string; + public version: string; + + constructor({ title, version }) { + this.title = title; + this.version = version; + } +} + +export class SwaggerTag { + public name: string; + + constructor({name}) { + this.name = name; + } +} + +export class Swagger { + public basePath: string; + public definitions: any; + public info: SwaggerInfo; + public paths: any; + public swagger: string; + public tags: SwaggerTag[]; + + constructor({basePath, definitions, info, paths, swagger, tags}) { + this.basePath = basePath; + this.definitions = this.initDefinitions(definitions); + this.info = new SwaggerInfo(info); + this.paths = this.initPaths(paths); + this.swagger = swagger; + this.tags = tags.map(tag => new SwaggerTag(tag)); + } + + private initPaths(paths: any): any { + const pathObjs = {}; + for (const key in paths) { + pathObjs[key] = this.initPath(paths[key]); + } + return pathObjs; + } + + private initPath(path: any): any { + const pathObj = {}; + + for (const key in path) { + pathObj[key] = new SwaggerMethod(path[key]); + } + + return pathObj; + } + + private initDefinitions(definitions: any): any { + const definitionObjs = {}; + for (const key in definitions) { + definitionObjs[key] = getSchemaObject(definitions[key]); + } + return definitionObjs; + } +} + +export function getSchemaObject(definition: any) { + if (definition.$ref) { + return new SwaggerReferenceObject(definition); + } else if (definition.type === 'array') { + return new SwaggerModelArray(definition); + } else if (definition.type === 'object') { + if (definition.properties) { + return new SwaggerModelSimple(definition); + } else if (definition.additionalProperties) { + return new SwaggerModelMap(definition); + } else { + return new SwaggerModel(); + } + } else { + return new SwaggerPrimitiveObject(definition); + } +} + +export class SwaggerSchemaObject { + +} + +export class SwaggerReferenceObject extends SwaggerSchemaObject { + public $ref: string; + + constructor({ $ref }) { + super(); + this.$ref = $ref; + } +} + +export class SwaggerPrimitiveObject extends SwaggerSchemaObject { + public collectionFormat: string; + public defaultValue: any; + public enumValues: any[]; + public exclusiveMaximum: boolean; + public exclusiveMinimum: boolean; + public format: string; + public maximum: number; + public maxLength: number; + public minimum: number; + public minLength: number; + public multipleOf: number; + public pattern: string; + public type: string; + + constructor(options: any) { + super(); + this.collectionFormat = options.collectionFormat; + this.defaultValue = options.default; + this.enumValues = options.enum; + this.exclusiveMaximum = options.exclusiveMaximum; + this.exclusiveMinimum = options.exclusiveMinimum; + this.format = options.format; + this.maximum = options.maximum; + this.maxLength = options.maxLength; + this.minimum = options.minimum; + this.minLength = options.minLength; + this.multipleOf = options.multipleOf; + this.pattern = options.pattern; + this.type = options.type; + } +} + +export class SwaggerModel extends SwaggerSchemaObject { + public type = 'object'; +} + +export class SwaggerModelSimple extends SwaggerModel { + public properties = {}; + public required = []; + + constructor(options: any) { + super(); + this.required = options.required; + for (const key in options.properties) { + this.properties[key] = getSchemaObject(options.properties[key]); + } + } +} + +export class SwaggerModelMap extends SwaggerModel { + public additionalProperties: SwaggerSchemaObject; + + constructor(options: any) { + super(); + this.additionalProperties = getSchemaObject(options.additionalProperties); + } +} + +export class SwaggerModelArray extends SwaggerSchemaObject { + public type = 'array'; + public items: SwaggerSchemaObject; + + constructor(options: any) { + super(); + this.items = getSchemaObject(options.items); + } +} diff --git a/sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts b/sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts index 025f7a10..b4fc1c10 100644 --- a/sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts +++ b/sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts @@ -11,10 +11,11 @@ */ import { Injectable } from '@angular/core'; +import { Swagger } from "../swagger"; /** * Microservice */ export class Microservice { - constructor(public name: string, public version: string, public swagger: any, public definition: string) {} + constructor(public name: string, public version: string, public swagger: Swagger, public definition: string) {} } diff --git a/sdc-workflow-designer-ui/src/app/services/workflow-config.service.ts b/sdc-workflow-designer-ui/src/app/services/workflow-config.service.ts index 2d2ca00b..00a2b9ef 100644 --- a/sdc-workflow-designer-ui/src/app/services/workflow-config.service.ts +++ b/sdc-workflow-designer-ui/src/app/services/workflow-config.service.ts @@ -34,6 +34,6 @@ export class WorkflowConfigService { Accept: 'application/json', }, }; - return this.httpService.get(url); + return this.httpService.get(url).map(response => response.data); } } -- 2.16.6