Skip to content

Commit ad7cef2

Browse files
committed
guiUtils: Prefer defaults from top-level templates when merging allOf templates
1 parent 4ed9642 commit ad7cef2

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v0.17.0
2+
## Fixed
3+
* guiUtils: Prefer defaults from top-level templates when merging allOf templates
4+
15
# v0.16.0
26
## Fixed
37
* schema_provider: Fix loading schema files with a '.' in the filename

lib/gui_utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,28 @@ const fixAllOfOrder = (schema, orderID) => {
112112
};
113113

114114
const mergeAllOf = (schema) => {
115+
const useObjValue = [
116+
'title',
117+
'description',
118+
'default',
119+
'propertyOrder'
120+
];
121+
115122
if (!schema.allOf) {
116123
return schema;
117124
}
118125

119126
schema.allOf.forEach(subSchema => mergeAllOf(subSchema));
120127

121128
mergeWith(schema, ...schema.allOf, (objValue, srcValue, key) => {
122-
if (key === 'title' || key === 'description') {
129+
if (useObjValue.includes(key)) {
123130
return objValue;
124131
}
125132

126133
if (key === 'required') {
127134
return Array.from(new Set([...(objValue || []), ...srcValue]));
128135
}
129136

130-
if (key === 'propertyOrder') {
131-
return objValue;
132-
}
133-
134137
if (key === 'dependencies') {
135138
return mergeWith({}, objValue, srcValue, (dst, src) => {
136139
if (Array.isArray(dst) && Array.isArray(src)) {

test/gui_utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ describe('GUI utils test', function () {
110110
// Preserve top-level title
111111
assert.strictEqual(schema.title, 'top level');
112112
});
113+
it('all_of_defaults', function () {
114+
let schema = {
115+
title: 'extended',
116+
properties: {
117+
port: {
118+
default: 45
119+
}
120+
},
121+
allOf: [
122+
{
123+
title: 'base',
124+
properties: {
125+
port: {
126+
type: 'integer',
127+
default: 443
128+
}
129+
}
130+
}
131+
]
132+
};
133+
schema = guiUtils.modSchemaForJSONEditor(schema);
134+
console.log(JSON.stringify(schema, null, 2));
135+
136+
const props = schema.properties;
137+
assert.strictEqual(props.port.default, 45);
138+
});
113139
it('all_of_merge_dependencies', function () {
114140
let schema = {
115141
type: 'object',

0 commit comments

Comments
 (0)