Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-cats-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-sampler": patch
---

Fixed skipped `readOnly` and `writeOnly` properties reappearing when a later `allOf` member supplies a value.
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,29 @@
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', () => {
Expand Down Expand Up @@ -608,7 +631,7 @@
type: 'object',
properties: { top: { type: 'number' } },
if: { properties: { foo: { type: 'string', format: 'email' } } },
then: { properties: { bar: { type: 'string' } } },

Check warning on line 634 in test/integration.spec.js

View workflow job for this annotation

GitHub Actions / Lint, Test & Build

unicorn(no-thenable)

test/integration.spec.js:634:9: Do not add `then` to an object.
else: { properties: { baz: { type: 'number' } } },
};

Expand Down Expand Up @@ -745,7 +768,7 @@
}
],
if: { properties: { foo: { type: 'string', format: 'email' } } },
then: { properties: { bar: { type: 'string' } } },

Check warning on line 771 in test/integration.spec.js

View workflow job for this annotation

GitHub Actions / Lint, Test & Build

unicorn(no-thenable)

test/integration.spec.js:771:9: Do not add `then` to an object.
else: { properties: { baz: { type: 'number' } } },
};

Expand Down
19 changes: 19 additions & 0 deletions test/xml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
"<User>
<username>string</username>
</User>
"
`);
});

it('should throw for a root null sample', () => {
expect(() => sample({ type: 'null' }, options, spec)).toThrow('Unknown format output for building XML.');
});
Expand Down
Loading