diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2017-09-04 09:32:08 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2017-09-04 15:16:37 +0800 |
commit | 76f9fa54825d98013182524d16290f04e3c9398a (patch) | |
tree | 99d46a4107b0c037a398ec34aac7561690be0ba0 /sdc-workflow-designer-ui/src/app/model | |
parent | c0d3e994ef109b4b0e089187f1db397ff3306613 (diff) |
support swagger for microservice definition
microservice definition follow swagger specification.
Issue-ID: SDC-271
Change-Id: I300732cbdc56791981b1aa626522c0f6b0b2a4c7
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
Diffstat (limited to 'sdc-workflow-designer-ui/src/app/model')
-rw-r--r-- | sdc-workflow-designer-ui/src/app/model/swagger.ts | 255 | ||||
-rw-r--r-- | sdc-workflow-designer-ui/src/app/model/workflow/microservice.ts | 3 |
2 files changed, 257 insertions, 1 deletions
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) {}
}
|