Skip to content

Commit 1bd5117

Browse files
committed
template/JsonTransformStrategy: Do not stringify objects used for sections
This matches the behavior for arrays used for sections.
1 parent 059ff45 commit 1bd5117

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/template.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ function JsonTransformStrategy(schema, value) {
130130
if (!value) {
131131
return JSON.stringify({});
132132
}
133-
return JSON.stringify(value);
133+
if (!schema.skip_xform) {
134+
return JSON.stringify(value);
135+
}
134136
}
135137

136138
if (schema.format === 'text' && value) {
@@ -471,6 +473,7 @@ class Template {
471473
}
472474
} else if (defType === 'object') {
473475
Object.assign(newDef, items);
476+
newDef.skip_xform = true;
474477
} else if (!asBool) {
475478
throw new Error(`unsupported type for section "${defName}": ${defType}`);
476479
}

test/template.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,28 @@ describe('Template class tests', function () {
546546
assert.strictEqual(tmpl.render(view), reference);
547547
});
548548
});
549+
it('render_section_as_object', function () {
550+
const ymldata = `
551+
contentType: application/json
552+
definitions:
553+
obj:
554+
type: object
555+
template: |-
556+
{{#obj}}{ "prop": {{prop}}}{{/obj}}
557+
`;
558+
const parameters = {
559+
obj: {
560+
prop: 'foo'
561+
}
562+
};
563+
const reference = '{\n "prop": "foo"\n}';
564+
565+
return Template.loadYaml(ymldata)
566+
.then((tmpl) => {
567+
console.log(tmpl.getParametersSchema());
568+
assert.strictEqual(tmpl.render(parameters), reference);
569+
});
570+
});
549571
it('render_section_with_partial', function () {
550572
const ymldata = `
551573
parameters:

0 commit comments

Comments
 (0)