From e3aa95d1a2af759bb6e094d679737873d3092878 Mon Sep 17 00:00:00 2001 From: suxiaoxin Date: Tue, 6 Mar 2018 11:33:16 +0800 Subject: [PATCH] feat: json-schema swagger --- .../InterfaceList/InterfaceEditForm.js | 13 ++- .../Setting/ProjectData/ProjectData.js | 3 +- exts/yapi-plugin-import-swagger/client.js | 110 +++++++++++------- package-lock.json | 106 ++++++++++++++--- package.json | 1 + 5 files changed, 168 insertions(+), 65 deletions(-) diff --git a/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js b/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js index 6f0353d2..f6687bb1 100755 --- a/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js +++ b/client/containers/Project/Interface/InterfaceList/InterfaceEditForm.js @@ -22,14 +22,15 @@ function checkIsJsonSchema(json) { try { json = json5.parse(json); if (!json.type) return false; + if(json.properties && typeof json.properties === 'object'){ + if(!json.type)json.type = 'object'; + } + if(json.items && typeof json.items === 'object'){ + if(!json.type)json.type = 'array' + } + json.type = json.type.toLowerCase(); let types = ["object", "string", "number", "array", "boolean", "integer"]; if (types.indexOf(json.type) === -1) return false; - if (json.type === "object") { - if (!json.properties) return false; - } - if (json.type === "array") { - if (!json.items) return false; - } return JSON.stringify(json); } catch (e) { return false; diff --git a/client/containers/Project/Setting/ProjectData/ProjectData.js b/client/containers/Project/Setting/ProjectData/ProjectData.js index aaca2855..f3ce0fdf 100755 --- a/client/containers/Project/Setting/ProjectData/ProjectData.js +++ b/client/containers/Project/Setting/ProjectData/ProjectData.js @@ -205,8 +205,7 @@ class ProjectData extends Component { let reader = new FileReader(); reader.readAsText(info.file); reader.onload = async res => { - res = importDataModule[this.state.curImportType].run(res.target.result); - console.log('res',res); + res = await importDataModule[this.state.curImportType].run(res.target.result); if (this.state.dataSync) { // 开启同步 this.showConfirm(res); diff --git a/exts/yapi-plugin-import-swagger/client.js b/exts/yapi-plugin-import-swagger/client.js index f155f635..766a6084 100644 --- a/exts/yapi-plugin-import-swagger/client.js +++ b/exts/yapi-plugin-import-swagger/client.js @@ -1,11 +1,12 @@ import { message } from 'antd' import _ from 'underscore' -const deref = require('deref')() +const swagger = require('swagger-client') function improtData(importDataModule) { var SwaggerData, isOAS3; function handlePath(path) { + if(path === '/') return path; if (path.charAt(0) != "/") { path = "/" + path; } @@ -21,21 +22,25 @@ function improtData(importDataModule) { _.each(data.paths, (apis) => { _.each(apis, (api) => { _.each(api.responses, (res) => { - if (res.content) { - res.schema = res.content['application/json'].schema; + if (res.content && res.content['application/json'] && typeof res.content['application/json'] === 'object') { + Object.assign(res, res.content['application/json']) delete res.content; } }) if (api.requestBody) { if (!api.parameters) api.parameters = []; - api.parameters.push({ + let body = { type: 'object', name: 'body', - in: 'body', - schema: { - $ref: api.requestBody.content['application/json'].schema.$ref - } - }); + in: 'body' + } + try{ + body.schema = api.requestBody.content['application/json'].schema + }catch(e){ + body.schema = {} + } + + api.parameters.push(body); } }) }) @@ -43,7 +48,20 @@ function improtData(importDataModule) { return data; } - function run(res) { + async function handleSwaggerData(res){ + + return await new Promise(resolve=>{ + let data = swagger({ + spec: res + }) + data.then(res=>{ + console.log(res.spec) + resolve(res.spec) + }) + }) + } + + async function run(res) { try { let interfaceData = { apis: [], cats: [] }; res = JSON.parse(res); @@ -51,7 +69,7 @@ function improtData(importDataModule) { if (isOAS3) { res = openapi2swagger(res) } - + res = await handleSwaggerData(res); SwaggerData = res; if (res.tags && Array.isArray(res.tags)) { res.tags.forEach(tag => { @@ -87,7 +105,7 @@ function improtData(importDataModule) { }) }) - + console.log(interfaceData) return interfaceData; } catch (e) { @@ -112,7 +130,6 @@ function improtData(importDataModule) { api.req_query = []; api.req_body_type = 'raw'; api.res_body_type = 'raw'; - if (data.produces && data.produces.indexOf('application/json') > -1) { api.res_body_type = 'json'; @@ -192,9 +209,10 @@ function improtData(importDataModule) { } function handleBodyPamras(data, api) { - api.req_body_other = handleSchema(data); + api.req_body_other = JSON.stringify(data,null,2); if (isJson(api.req_body_other)) { api.req_body_type = 'json'; + api.req_body_is_json_schema = true; } } @@ -203,41 +221,49 @@ function improtData(importDataModule) { if (!api || typeof api !== 'object') { return res_body; } - _.each(api, (res, code) => { - if (/^2/.test(code)) { - if (res && typeof res === 'object') { + let codes = Object.keys(api); + let curCode; + if(codes.length > 0){ + if(codes.indexOf(200) > -1){ + curCode = 200; + }else curCode = codes[0] + let res = api[curCode]; + if (res && typeof res === 'object') { - if (res.schema) { - res_body = handleSchema(res.schema); - } else if (res.description) { - res_body = res.description; - } - } else if (typeof res === 'string') { - res_body = res; - } else { - res_body = ''; + if (res.schema) { + res_body = JSON.stringify(res.schema,null,2); + } else if (res.description) { + res_body = res.description; } + } else if (typeof res === 'string') { + res_body = res; + } else { + res_body = ''; } - }) + }else{ + res_body = '' + } return res_body; } - function handleSchema(data) { - if (!data) return data; - if (typeof data !== 'object') { - return data; - } + // function handleSchema(data) { + // // if (!data) return data; + // // if (typeof data !== 'object') { + // // return data; + // // } + - try { - // data.definitions = SwaggerData.definitions; - isOAS3 ? data.components = SwaggerData.components : data.definitions = SwaggerData.definitions - let schema = deref(data, true); - let jsfData = JSON.stringify(schema, null, 2); - return jsfData; - } catch (e) { - return ''; - } - } + // // try { + // // // data.definitions = SwaggerData.definitions; + // // isOAS3 ? data.components = SwaggerData.components : data.definitions = SwaggerData.definitions + // // let schema = deref(data, true); + // // let jsfData = JSON.stringify(schema, null, 2); + // // return jsfData; + // // } catch (e) { + // // return ''; + // // } + // return data; + // } if (!importDataModule || typeof importDataModule !== 'object') { console.error('importDataModule 参数Must be Object Type'); diff --git a/package-lock.json b/package-lock.json index 83696bbd..db876a95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2153,7 +2153,6 @@ "version": "6.26.0", "resolved": "http://registry.npm.taobao.org/babel-runtime/download/babel-runtime-6.26.0.tgz", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", - "dev": true, "requires": { "core-js": "2.5.3", "regenerator-runtime": "0.11.1" @@ -2635,6 +2634,11 @@ "resolved": "http://registry.npm.taobao.org/bson/download/bson-0.5.7.tgz", "integrity": "sha1-DRH+CTbB/uAp4R9wY/XQqyQi6j4=" }, + "btoa": { + "version": "1.1.2", + "resolved": "http://registry.npm.taobao.org/btoa/download/btoa-1.1.2.tgz", + "integrity": "sha1-PkC4FmP4HS3WWWpMtxSo3BbPq+A=" + }, "buf-compare": { "version": "1.0.1", "resolved": "http://registry.npm.taobao.org/buf-compare/download/buf-compare-1.0.1.tgz", @@ -3798,8 +3802,7 @@ "cookie": { "version": "0.3.1", "resolved": "http://registry.npm.taobao.org/cookie/download/cookie-0.3.1.tgz", - "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=", - "dev": true + "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" }, "cookie-signature": { "version": "1.0.6", @@ -3955,8 +3958,7 @@ "core-js": { "version": "2.5.3", "resolved": "http://registry.npm.taobao.org/core-js/download/core-js-2.5.3.tgz", - "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=", - "dev": true + "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=" }, "core-util-is": { "version": "1.0.2", @@ -4061,6 +4063,15 @@ } } }, + "cross-fetch": { + "version": "0.0.8", + "resolved": "http://registry.npm.taobao.org/cross-fetch/download/cross-fetch-0.0.8.tgz", + "integrity": "sha1-Ae2U3EB98sAPGAf95wCnz6SKIFw=", + "requires": { + "node-fetch": "1.7.3", + "whatwg-fetch": "2.0.3" + } + }, "cross-spawn": { "version": "5.1.0", "resolved": "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-5.1.0.tgz", @@ -5045,6 +5056,11 @@ "core-js": "2.5.3" } }, + "encode-3986": { + "version": "1.0.0", + "resolved": "http://registry.npm.taobao.org/encode-3986/download/encode-3986-1.0.0.tgz", + "integrity": "sha1-lA1RSY+HQa3hhLda0UObMXwMemA=" + }, "encodeurl": { "version": "1.0.2", "resolved": "http://registry.npm.taobao.org/encodeurl/download/encodeurl-1.0.2.tgz", @@ -5055,7 +5071,6 @@ "version": "0.1.12", "resolved": "http://registry.npm.taobao.org/encoding/download/encoding-0.1.12.tgz", "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", - "dev": true, "requires": { "iconv-lite": "0.4.13" } @@ -6141,6 +6156,14 @@ "integrity": "sha1-S2LEK44D3j+EhGC2OQeZIGldAVQ=", "dev": true }, + "fast-json-patch": { + "version": "2.0.6", + "resolved": "http://registry.npm.taobao.org/fast-json-patch/download/fast-json-patch-2.0.6.tgz", + "integrity": "sha1-hv/4+GYjkaqBlyKGTWMuYD5u5gU=", + "requires": { + "deep-equal": "1.0.1" + } + }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "http://registry.npm.taobao.org/fast-json-stable-stringify/download/fast-json-stable-stringify-2.0.0.tgz", @@ -9215,8 +9238,7 @@ "is-stream": { "version": "1.1.0", "resolved": "http://registry.npm.taobao.org/is-stream/download/is-stream-1.1.0.tgz", - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", - "dev": true + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" }, "is-svg": { "version": "2.1.0", @@ -9305,6 +9327,26 @@ "whatwg-fetch": "2.0.3" } }, + "isomorphic-form-data": { + "version": "0.0.1", + "resolved": "http://registry.npm.taobao.org/isomorphic-form-data/download/isomorphic-form-data-0.0.1.tgz", + "integrity": "sha1-Am9ifgMrDNhBPsyHVZKLlKRosGI=", + "requires": { + "form-data": "1.0.1" + }, + "dependencies": { + "form-data": { + "version": "1.0.1", + "resolved": "http://registry.npm.taobao.org/form-data/download/form-data-1.0.1.tgz", + "integrity": "sha1-rjFduaSQf6BlUCMEpm13M0de43w=", + "requires": { + "async": "2.1.2", + "combined-stream": "1.0.6", + "mime-types": "2.1.18" + } + } + } + }, "isstream": { "version": "0.1.2", "resolved": "http://registry.npm.taobao.org/isstream/download/isstream-0.1.2.tgz", @@ -12265,7 +12307,6 @@ "version": "1.7.3", "resolved": "http://registry.npm.taobao.org/node-fetch/download/node-fetch-1.7.3.tgz", "integrity": "sha1-mA9vcthSEaU0fGsrwYxbhMPrR+8=", - "dev": true, "requires": { "encoding": "0.1.12", "is-stream": "1.1.0" @@ -18209,8 +18250,7 @@ "regenerator-runtime": { "version": "0.11.1", "resolved": "http://registry.npm.taobao.org/regenerator-runtime/download/regenerator-runtime-0.11.1.tgz", - "integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk=", - "dev": true + "integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk=" }, "regenerator-transform": { "version": "0.10.1", @@ -20079,6 +20119,39 @@ "serviceworker-cache-polyfill": "4.0.0" } }, + "swagger-client": { + "version": "3.5.1", + "resolved": "http://registry.npm.taobao.org/swagger-client/download/swagger-client-3.5.1.tgz", + "integrity": "sha1-OJqOvJBgL/h73uUp06P2sdaYAgM=", + "requires": { + "babel-runtime": "6.26.0", + "btoa": "1.1.2", + "cookie": "0.3.1", + "cross-fetch": "0.0.8", + "deep-extend": "0.4.2", + "encode-3986": "1.0.0", + "fast-json-patch": "2.0.6", + "isomorphic-form-data": "0.0.1", + "js-yaml": "3.10.0", + "lodash": "4.17.5", + "qs": "6.5.1", + "url": "0.11.0", + "utf8-bytes": "0.0.1", + "utfstring": "2.0.0" + }, + "dependencies": { + "deep-extend": { + "version": "0.4.2", + "resolved": "http://registry.npm.taobao.org/deep-extend/download/deep-extend-0.4.2.tgz", + "integrity": "sha1-SLaZwn4zS/ifEIkr5DL25MfTSn8=" + }, + "qs": { + "version": "6.5.1", + "resolved": "http://registry.npm.taobao.org/qs/download/qs-6.5.1.tgz", + "integrity": "sha1-NJzfbu+J7EXBLX1es/wMhwNDptg=" + } + } + }, "symbol-observable": { "version": "0.2.4", "resolved": "http://registry.npm.taobao.org/symbol-observable/download/symbol-observable-0.2.4.tgz", @@ -21201,8 +21274,12 @@ "utf8-bytes": { "version": "0.0.1", "resolved": "http://registry.npm.taobao.org/utf8-bytes/download/utf8-bytes-0.0.1.tgz", - "integrity": "sha1-EWsCVEjJtQAIHN+/H01sbDfYg30=", - "dev": true + "integrity": "sha1-EWsCVEjJtQAIHN+/H01sbDfYg30=" + }, + "utfstring": { + "version": "2.0.0", + "resolved": "http://registry.npm.taobao.org/utfstring/download/utfstring-2.0.0.tgz", + "integrity": "sha1-szH3NR6b4cRjNMx1GIJs2jtEJCo=" }, "util": { "version": "0.10.3", @@ -22017,8 +22094,7 @@ "whatwg-fetch": { "version": "2.0.3", "resolved": "http://registry.npm.taobao.org/whatwg-fetch/download/whatwg-fetch-2.0.3.tgz", - "integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ=", - "dev": true + "integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ=" }, "whatwg-url": { "version": "4.8.0", diff --git a/package.json b/package.json index 9c9f4442..80cca0c8 100755 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "request": "2.81.0", "sha.js": "2.4.9", "sha1": "1.1.1", + "swagger-client": "^3.5.1", "tslib": "1.8.0", "underscore": "1.8.3", "url": "0.11.0",