From bbc9072a634a996beab19e1a8fd58ea52b8d09d7 Mon Sep 17 00:00:00 2001 From: kt81 Date: Thu, 27 Sep 2018 14:43:22 +0900 Subject: [PATCH] handle implicit types --- src/baseInterfaces.ts | 3 ++- src/requestCodeGen.ts | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/baseInterfaces.ts b/src/baseInterfaces.ts index 1d6fa6f..a7e3401 100644 --- a/src/baseInterfaces.ts +++ b/src/baseInterfaces.ts @@ -39,7 +39,8 @@ export interface IRequestMethod { description: string schema: { '$ref': string, - 'type': string, + 'type'?: string, + 'items'?: IParameterItems, } } } diff --git a/src/requestCodeGen.ts b/src/requestCodeGen.ts index 57f75ec..65761a1 100644 --- a/src/requestCodeGen.ts +++ b/src/requestCodeGen.ts @@ -114,18 +114,31 @@ export function requestCodeGen(paths: IPaths, options: ISwaggerOptions): string pathReplace = parsedParameters.requestPathReplace } + // 确定响应的类型 + // It does not allow the schema defined directly, but only the primitive type is allowed. let responseType: string; - if (!v.responses['200'] || !v.responses['200'].schema) { responseType = 'any'; } else if (v.responses['200'].schema.$ref) { responseType = refClassName(v.responses['200'].schema.$ref) } else { - responseType = v.responses[200].schema.type; - if (responseType == 'object' || responseType == 'array') { - // Direct defining is not supported. + let checkType = v.responses[200].schema.type; + if (!checkType) { + // implicit types + if (v.responses[200].schema.items) { + responseType = 'array'; + } else { // if (v.responses[200].schema.properties) // actual check + responseType = 'object'; + } + } else { + responseType = checkType; // string? -> string + } + if (responseType == 'object') { responseType = 'any'; + } else if (responseType == 'array') { + responseType = 'any[]'; } + // else ... JSON primitive types (string, boolean, number) } // 模版