From 6bfb37cb47b9bcf89ae30eb87eecc7cd7124456e Mon Sep 17 00:00:00 2001 From: suxiaoxin Date: Sun, 15 Oct 2017 17:46:53 +0800 Subject: [PATCH] fix: swagger import data bug --- common/json-schema-mockjs.js | 43 ++++++++++++++++++++++- exts/yapi-plugin-import-swagger/client.js | 20 +++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/common/json-schema-mockjs.js b/common/json-schema-mockjs.js index 18c7caa3..72908469 100644 --- a/common/json-schema-mockjs.js +++ b/common/json-schema-mockjs.js @@ -664,10 +664,11 @@ var anyType = { type: ['string', 'number', 'integer', 'boolean'] }; var objectType = function objectType(value, path, resolve, traverseCallback) { var props = {}; var properties = value.properties || {}; + var propertyKeys = value.required = Object.keys(properties); var patternProperties = value.patternProperties || {}; var requiredProperties = (value.required || []).slice(); var allowsAdditional = value.additionalProperties === false ? false : true; - var propertyKeys = Object.keys(properties); + var patternPropertyKeys = Object.keys(patternProperties); var additionalProperties = allowsAdditional ? (value.additionalProperties === true ? {} : value.additionalProperties) @@ -1110,3 +1111,43 @@ var VERSION = "0.5.0-rc11"; jsf.version = VERSION; module.exports = jsf; +var a = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "username": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "email": { + "type": "string" + }, + "password": { + "type": "string" + }, + "phone": { + "type": "string" + }, + "userStatus": { + "type": "integer", + "format": "int32", + "description": "User Status" + } + }, + "xml": { + "name": "User" + } + } +} + +console.log(jsf(a)) \ No newline at end of file diff --git a/exts/yapi-plugin-import-swagger/client.js b/exts/yapi-plugin-import-swagger/client.js index ede4303a..44cffbae 100644 --- a/exts/yapi-plugin-import-swagger/client.js +++ b/exts/yapi-plugin-import-swagger/client.js @@ -67,12 +67,13 @@ function improtData(importDataModule){ } if(data.consumes && Array.isArray(data.consumes)){ - if(data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data' > -1)){ + if(data.consumes.indexOf('application/x-www-form-urlencoded') > -1 || data.consumes.indexOf('multipart/form-data') > -1 ){ api.req_body_type = 'form'; }else if(data.consumes.indexOf('application/json') > -1){ api.req_body_type = 'json'; } } + //处理response api.res_body = handleResponse(data.responses); @@ -87,7 +88,7 @@ function improtData(importDataModule){ switch(param.in){ case 'path' : api.req_params.push(defaultParam); break; case 'query': api.req_query.push(defaultParam); break; - case 'body' : api.req_body_other = handleSchema(param.schema); break; + case 'body' : handleBodyPamras(param.schema, api); break; case 'formData' : defaultParam.type = param.type === 'file'? 'file' : 'text'; api.req_body_form.push(defaultParam); break; case 'header' : api.req_headers.push(defaultParam);break; } @@ -98,6 +99,21 @@ function improtData(importDataModule){ return api; } + function isJson(json){ + try{ + return JSON.parse(json); + }catch(e){ + return false; + } + } + + function handleBodyPamras(data, api){ + api.req_body_other = handleSchema(data); + if(isJson(api.req_body_other)){ + api.req_body_type = 'json'; + } + } + function handleResponse(api){ let res_body = ''; if(!api || typeof api !== 'object'){