From a6ce69cd3da951905395e88ec1599706bae9bb09 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 13 Nov 2017 20:43:04 +0000 Subject: [PATCH] Allow optional object properties --- lib/typescript.js | 7 ++++++- templates/type.mustache | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/typescript.js b/lib/typescript.js index c0829f3a..b6c5a844 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -61,6 +61,12 @@ function convertType(swaggerType, swagger) { _.forEach(swaggerType.properties, function (propertyType, propertyName) { var property = convertType(propertyType); property.name = propertyName; + + property.optional = true; + if (swaggerType.required && swaggerType.required.indexOf(propertyName) !== -1) { + property.optional = false; + } + typespec.properties.push(property); }); } @@ -79,4 +85,3 @@ function convertType(swaggerType, swagger) { } module.exports.convertType = convertType; - diff --git a/templates/type.mustache b/templates/type.mustache index d8f2518d..fd7b9157 100644 --- a/templates/type.mustache +++ b/templates/type.mustache @@ -4,8 +4,8 @@ <%#isRef%><%target%><%/isRef%><%! %><%#isAtomic%><%&tsType%><%/isAtomic%><%! %><%#isObject%>{<%#properties%> -'<%name%>': <%>type%><%/properties%> +'<%name%>'<%#optional%>?<%/optional%>: <%>type%><%/properties%> }<%/isObject%><%! %><%#isArray%>Array<<%#elementType%><%>type%><%/elementType%>>|<%#elementType%><%>type%><%/elementType%><%/isArray%> <%={{ }}=%> -{{/tsType}} \ No newline at end of file +{{/tsType}}