Skip to content

Commit 5977044

Browse files
committed
Fixed jschema adapter null values issue
1 parent 0d9f05c commit 5977044

File tree

2 files changed

+91
-61
lines changed

2 files changed

+91
-61
lines changed

components/__tests__/jschema/jschema_adapter.test.js

Lines changed: 87 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -278,67 +278,97 @@ describe('jschema_adapter', () => {
278278
title: 'ProcessTask'
279279
});
280280

281-
expect(JSON.stringify(cleanJschema)).eq(
282-
JSON.stringify({
283-
$defs: {
284-
ProcessAModel: {
285-
description: 'A process model with the same parameter name as ProcessBModel.',
286-
properties: {
287-
step: {
288-
const: 'ProcessA',
289-
title: 'Step',
290-
type: 'string',
291-
description: 'A literal to identify the process type.'
292-
},
293-
parameter1: {
294-
title: 'Parameter1',
295-
type: 'number',
296-
description: 'An integer parameter in A.'
297-
}
281+
expect(cleanJschema).deep.eq({
282+
$defs: {
283+
ProcessAModel: {
284+
description: 'A process model with the same parameter name as ProcessBModel.',
285+
properties: {
286+
step: {
287+
const: 'ProcessA',
288+
title: 'Step',
289+
type: 'string',
290+
description: 'A literal to identify the process type.'
298291
},
299-
required: ['step', 'parameter1'],
300-
title: 'ProcessAModel',
301-
type: 'object'
292+
parameter1: {
293+
title: 'Parameter1',
294+
type: 'number',
295+
description: 'An integer parameter in A.'
296+
}
302297
},
303-
ProcessBModel: {
304-
description: 'B process model with the same parameter name as ProcessAModel.',
305-
properties: {
306-
step: {
307-
const: 'ProcessB',
308-
title: 'Step',
309-
type: 'string',
310-
description: 'A literal to identify the process type.'
311-
},
312-
parameter1: {
313-
title: 'Parameter1',
314-
type: 'number',
315-
description: 'An integer parameter in B.'
316-
}
298+
required: ['step', 'parameter1'],
299+
title: 'ProcessAModel',
300+
type: 'object'
301+
},
302+
ProcessBModel: {
303+
description: 'B process model with the same parameter name as ProcessAModel.',
304+
properties: {
305+
step: {
306+
const: 'ProcessB',
307+
title: 'Step',
308+
type: 'string',
309+
description: 'A literal to identify the process type.'
310+
},
311+
parameter1: {
312+
title: 'Parameter1',
313+
type: 'number',
314+
description: 'An integer parameter in B.'
315+
}
316+
},
317+
required: ['step', 'parameter1'],
318+
title: 'ProcessBModel',
319+
type: 'object'
320+
}
321+
},
322+
additionalProperties: false,
323+
properties: {
324+
proc_step: {
325+
oneOf: [
326+
{
327+
$ref: '#/$defs/ProcessAModel'
317328
},
318-
required: ['step', 'parameter1'],
319-
title: 'ProcessBModel',
320-
type: 'object'
329+
{
330+
$ref: '#/$defs/ProcessBModel'
331+
}
332+
],
333+
title: 'Proc Step',
334+
description: 'The processing step to apply.'
335+
}
336+
},
337+
required: ['proc_step'],
338+
type: 'object',
339+
title: 'ProcessTask'
340+
});
341+
});
342+
343+
it('Strip discriminator, schema with default null value (ref #871)', () => {
344+
const schema = stripDiscriminator({
345+
properties: {
346+
advanced_options: {
347+
type: 'object',
348+
properties: {
349+
position_scale: { type: 'number' }
350+
},
351+
default: {
352+
position_scale: null
321353
}
322-
},
323-
additionalProperties: false,
324-
properties: {
325-
proc_step: {
326-
oneOf: [
327-
{
328-
$ref: '#/$defs/ProcessAModel'
329-
},
330-
{
331-
$ref: '#/$defs/ProcessBModel'
332-
}
333-
],
334-
title: 'Proc Step',
335-
description: 'The processing step to apply.'
354+
}
355+
},
356+
type: 'object'
357+
});
358+
359+
expect(schema).deep.eq({
360+
properties: {
361+
advanced_options: {
362+
type: 'object',
363+
properties: {
364+
position_scale: { type: 'number' }
365+
},
366+
default: {
367+
position_scale: null
336368
}
337-
},
338-
required: ['proc_step'],
339-
type: 'object',
340-
title: 'ProcessTask'
341-
})
342-
);
369+
}
370+
},
371+
type: 'object'
372+
});
343373
});
344374
});

components/src/lib/jschema/jschema_adapter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ function mergeProperty(parentObject, key, value) {
172172
* @param {any} parentObject
173173
* @returns {object}
174174
*/
175-
export function stripDiscriminator(jschema, parentObject = null) {
176-
if (parentObject === null) {
175+
export function stripDiscriminator(jschema, parentObject = null, initialized = false) {
176+
if (!initialized) {
177177
parentObject = deepCopy(jschema);
178178
}
179179
if (Array.isArray(parentObject)) {
180180
const adaptedArray = [];
181181
for (const item of parentObject) {
182-
adaptedArray.push(stripDiscriminator(jschema, item));
182+
adaptedArray.push(stripDiscriminator(jschema, item, true));
183183
}
184184
return adaptedArray;
185185
} else if (isObject(parentObject)) {
@@ -188,7 +188,7 @@ export function stripDiscriminator(jschema, parentObject = null) {
188188
const discriminator =
189189
key === 'discriminator' && 'oneOf' in parentObject && isDiscriminator(child);
190190
if (!discriminator) {
191-
adaptedObject[key] = stripDiscriminator(jschema, child);
191+
adaptedObject[key] = stripDiscriminator(jschema, child, true);
192192
}
193193
}
194194
return adaptedObject;

0 commit comments

Comments
 (0)