From 414ff8c0543959efb65a7773b32ec12ce593af81 Mon Sep 17 00:00:00 2001 From: fbirds5230 Date: Mon, 23 Nov 2026 21:47:39 +0100 Subject: [PATCH] fix: preserve skipped properties across allOf samples --- .changeset/calm-cats-listen.md | 5 +++++ src/utils.js | 5 +++++ test/integration.spec.js | 23 +++++++++++++++++++++++ test/xml.spec.js | 19 +++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 .changeset/calm-cats-listen.md diff --git a/.changeset/calm-cats-listen.md b/.changeset/calm-cats-listen.md new file mode 100644 index 0000000..5766283 --- /dev/null +++ b/.changeset/calm-cats-listen.md @@ -0,0 +1,5 @@ +--- +"openapi-sampler": patch +--- + +Fixed skipped `readOnly` and `writeOnly` properties reappearing when a later `allOf` member supplies a value. diff --git a/src/utils.js b/src/utils.js index 105865d..1e4a70e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -41,6 +41,11 @@ export function mergeDeep(...objects) { const pVal = prev[key]; const oVal = obj[key]; + // A later allOf sample must not restore a property that was skipped. + if (pVal === SKIP_SYMBOL) { + return; + } + if (isObject(pVal) && isObject(oVal)) { prev[key] = mergeDeep(pVal, oVal); } else { diff --git a/test/integration.spec.js b/test/integration.spec.js index 1007aef..46868e5 100644 --- a/test/integration.spec.js +++ b/test/integration.spec.js @@ -488,6 +488,29 @@ describe('Integration', () => { a: 'string', }); }); + + it.each([ + ['readOnly', 'skipReadOnly', 'first'], + ['readOnly', 'skipReadOnly', 'last'], + ['writeOnly', 'skipWriteOnly', 'first'], + ['writeOnly', 'skipWriteOnly', 'last'], + ])('should respect %s with %s when its allOf schema comes %s', (annotation, option, position) => { + const restriction = { type: 'object', properties: { b: { [annotation]: true } } }; + const base = { + type: 'object', + properties: { + a: { type: 'string' }, + b: { type: 'integer', [annotation]: false }, + }, + }; + const schema = { + allOf: position === 'first' ? [restriction, base] : [base, restriction], + }; + const result = sample(schema, { [option]: true }); + expect(result).toEqual({ + a: 'string', + }); + }); }); describe('Inheritance', () => { diff --git a/test/xml.spec.js b/test/xml.spec.js index 5631ec9..e9d9382 100644 --- a/test/xml.spec.js +++ b/test/xml.spec.js @@ -824,6 +824,25 @@ describe('XML', () => { `); }); + it.each([ + ['readOnly', 'skipReadOnly'], + ['writeOnly', 'skipWriteOnly'], + ])('should skip %s properties when their allOf schema precedes a $ref', (annotation, option) => { + const schema = { + allOf: [ + { type: 'object', properties: { tenant_id: { [annotation]: true } } }, + { $ref: '#/components/schemas/User' }, + ], + }; + + expect(sample(schema, { ...options, skipReadOnly: true, [option]: true }, spec)).toMatchInlineSnapshot(` +" + string + +" +`); + }); + it('should throw for a root null sample', () => { expect(() => sample({ type: 'null' }, options, spec)).toThrow('Unknown format output for building XML.'); });