Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add empty value check in getRequestBodyExamples() #904

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion packages/oas/src/operation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,9 @@ export class Operation {
*
*/
getRequestBodyExamples(): RequestBodyExamples {
if (this.requestBodyExamples) {
const nonEmptyRequestExample = this.requestBodyExamples?.[0]?.examples?.[0].value;
darrenyong marked this conversation as resolved.
Show resolved Hide resolved

if (this.requestBodyExamples && nonEmptyRequestExample) {
return this.requestBodyExamples;
}

Expand Down
33 changes: 33 additions & 0 deletions packages/oas/test/operation/lib/get-requestbody-examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,53 @@ import cleanStringify from '../../__fixtures__/json-stringify-clean.js';

let operationExamples: Oas;
let petstore: Oas;
let webhooksOas: Oas;

beforeAll(async () => {
operationExamples = await import('../../__datasets__/operation-examples.json').then(r => r.default).then(Oas.init);
await operationExamples.dereference();

petstore = await import('@readme/oas-examples/3.0/json/petstore.json').then(r => r.default).then(Oas.init);
await petstore.dereference();

webhooksOas = await import('@readme/oas-examples/3.1/json/webhooks.json').then(r => r.default).then(Oas.init);
await webhooksOas.dereference();
});

test('should return early if there is no request body', () => {
const operation = operationExamples.operation('/nothing', 'get');
expect(operation.getRequestBodyExamples()).toStrictEqual([]);
});

test('webhooks', () => {
darrenyong marked this conversation as resolved.
Show resolved Hide resolved
darrenyong marked this conversation as resolved.
Show resolved Hide resolved
const webhookOperation = webhooksOas.operation('newPet', 'post', { isWebhook: true });
webhookOperation.requestBodyExamples = [
{
mediaType: 'application/json',
examples: [
{
value: undefined,
kanadgupta marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
];

expect(webhookOperation.getRequestBodyExamples()).toStrictEqual([
{
mediaType: 'application/json',
examples: [
{
value: {
id: 0,
name: 'string',
tag: 'string',
},
},
],
},
]);
});

test('should support */* media types', () => {
const operation = operationExamples.operation('/wildcard-media-type', 'post');
expect(operation.getRequestBodyExamples()).toStrictEqual([
Expand Down