diff --git a/README.md b/README.md index 4e39d1c4..96b563ed 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,12 @@ methods: type: boolean isFormParameter: type: boolean + responses: + type: object + description: Includes all defined response-codes and its JSONs schema + additionalProperties: + type: object + description: See the 'Response Object' section in the [Swagger 2.0 specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#response-object) ``` #### Custom Mustache Variables diff --git a/lib/codegen.js b/lib/codegen.js index 6d566b41..af6502f4 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -1,5 +1,4 @@ 'use strict'; - var fs = require('fs'); var Mustache = require('mustache'); var beautify = require('js-beautify').js_beautify; @@ -131,6 +130,11 @@ var getViewForSwagger2 = function(opts, type){ parameter.cardinality = parameter.required ? '' : '?'; method.parameters.push(parameter); }); + + method.responses = op.responses; + _.forEach(method.responses, (obj, key) => { + method.responses[key].tsType = ts.convertType(obj); + }); data.methods.push(method); }); }); diff --git a/lib/typescript.js b/lib/typescript.js index 9d1231f4..45d69357 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -62,6 +62,24 @@ function convertType(swaggerType, swagger) { typespec.properties.push(property); }); } + + _.forEach(swaggerType.properties, function (propertyType, propertyName) { + var property = convertType(propertyType); + property.name = propertyName; + typespec.properties.push(property); + // see if property is required + if (Array.isArray(swaggerType.required)) { + if(swaggerType.required.indexOf(propertyName) > -1) { + property.isRequired = true; + } + else { + property.isRequired = false; + } + } + else { + property.isRequired = true; + } + }); } else { // type unknown or unsupported... just map to 'any'... typespec.tsType = 'any'; diff --git a/templates/type.mustache b/templates/type.mustache index d8f2518d..d0f258cc 100644 --- a/templates/type.mustache +++ b/templates/type.mustache @@ -4,7 +4,7 @@ <%#isRef%><%target%><%/isRef%><%! %><%#isAtomic%><%&tsType%><%/isAtomic%><%! %><%#isObject%>{<%#properties%> -'<%name%>': <%>type%><%/properties%> +'<%name%>'<%^isRequired%>?<%/isRequired%>: <%>type%><%/properties%> }<%/isObject%><%! %><%#isArray%>Array<<%#elementType%><%>type%><%/elementType%>>|<%#elementType%><%>type%><%/elementType%><%/isArray%> <%={{ }}=%>