support swagger for microservice definition 11/10111/2
authorLvbo163 <lv.bo163@zte.com.cn>
Mon, 4 Sep 2017 01:32:08 +0000 (09:32 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Mon, 4 Sep 2017 07:16:37 +0000 (15:16 +0800)
microservice definition follow swagger specification.

Issue-ID: SDC-271

Change-Id: I300732cbdc56791981b1aa626522c0f6b0b2a4c7
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-detail/microservice-detail.component.ts
sdc-workflow-designer-ui/src/app/components/menu/microservice/microservice-list/microservice-list.component.ts
sdc-workflow-designer-ui/src/app/model/swagger.ts [new file with mode: 0644]
sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts
sdc-workflow-designer-ui/src/app/services/workflow-config.service.ts

index 90b582c..3e47217 100644 (file)
@@ -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');
index 6dcfe8c..6e31b2e 100644 (file)
@@ -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 (file)
index 0000000..e90c5e9
--- /dev/null
@@ -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);
+    }
+}
index 025f7a1..b4fc1c1 100644 (file)
  */\r
 \r
 import { Injectable } from '@angular/core';\r
+import { Swagger } from "../swagger";\r
 \r
 /**\r
  * Microservice\r
  */\r
 export class Microservice {\r
-    constructor(public name: string, public version: string, public swagger: any, public definition: string) {}\r
+    constructor(public name: string, public version: string, public swagger: Swagger, public definition: string) {}\r
 }\r
index 2d2ca00..00a2b9e 100644 (file)
@@ -34,6 +34,6 @@ export class WorkflowConfigService {
                 Accept: 'application/json',\r
             },\r
         };\r
-        return this.httpService.get(url);\r
+        return this.httpService.get(url).map(response => response.data);\r
     }\r
 }\r