diff --git a/.changeset/allof-recursive-ref.md b/.changeset/allof-recursive-ref.md new file mode 100644 index 0000000..9c22065 --- /dev/null +++ b/.changeset/allof-recursive-ref.md @@ -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. diff --git a/src/allOf.js b/src/allOf.js index a4d3d31..ad1a028 100644 --- a/src/allOf.js +++ b/src/allOf.js @@ -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, diff --git a/test/integration.spec.js b/test/integration.spec.js index 4e662a5..218788e 100644 --- a/test/integration.spec.js +++ b/test/integration.spec.js @@ -960,6 +960,27 @@ describe('Integration', () => { } }); + 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 = {