Skip to content

Commit 47fadd9

Browse files
committed
template: Only use JsonTransformStrategy when contentType is 'application/json'
Also make transformStrategies customizable (similar to mergeStrategies). NOTE: Templates that were relying on this behavior for plain text (the default contentType) templates will need to be updated to use the 'application/json' contentType, or specify a custom TransformStrategy.
1 parent 1bee973 commit 47fadd9

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const {
2626
CompositeTemplateProvider
2727
} = require('./lib/template_provider');
2828
const { GitHubTemplateProvider, GitHubSchemaProvider } = require('./lib/github_provider');
29-
const { Template, mergeStrategies, postProcessStrategies } = require('./lib/template');
29+
const {
30+
Template, mergeStrategies, postProcessStrategies, transformStrategies
31+
} = require('./lib/template');
3032
const guiUtils = require('./lib/gui_utils');
3133
const TransactionLogger = require('./lib/transaction_logger');
3234

@@ -42,6 +44,7 @@ module.exports = {
4244
Template,
4345
mergeStrategies,
4446
postProcessStrategies,
47+
transformStrategies,
4548
guiUtils,
4649
dataStores,
4750
TransactionLogger

lib/template.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ function nodeHttpToAxios(configObj) {
106106
}
107107
}
108108

109+
/**
110+
* TransformStrategy for plain text output
111+
*/
112+
function PlainTextTransformStrategy(_schema, value) {
113+
return value;
114+
}
115+
109116
/**
110117
* TransformStrategy for JSON output
111118
*/
@@ -133,6 +140,15 @@ function JsonTransformStrategy(schema, value) {
133140
return value;
134141
}
135142

143+
/**
144+
* Object containing available transform strategy functions.
145+
* The property is a Content-Type MIME (e.g., `test/plain`).
146+
* The value is a TransformStrategy function that accepts the parameter's schema and current value.
147+
*/
148+
const transformStrategies = {
149+
'application/json': JsonTransformStrategy
150+
};
151+
136152
/**
137153
* MergeStrategy for targeting `test/plain` Content-Type
138154
*/
@@ -1070,7 +1086,7 @@ class Template {
10701086
*/
10711087
transformParameters(parameters) {
10721088
const schema = this.getParametersSchema();
1073-
const transform = JsonTransformStrategy;
1089+
const transform = transformStrategies[this.contentType] || PlainTextTransformStrategy;
10741090
return Object.keys(parameters).reduce((acc, curr) => {
10751091
const value = parameters[curr];
10761092
const valueSchema = schema.properties && schema.properties[curr];
@@ -1255,5 +1271,6 @@ class Template {
12551271

12561272
module.exports = {
12571273
Template,
1258-
mergeStrategies
1274+
mergeStrategies,
1275+
transformStrategies
12591276
};

test/template.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ describe('Template class tests', function () {
526526
{{> numbpartial}}
527527
{{> arraypartial}}
528528
`;
529-
const reference = 'numb=5\narr=["1","2"]\n';
529+
const reference = 'numb=5\narr=1,2\n';
530530
return Template.loadYaml(ymldata)
531531
.then((tmpl) => {
532532
assert.strictEqual(tmpl.render(), reference);
@@ -604,22 +604,24 @@ describe('Template class tests', function () {
604604
assert.strictEqual(schema.properties.virtual_port.type, 'integer');
605605
});
606606
});
607-
it('render_array', function () {
607+
it('render_json_array', function () {
608608
const mstdata = '{{values::array}}';
609609
const parameters = { values: ['1', '2', '3'] };
610-
const reference = '["1","2","3"]';
610+
const reference = '[\n "1",\n "2",\n "3"\n]';
611611
return Template.loadMst(mstdata)
612612
.then((tmpl) => {
613+
tmpl.contentType = 'application/json';
613614
console.log(JSON.stringify(tmpl.getParametersSchema(), null, 2));
614615
assert.strictEqual(tmpl.render(parameters), reference);
615616
});
616617
});
617-
it('render_text', function () {
618+
it('render_json_text', function () {
618619
const mstdata = '{{textvar::text}}';
619620
const parameters = { textvar: 'multi\nline' };
620621
const reference = '"multi\\nline"';
621622
return Template.loadMst(mstdata)
622623
.then((tmpl) => {
624+
tmpl.contentType = 'application/json';
623625
console.log(JSON.stringify(tmpl.getParametersSchema(), null, 2));
624626
assert.strictEqual(tmpl.render(parameters), reference);
625627
});

0 commit comments

Comments
 (0)