diff --git a/packages/oas/src/operation/index.ts b/packages/oas/src/operation/index.ts index 8497aa0c..2608b5ba 100644 --- a/packages/oas/src/operation/index.ts +++ b/packages/oas/src/operation/index.ts @@ -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; } diff --git a/packages/oas/test/operation/lib/get-requestbody-examples.test.ts b/packages/oas/test/operation/lib/get-requestbody-examples.test.ts index fe5fd547..5e9a4cc0 100644 --- a/packages/oas/test/operation/lib/get-requestbody-examples.test.ts +++ b/packages/oas/test/operation/lib/get-requestbody-examples.test.ts @@ -5,6 +5,7 @@ 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); @@ -12,6 +13,8 @@ beforeAll(async () => { 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', () => { @@ -19,6 +22,37 @@ test('should return early if there is no request body', () => { 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([