From 15f0e4fa63bb1fd533091d9ab85d3612752c2d27 Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Wed, 6 Sep 2017 14:32:16 +0800 Subject: support set body parameter by json Rest task's body parameter is definited by json object in swagger definition. Issue-ID: SDC-67 Change-Id: Idb1d96cf9d1135afd7b285105abfdd9a2c50380c Signed-off-by: Lvbo163 --- .../app/services/swagger-tree-converter.service.ts | 157 +++++++++++++++++++++ .../src/app/services/workflow-config.service.ts | 9 +- 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts (limited to 'sdc-workflow-designer-ui/src/app/services') diff --git a/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts b/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts new file mode 100644 index 00000000..a83ae2e4 --- /dev/null +++ b/sdc-workflow-designer-ui/src/app/services/swagger-tree-converter.service.ts @@ -0,0 +1,157 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ + +import { Injectable } from '@angular/core'; +import { TreeNode } from 'primeng/primeng'; + +import { ValueSource } from '../model/value-source.enum'; +import { WorkflowUtil } from '../util/workflow-util'; +import { WorkflowConfigService } from "./workflow-config.service"; + +@Injectable() +export class SwaggerTreeConverterService { + private serviceName: string; + private serviceVersion: string; + + constructor(private configService: WorkflowConfigService) { + + } + + public schema2TreeNode(key: string | number, serviceName: string, serviceVersion: string, schema: any): any { + this.serviceName = serviceName; + this.serviceVersion = serviceVersion; + + if (schema.$ref) { + const treeNode = this.getTreeNodeBySwaggerDefinition(key, schema); + return treeNode; + } else { + this.setInitValue4Param(schema.value, schema); + return this.parameter2TreeNode(key, schema); + } + } + + private getTreeNodeBySwaggerDefinition(key: string | number, schema: any): TreeNode { + const swagger = this.configService.getSwaggerInfo(this.serviceName, this.serviceVersion); + if(swagger === undefined) { + console.log(`swagger definition not exist, [${this.serviceName}].[${this.serviceVersion}]`); + return null; + } + const swaggerDefinition = this.configService.getDefinition(swagger, schema.$ref); + + const definitionCopy = WorkflowUtil.deepClone(swaggerDefinition); + + this.setInitValue4Param(schema.value, definitionCopy); + if (schema.value !== definitionCopy.value) { + schema.value = definitionCopy.value; + } + + return this.schema2TreeNode(key, this.serviceName, this.serviceVersion, definitionCopy); + } + + private setInitValue4Param(value: any | any[], param: any) { + param.value = value; + if (value == null) { + if (param.type === 'object') { + param.value = {}; + } else if (param.type === 'array') { + param.value = []; + } else { // primary type + param.valueSource = ValueSource[ValueSource.String]; + } + } + } + + private parameter2TreeNode(name: string | number, paramDefinition: any): any { + const nodeType = this.getTreeNodeType(paramDefinition); + + const node = { + label: name, + type: nodeType, + children: [], + parameter: paramDefinition, + }; + + if (paramDefinition.type === 'object') { + node.children = this.getPropertyFromObject(paramDefinition.value, paramDefinition); + } else if (paramDefinition.type === 'array') { + this.setChildrenForArray(node, paramDefinition); + } + + return node; + } + + private getTreeNodeType(param: any): string { + const type = param.type; + if (type === 'array') { + return 'array'; + } else if (type === 'object') { + if (param.additionalProperties) { + return 'map'; + } else { + return 'object'; + } + } else { + return 'default'; + } + } + + private setChildrenForArray(node: any, param: any) { + param.value.forEach((itemValue, index) => { + const itemCopy = WorkflowUtil.deepClone(param.items); + itemCopy.value = itemValue; + node.children.push(this.schema2TreeNode(index, this.serviceName, this.serviceVersion, itemCopy)); + }); + } + + private getPropertyFromObject(objectValue: any, param: any): TreeNode[] { + if (param.properties) { + return this.getPropertyFromSimpleObject(objectValue, param.properties); + } else if (param.additionalProperties) { + return this.getPropertyFromMapOrDictionary(objectValue, param.additionalProperties); + } else { + return []; + } + + } + + private getPropertyFromSimpleObject(objectValue: any, properties: any): TreeNode[] { + const treeNodes: TreeNode[] = []; + for (const key in properties) { + const property = properties[key]; + this.setInitValue4Param(objectValue[key], property); + + if (property.value !== objectValue[key]) { + objectValue[key] = property.value; + } + + treeNodes.push(this.schema2TreeNode(key, this.serviceName, this.serviceVersion, property)); + } + return treeNodes; + } + + private getPropertyFromMapOrDictionary(mapOrDictionary: any, additionalProperties: any): TreeNode[] { + const treeNodes: TreeNode[] = []; + for (const key in mapOrDictionary) { + const propertyCopy = WorkflowUtil.deepClone(additionalProperties); + propertyCopy.value = mapOrDictionary[key]; + + const treeNode = this.schema2TreeNode(key, this.serviceName, this.serviceVersion, propertyCopy); + treeNode.keyEditable = true; + treeNodes.push(treeNode); + + if (mapOrDictionary[key] !== propertyCopy.value) { + mapOrDictionary[key] = propertyCopy.value; + } + } + return treeNodes; + } +} 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 469fde92..38d9fb5c 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 @@ -15,7 +15,7 @@ import { WorkflowService } from "./workflow.service"; import { Microservice } from "../model/workflow/microservice"; import { Observable } from "rxjs/Rx"; import { HttpService } from "../util/http.service"; -import { Swagger } from "../model/swagger"; +import { Swagger, SwaggerSchemaObject } from "../model/swagger"; /** * WorkflowConfigService @@ -46,4 +46,11 @@ export class WorkflowConfigService { return undefined; } } + + public getDefinition(swagger: Swagger, position: string): SwaggerSchemaObject { + const definitionName = position.substring('#/definitions/'.length); + + return swagger.definitions[definitionName]; + } + } -- cgit 1.2.3-korg