feat: json-schema swagger

This commit is contained in:
suxiaoxin 2018-03-06 11:33:16 +08:00
parent 6fd0d8206a
commit e3aa95d1a2
5 changed files with 168 additions and 65 deletions

View File

@ -22,14 +22,15 @@ function checkIsJsonSchema(json) {
try { try {
json = json5.parse(json); json = json5.parse(json);
if (!json.type) return false; 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"]; let types = ["object", "string", "number", "array", "boolean", "integer"];
if (types.indexOf(json.type) === -1) return false; 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); return JSON.stringify(json);
} catch (e) { } catch (e) {
return false; return false;

View File

@ -205,8 +205,7 @@ class ProjectData extends Component {
let reader = new FileReader(); let reader = new FileReader();
reader.readAsText(info.file); reader.readAsText(info.file);
reader.onload = async res => { reader.onload = async res => {
res = importDataModule[this.state.curImportType].run(res.target.result); res = await importDataModule[this.state.curImportType].run(res.target.result);
console.log('res',res);
if (this.state.dataSync) { if (this.state.dataSync) {
// 开启同步 // 开启同步
this.showConfirm(res); this.showConfirm(res);

View File

@ -1,11 +1,12 @@
import { message } from 'antd' import { message } from 'antd'
import _ from 'underscore' import _ from 'underscore'
const deref = require('deref')() const swagger = require('swagger-client')
function improtData(importDataModule) { function improtData(importDataModule) {
var SwaggerData, isOAS3; var SwaggerData, isOAS3;
function handlePath(path) { function handlePath(path) {
if(path === '/') return path;
if (path.charAt(0) != "/") { if (path.charAt(0) != "/") {
path = "/" + path; path = "/" + path;
} }
@ -21,21 +22,25 @@ function improtData(importDataModule) {
_.each(data.paths, (apis) => { _.each(data.paths, (apis) => {
_.each(apis, (api) => { _.each(apis, (api) => {
_.each(api.responses, (res) => { _.each(api.responses, (res) => {
if (res.content) { if (res.content && res.content['application/json'] && typeof res.content['application/json'] === 'object') {
res.schema = res.content['application/json'].schema; Object.assign(res, res.content['application/json'])
delete res.content; delete res.content;
} }
}) })
if (api.requestBody) { if (api.requestBody) {
if (!api.parameters) api.parameters = []; if (!api.parameters) api.parameters = [];
api.parameters.push({ let body = {
type: 'object', type: 'object',
name: 'body', name: 'body',
in: 'body', in: 'body'
schema: { }
$ref: api.requestBody.content['application/json'].schema.$ref 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; 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 { try {
let interfaceData = { apis: [], cats: [] }; let interfaceData = { apis: [], cats: [] };
res = JSON.parse(res); res = JSON.parse(res);
@ -51,7 +69,7 @@ function improtData(importDataModule) {
if (isOAS3) { if (isOAS3) {
res = openapi2swagger(res) res = openapi2swagger(res)
} }
res = await handleSwaggerData(res);
SwaggerData = res; SwaggerData = res;
if (res.tags && Array.isArray(res.tags)) { if (res.tags && Array.isArray(res.tags)) {
res.tags.forEach(tag => { res.tags.forEach(tag => {
@ -87,7 +105,7 @@ function improtData(importDataModule) {
}) })
}) })
console.log(interfaceData)
return interfaceData; return interfaceData;
} catch (e) { } catch (e) {
@ -112,7 +130,6 @@ function improtData(importDataModule) {
api.req_query = []; api.req_query = [];
api.req_body_type = 'raw'; api.req_body_type = 'raw';
api.res_body_type = 'raw'; api.res_body_type = 'raw';
if (data.produces && data.produces.indexOf('application/json') > -1) { if (data.produces && data.produces.indexOf('application/json') > -1) {
api.res_body_type = 'json'; api.res_body_type = 'json';
@ -192,9 +209,10 @@ function improtData(importDataModule) {
} }
function handleBodyPamras(data, api) { 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)) { if (isJson(api.req_body_other)) {
api.req_body_type = 'json'; api.req_body_type = 'json';
api.req_body_is_json_schema = true;
} }
} }
@ -203,41 +221,49 @@ function improtData(importDataModule) {
if (!api || typeof api !== 'object') { if (!api || typeof api !== 'object') {
return res_body; return res_body;
} }
_.each(api, (res, code) => { let codes = Object.keys(api);
if (/^2/.test(code)) { let curCode;
if (res && typeof res === 'object') { 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) { if (res.schema) {
res_body = handleSchema(res.schema); res_body = JSON.stringify(res.schema,null,2);
} else if (res.description) { } else if (res.description) {
res_body = res.description; res_body = res.description;
}
} else if (typeof res === 'string') {
res_body = res;
} else {
res_body = '';
} }
} else if (typeof res === 'string') {
res_body = res;
} else {
res_body = '';
} }
}) }else{
res_body = ''
}
return res_body; return res_body;
} }
function handleSchema(data) { // function handleSchema(data) {
if (!data) return data; // // if (!data) return data;
if (typeof data !== 'object') { // // if (typeof data !== 'object') {
return data; // // return data;
} // // }
try { // // try {
// data.definitions = SwaggerData.definitions; // // // data.definitions = SwaggerData.definitions;
isOAS3 ? data.components = SwaggerData.components : data.definitions = SwaggerData.definitions // // isOAS3 ? data.components = SwaggerData.components : data.definitions = SwaggerData.definitions
let schema = deref(data, true); // // let schema = deref(data, true);
let jsfData = JSON.stringify(schema, null, 2); // // let jsfData = JSON.stringify(schema, null, 2);
return jsfData; // // return jsfData;
} catch (e) { // // } catch (e) {
return ''; // // return '';
} // // }
} // return data;
// }
if (!importDataModule || typeof importDataModule !== 'object') { if (!importDataModule || typeof importDataModule !== 'object') {
console.error('importDataModule 参数Must be Object Type'); console.error('importDataModule 参数Must be Object Type');

106
package-lock.json generated
View File

@ -2153,7 +2153,6 @@
"version": "6.26.0", "version": "6.26.0",
"resolved": "http://registry.npm.taobao.org/babel-runtime/download/babel-runtime-6.26.0.tgz", "resolved": "http://registry.npm.taobao.org/babel-runtime/download/babel-runtime-6.26.0.tgz",
"integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=",
"dev": true,
"requires": { "requires": {
"core-js": "2.5.3", "core-js": "2.5.3",
"regenerator-runtime": "0.11.1" "regenerator-runtime": "0.11.1"
@ -2635,6 +2634,11 @@
"resolved": "http://registry.npm.taobao.org/bson/download/bson-0.5.7.tgz", "resolved": "http://registry.npm.taobao.org/bson/download/bson-0.5.7.tgz",
"integrity": "sha1-DRH+CTbB/uAp4R9wY/XQqyQi6j4=" "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": { "buf-compare": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "http://registry.npm.taobao.org/buf-compare/download/buf-compare-1.0.1.tgz", "resolved": "http://registry.npm.taobao.org/buf-compare/download/buf-compare-1.0.1.tgz",
@ -3798,8 +3802,7 @@
"cookie": { "cookie": {
"version": "0.3.1", "version": "0.3.1",
"resolved": "http://registry.npm.taobao.org/cookie/download/cookie-0.3.1.tgz", "resolved": "http://registry.npm.taobao.org/cookie/download/cookie-0.3.1.tgz",
"integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=", "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s="
"dev": true
}, },
"cookie-signature": { "cookie-signature": {
"version": "1.0.6", "version": "1.0.6",
@ -3955,8 +3958,7 @@
"core-js": { "core-js": {
"version": "2.5.3", "version": "2.5.3",
"resolved": "http://registry.npm.taobao.org/core-js/download/core-js-2.5.3.tgz", "resolved": "http://registry.npm.taobao.org/core-js/download/core-js-2.5.3.tgz",
"integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=", "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4="
"dev": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "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": { "cross-spawn": {
"version": "5.1.0", "version": "5.1.0",
"resolved": "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-5.1.0.tgz", "resolved": "http://registry.npm.taobao.org/cross-spawn/download/cross-spawn-5.1.0.tgz",
@ -5045,6 +5056,11 @@
"core-js": "2.5.3" "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": { "encodeurl": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "http://registry.npm.taobao.org/encodeurl/download/encodeurl-1.0.2.tgz", "resolved": "http://registry.npm.taobao.org/encodeurl/download/encodeurl-1.0.2.tgz",
@ -5055,7 +5071,6 @@
"version": "0.1.12", "version": "0.1.12",
"resolved": "http://registry.npm.taobao.org/encoding/download/encoding-0.1.12.tgz", "resolved": "http://registry.npm.taobao.org/encoding/download/encoding-0.1.12.tgz",
"integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=",
"dev": true,
"requires": { "requires": {
"iconv-lite": "0.4.13" "iconv-lite": "0.4.13"
} }
@ -6141,6 +6156,14 @@
"integrity": "sha1-S2LEK44D3j+EhGC2OQeZIGldAVQ=", "integrity": "sha1-S2LEK44D3j+EhGC2OQeZIGldAVQ=",
"dev": true "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": { "fast-json-stable-stringify": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/fast-json-stable-stringify/download/fast-json-stable-stringify-2.0.0.tgz", "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": { "is-stream": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "http://registry.npm.taobao.org/is-stream/download/is-stream-1.1.0.tgz", "resolved": "http://registry.npm.taobao.org/is-stream/download/is-stream-1.1.0.tgz",
"integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ="
"dev": true
}, },
"is-svg": { "is-svg": {
"version": "2.1.0", "version": "2.1.0",
@ -9305,6 +9327,26 @@
"whatwg-fetch": "2.0.3" "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": { "isstream": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "http://registry.npm.taobao.org/isstream/download/isstream-0.1.2.tgz", "resolved": "http://registry.npm.taobao.org/isstream/download/isstream-0.1.2.tgz",
@ -12265,7 +12307,6 @@
"version": "1.7.3", "version": "1.7.3",
"resolved": "http://registry.npm.taobao.org/node-fetch/download/node-fetch-1.7.3.tgz", "resolved": "http://registry.npm.taobao.org/node-fetch/download/node-fetch-1.7.3.tgz",
"integrity": "sha1-mA9vcthSEaU0fGsrwYxbhMPrR+8=", "integrity": "sha1-mA9vcthSEaU0fGsrwYxbhMPrR+8=",
"dev": true,
"requires": { "requires": {
"encoding": "0.1.12", "encoding": "0.1.12",
"is-stream": "1.1.0" "is-stream": "1.1.0"
@ -18209,8 +18250,7 @@
"regenerator-runtime": { "regenerator-runtime": {
"version": "0.11.1", "version": "0.11.1",
"resolved": "http://registry.npm.taobao.org/regenerator-runtime/download/regenerator-runtime-0.11.1.tgz", "resolved": "http://registry.npm.taobao.org/regenerator-runtime/download/regenerator-runtime-0.11.1.tgz",
"integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk=", "integrity": "sha1-vgWtf5v30i4Fb5cmzuUBf78Z4uk="
"dev": true
}, },
"regenerator-transform": { "regenerator-transform": {
"version": "0.10.1", "version": "0.10.1",
@ -20079,6 +20119,39 @@
"serviceworker-cache-polyfill": "4.0.0" "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": { "symbol-observable": {
"version": "0.2.4", "version": "0.2.4",
"resolved": "http://registry.npm.taobao.org/symbol-observable/download/symbol-observable-0.2.4.tgz", "resolved": "http://registry.npm.taobao.org/symbol-observable/download/symbol-observable-0.2.4.tgz",
@ -21201,8 +21274,12 @@
"utf8-bytes": { "utf8-bytes": {
"version": "0.0.1", "version": "0.0.1",
"resolved": "http://registry.npm.taobao.org/utf8-bytes/download/utf8-bytes-0.0.1.tgz", "resolved": "http://registry.npm.taobao.org/utf8-bytes/download/utf8-bytes-0.0.1.tgz",
"integrity": "sha1-EWsCVEjJtQAIHN+/H01sbDfYg30=", "integrity": "sha1-EWsCVEjJtQAIHN+/H01sbDfYg30="
"dev": true },
"utfstring": {
"version": "2.0.0",
"resolved": "http://registry.npm.taobao.org/utfstring/download/utfstring-2.0.0.tgz",
"integrity": "sha1-szH3NR6b4cRjNMx1GIJs2jtEJCo="
}, },
"util": { "util": {
"version": "0.10.3", "version": "0.10.3",
@ -22017,8 +22094,7 @@
"whatwg-fetch": { "whatwg-fetch": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "http://registry.npm.taobao.org/whatwg-fetch/download/whatwg-fetch-2.0.3.tgz", "resolved": "http://registry.npm.taobao.org/whatwg-fetch/download/whatwg-fetch-2.0.3.tgz",
"integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ=", "integrity": "sha1-nITsLc9oGH/wC8ZOEnS0QhduHIQ="
"dev": true
}, },
"whatwg-url": { "whatwg-url": {
"version": "4.8.0", "version": "4.8.0",

View File

@ -65,6 +65,7 @@
"request": "2.81.0", "request": "2.81.0",
"sha.js": "2.4.9", "sha.js": "2.4.9",
"sha1": "1.1.1", "sha1": "1.1.1",
"swagger-client": "^3.5.1",
"tslib": "1.8.0", "tslib": "1.8.0",
"underscore": "1.8.3", "underscore": "1.8.3",
"url": "0.11.0", "url": "0.11.0",