From 3217450dca632633821d66a938cd26a2856fe4f1 Mon Sep 17 00:00:00 2001 From: "Buri Erich (extern) ( U804174 )" Date: Wed, 13 Jul 2016 13:34:43 +0200 Subject: [PATCH 1/2] parse definitions from Swagger --- lib/codegen.js | 12 +++++++++++- lib/typescript.js | 3 +++ templates/typescript-definitions-only.mustache | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 templates/typescript-definitions-only.mustache diff --git a/lib/codegen.js b/lib/codegen.js index cafa13cd..a53cefb9 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -65,7 +65,8 @@ var getViewForSwagger2 = function(opts, type){ className: opts.className, imports: opts.imports, domain: (swagger.schemes && swagger.schemes.length > 0 && swagger.host && swagger.basePath) ? swagger.schemes[0] + '://' + swagger.host + swagger.basePath.replace(/\/+$/g,'') : '', - methods: [] + methods: [], + definitions: [] }; _.forEach(swagger.paths, function(api, path){ @@ -157,6 +158,15 @@ var getViewForSwagger2 = function(opts, type){ data.methods.push(method); }); }); + + _.forEach(swagger.definitions, function(typeInfo, typeName) { + typeInfo.type = typeInfo.type || 'object'; + + var tsType = ts.convertType(typeInfo); + tsType.name = typeName; + data.definitions.push(tsType); + }); + return data; }; diff --git a/lib/typescript.js b/lib/typescript.js index 15e1c9ca..173ad99f 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -15,6 +15,9 @@ function convertType(swaggerType) { var typespec = {}; + // sanitize input + swaggerType = swaggerType || {}; + if (swaggerType.hasOwnProperty('schema')) { return convertType(swaggerType.schema); } else if (_.isString(swaggerType.$ref)) { diff --git a/templates/typescript-definitions-only.mustache b/templates/typescript-definitions-only.mustache new file mode 100644 index 00000000..d238ae58 --- /dev/null +++ b/templates/typescript-definitions-only.mustache @@ -0,0 +1,3 @@ +{{#definitions}} +interface {{name}} {{>type}} +{{/definitions}} From 9e0cb8fecb8f03ecf4033cf2a5c8c1ff613c0887 Mon Sep 17 00:00:00 2001 From: "Buri Erich (extern) ( U804174 )" Date: Wed, 13 Jul 2016 13:44:00 +0200 Subject: [PATCH 2/2] updated README.md --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 568c22d1..29f8a91b 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,24 @@ methods: type: boolean isFormParameter: type: boolean +definitions: + type: array + description: Array with all Types defined in defintions + items: + type: object + properties: + tsType: + type: string + isRef: + type: boolean + isObject: + type: boolean + isArray: + type: boolean + isAtomic: + type: boolean + name: + type: string ``` ####Custom Mustache Variables