Skip to content

Commit

Permalink
fix: add empty value check in getRequestBodyExamples() (#904)
Browse files Browse the repository at this point in the history
| 🚥 Resolves ISSUE_ID |
| :------------------- |

## 🧰 Changes
There is a weird case in our main repo where we initialize `Webhooks`
with an empty `requestBodyExamples` with the following structure:
```
requestBodyExamples: [
  {
    mediaType: 'application/json',
    examples: [{
      value: undefined
    }],
  },
];
```

This causes `Operation.getRequestBodyExamples()` to return with an empty
request example even though we should be creating an example for them.
This adds an extra check so that we avoid returning the empty example
and proceed to the subsequent `getRequestBodyExamples()` call that
generates an example for the user.

## 🧬 QA & Testing

Provide as much information as you can on how to test what you've done.
  • Loading branch information
darrenyong committed Sep 13, 2024
1 parent 42c2a5d commit dcb7ab6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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 isRequestExampleValueDefined = typeof this.requestBodyExamples?.[0]?.examples?.[0].value !== 'undefined';

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

Expand Down
34 changes: 34 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,54 @@ 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);
});

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

test('should re-intialize the request examples after the oas is dereferenced', async () => {
const webhookOperation = webhooksOas.operation('newPet', 'post', { isWebhook: true });

expect(webhookOperation.getRequestBodyExamples()).toStrictEqual([
{
mediaType: 'application/json',
examples: [
{
value: undefined,
},
],
},
]);

await webhooksOas.dereference();
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

0 comments on commit dcb7ab6

Please sign in to comment.