Skip to content

Commit 6da0cf7

Browse files
olivierbeaulieuOlivier Beaulieu
andauthored
fix: fixed an issue with oneOf and readOnly/writeOnly (#130)
Co-authored-by: Olivier Beaulieu <olivier@synctera.com>
1 parent 9f5c46d commit 6da0cf7

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/traverse.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,16 @@ export function traverse(schema, options, spec, context) {
105105
if (!options.quiet) console.warn('oneOf and anyOf are not supported on the same level. Skipping anyOf');
106106
}
107107
popSchemaStack(seenSchemasStack, context);
108-
return tryInferExample(schema) || traverse(schema.oneOf[0], options, spec, context);
108+
109+
// Make sure to pass down readOnly and writeOnly annotations from the parent
110+
const firstOneOf = Object.assign({
111+
readOnly: schema.readOnly,
112+
writeOnly: schema.writeOnly
113+
}, schema.oneOf[0]);
114+
115+
return (
116+
tryInferExample(schema) || traverse(firstOneOf, options, spec, context)
117+
);
109118
}
110119

111120
if (schema.anyOf && schema.anyOf.length) {

test/unit/object.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,42 @@ describe('sampleObject', () => {
4444
});
4545
});
4646

47+
it('should skip readonly properties in oneOfs if skipReadOnly=true', () => {
48+
res = sampleObject(
49+
{
50+
properties: {
51+
a: { type: 'string' },
52+
b: {
53+
type: 'object',
54+
oneOf: [
55+
{
56+
type: 'object',
57+
properties: {
58+
c: {
59+
type: 'string',
60+
},
61+
},
62+
},
63+
{
64+
type: 'object',
65+
properties: {
66+
d: {
67+
type: 'string',
68+
},
69+
},
70+
},
71+
],
72+
readOnly: true,
73+
},
74+
},
75+
},
76+
{ skipReadOnly: true }
77+
);
78+
expect(res).to.deep.equal({
79+
a: 'string',
80+
});
81+
});
82+
4783
it('should skip writeonly properties if writeonly=true', () => {
4884
res = sampleObject({properties: {
4985
a: {type: 'string'},
@@ -70,6 +106,42 @@ describe('sampleObject', () => {
70106
});
71107
});
72108

109+
it('should skip writeonly properties in oneOfs if skipReadOnly=true', () => {
110+
res = sampleObject(
111+
{
112+
properties: {
113+
a: { type: 'string' },
114+
b: {
115+
type: 'object',
116+
oneOf: [
117+
{
118+
type: 'object',
119+
properties: {
120+
c: {
121+
type: 'string',
122+
},
123+
},
124+
},
125+
{
126+
type: 'object',
127+
properties: {
128+
d: {
129+
type: 'string',
130+
},
131+
},
132+
},
133+
],
134+
writeOnly: true,
135+
},
136+
},
137+
},
138+
{ skipWriteOnly: true }
139+
);
140+
expect(res).to.deep.equal({
141+
a: 'string',
142+
});
143+
});
144+
73145
it('should should instantiate 2 additionalProperties', () => {
74146
res = sampleObject({additionalProperties: {type: 'string'}});
75147
expect(res).to.deep.equal({

0 commit comments

Comments
 (0)