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/allof-recursive-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-sampler": patch
---

Fixed a regression from 1.7.8 where a recursive `$ref` inside `allOf` was sampled as `null` instead of an empty object.
2 changes: 1 addition & 1 deletion src/allOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function allOfSample(into, children, options, spec, context) {
const subSamples = [];

for (let subSchema of children) {
const childSchema = subSchema.$ref ? subSchema : { type: res.type, ...subSchema };
const childSchema = subSchema.$ref ? { ...subSchema } : { type: res.type, ...subSchema };
const { type, readOnly, writeOnly, value } = traverse(childSchema, options, spec, {
...context,
isAllOfChild: true,
Expand Down
21 changes: 21 additions & 0 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@
type: 'object',
properties: { top: { type: 'number' } },
if: { properties: { foo: { type: 'string', format: 'email' } } },
then: { properties: { bar: { type: 'string' } } },

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

View workflow job for this annotation

GitHub Actions / Lint, Test & Build

unicorn(no-thenable)

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

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

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

View workflow job for this annotation

GitHub Actions / Lint, Test & Build

unicorn(no-thenable)

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

Expand Down Expand Up @@ -960,6 +960,27 @@
}
});

it('should sample a recursive $ref inside allOf as an empty object', () => {
for (const version of [{ openapi: '3.0.0' }, { openapi: '3.1.0' }]) {
const spec = {
...version,
components: {
schemas: {
Node: {
type: 'object',
properties: {
name: { type: 'string' },
child: { allOf: [{ $ref: '#/components/schemas/Node' }] },
},
},
},
},
};
result = sample(spec.components.schemas.Node, {}, spec);
expect(result).toEqual({ name: 'string', child: { name: 'string', child: {} } });
}
});

it('should keep readOnly and writeOnly of a $ref in oneOf and anyOf', () => {
for (const version of [{ openapi: '3.1.0' }, {}]) {
const spec = {
Expand Down
Loading