Skip to content

Commit 81735d7

Browse files
committed
fix: remove readOnly/writeOnly from nested allOf
1 parent 61d4ba0 commit 81735d7

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/allOf.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { traverse } from './traverse';
2-
import { mergeDeep } from './utils';
2+
import { mergeDeep, SKIP_SYMBOL } from './utils';
3+
34

45
export function allOfSample(into, children, options, spec, context) {
56
let res = traverse(into, options, spec);
67
const subSamples = [];
78

89
for (let subSchema of children) {
9-
const { type, readOnly, writeOnly, value } = traverse({ type: res.type, ...subSchema }, options, spec, context);
10+
const { type, readOnly, writeOnly, value } = traverse({ type: res.type, ...subSchema }, options, spec, {
11+
...context,
12+
isAllOfChild: true,
13+
});
1014
if (res.type && type && type !== res.type) {
1115
console.warn('allOf: schemas with different types can\'t be merged');
1216
res.type = type;
@@ -19,6 +23,11 @@ export function allOfSample(into, children, options, spec, context) {
1923

2024
if (res.type === 'object') {
2125
res.value = mergeDeep(res.value || {}, ...subSamples.filter(sample => typeof sample === 'object'));
26+
for (const key in res.value) {
27+
if (res.value[key] === SKIP_SYMBOL) {
28+
delete res.value[key];
29+
}
30+
}
2231
return res;
2332
} else {
2433
if (res.type === 'array') {

src/samplers/object.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { traverse } from '../traverse';
22
import { applyXMLAttributes } from '../utils';
3+
import { SKIP_SYMBOL } from '../utils';
34

45
export function sampleObject(schema, options = {}, spec, context) {
56
let res = {};
@@ -23,10 +24,16 @@ export function sampleObject(schema, options = {}, spec, context) {
2324

2425
const sample = traverse(schema.properties[propertyName], options, spec, { propertyName, depth: depth + 1 });
2526
if (options.skipReadOnly && sample.readOnly) {
27+
if (context?.isAllOfChild) {
28+
res[propertyName] = SKIP_SYMBOL;
29+
}
2630
return;
2731
}
2832

2933
if (options.skipWriteOnly && sample.writeOnly) {
34+
if (context?.isAllOfChild) {
35+
res[propertyName] = SKIP_SYMBOL;
36+
}
3037
return;
3138
}
3239

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
export const SKIP_SYMBOL = Symbol('skip');
4+
35
function pad(number) {
46
if (number < 10) {
57
return '0' + number;

test/integration.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,30 @@ describe('Integration', () => {
464464

465465
expect(result).toEqual(expected);
466466
});
467+
468+
469+
it('should repsect readOnly from schemas in allOf', () => {
470+
const schema = {
471+
allOf: [
472+
{
473+
type: 'object',
474+
properties: {
475+
a: { type: 'string' },
476+
b: { type: 'integer' },
477+
},
478+
},
479+
{
480+
properties: {
481+
b: { readOnly: true },
482+
},
483+
}
484+
],
485+
};
486+
const result = sample(schema, { skipReadOnly: true });
487+
expect(result).toEqual({
488+
a: 'string',
489+
});
490+
});
467491
});
468492

469493
describe('Inheritance', () => {

0 commit comments

Comments
 (0)