fix: swagger import data bug

This commit is contained in:
suxiaoxin 2017-10-15 17:46:53 +08:00
parent 018f47a224
commit c6105a5d18
2 changed files with 60 additions and 3 deletions

View File

@ -664,10 +664,11 @@ var anyType = { type: ['string', 'number', 'integer', 'boolean'] };
var objectType = function objectType(value, path, resolve, traverseCallback) { var objectType = function objectType(value, path, resolve, traverseCallback) {
var props = {}; var props = {};
var properties = value.properties || {}; var properties = value.properties || {};
var propertyKeys = value.required = Object.keys(properties);
var patternProperties = value.patternProperties || {}; var patternProperties = value.patternProperties || {};
var requiredProperties = (value.required || []).slice(); var requiredProperties = (value.required || []).slice();
var allowsAdditional = value.additionalProperties === false ? false : true; var allowsAdditional = value.additionalProperties === false ? false : true;
var propertyKeys = Object.keys(properties);
var patternPropertyKeys = Object.keys(patternProperties); var patternPropertyKeys = Object.keys(patternProperties);
var additionalProperties = allowsAdditional var additionalProperties = allowsAdditional
? (value.additionalProperties === true ? {} : value.additionalProperties) ? (value.additionalProperties === true ? {} : value.additionalProperties)
@ -1110,3 +1111,43 @@ var VERSION = "0.5.0-rc11";
jsf.version = VERSION; jsf.version = VERSION;
module.exports = jsf; 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))

View File

@ -67,12 +67,13 @@ function improtData(importDataModule){
} }
if(data.consumes && Array.isArray(data.consumes)){ 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'; api.req_body_type = 'form';
}else if(data.consumes.indexOf('application/json') > -1){ }else if(data.consumes.indexOf('application/json') > -1){
api.req_body_type = 'json'; api.req_body_type = 'json';
} }
} }
//处理response //处理response
api.res_body = handleResponse(data.responses); api.res_body = handleResponse(data.responses);
@ -87,7 +88,7 @@ function improtData(importDataModule){
switch(param.in){ switch(param.in){
case 'path' : api.req_params.push(defaultParam); break; case 'path' : api.req_params.push(defaultParam); break;
case 'query': api.req_query.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 'formData' : defaultParam.type = param.type === 'file'? 'file' : 'text'; api.req_body_form.push(defaultParam); break;
case 'header' : api.req_headers.push(defaultParam);break; case 'header' : api.req_headers.push(defaultParam);break;
} }
@ -98,6 +99,21 @@ function improtData(importDataModule){
return api; 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){ function handleResponse(api){
let res_body = ''; let res_body = '';
if(!api || typeof api !== 'object'){ if(!api || typeof api !== 'object'){