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

Fixed a regression from 1.7.7: a `$ref` inside `allOf` was sampled as `null`, and `skipReadOnly`/`skipWriteOnly` ignored a `$ref` inside `oneOf` or `anyOf`.
3 changes: 2 additions & 1 deletion src/allOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function allOfSample(into, children, options, spec, context) {
const subSamples = [];

for (let subSchema of children) {
const { type, readOnly, writeOnly, value } = traverse({ type: res.type, ...subSchema }, options, spec, {
const childSchema = subSchema.$ref ? subSchema : { type: res.type, ...subSchema };
const { type, readOnly, writeOnly, value } = traverse(childSchema, options, spec, {
...context,
isAllOfChild: true,
});
Expand Down
4 changes: 3 additions & 1 deletion src/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export function traverse(schema, options, spec, context) {
ref = ref.substring(1);
}

const { $ref: _, ...siblings } = schema;
const siblings = Object.fromEntries(
Object.entries(schema).filter(([key, value]) => key !== '$ref' && value !== undefined),
);
const referenced = Object.keys(siblings).length && !shouldIgnoreRefSiblings(spec)
? mergeRefSiblings(JsonPointer.get(spec, ref), siblings)
: JsonPointer.get(spec, ref);
Expand Down
48 changes: 48 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 @@ -939,6 +939,54 @@
expect(spec.components.schemas.Num).toEqual({ type: 'number' });
}
});

it('should apply a null sibling written in the document', () => {
result = sample({ $ref: '#/components/schemas/Num', default: null }, {}, refSiblingsSpec({ openapi: '3.1.0' }));
expect(result).toBeNull();
});

it('should not apply the type that allOf passes to its items to a $ref', () => {
for (const spec of [refSiblingsSpec({ openapi: '3.1.0' }), refSiblingsSpec({})]) {
result = sample({ allOf: [{ $ref: '#/components/schemas/Pet' }] }, {}, spec);
expect(result).toEqual({ name: 'string' });

result = sample({
allOf: [
{ $ref: '#/components/schemas/Pet' },
{ properties: { age: { type: 'integer' } } },
]
}, {}, spec);
expect(result).toEqual({ name: 'string', age: 0 });
}
});

it('should keep readOnly and writeOnly of a $ref in oneOf and anyOf', () => {
for (const version of [{ openapi: '3.1.0' }, {}]) {
const spec = {
...version,
components: {
schemas: {
Id: { type: 'string', readOnly: true },
Password: { type: 'string', writeOnly: true },
},
},
};
const schema = {
type: 'object',
properties: {
id: { oneOf: [{ $ref: '#/components/schemas/Id' }] },
password: { anyOf: [{ $ref: '#/components/schemas/Password' }] },
name: { type: 'string' },
},
};

result = sample(schema, { skipReadOnly: true }, spec);
expect(result).toEqual({ password: 'string', name: 'string' });

result = sample(schema, { skipWriteOnly: true }, spec);
expect(result).toEqual({ id: 'string', name: 'string' });
}
});
});
});

Expand Down
Loading