From 9ca31820182ebc1ab1e910799a58c0f50b1e27db Mon Sep 17 00:00:00 2001 From: Nik Nasr Date: Wed, 15 Jan 2025 16:45:37 +0000 Subject: [PATCH] handle no schema request body Signed-off-by: Nik Nasr remove unused import Signed-off-by: Nik Nasr fix Signed-off-by: Nik Nasr trigger CI update tests Signed-off-by: Nik Nasr --- .../src/components/TryIt/TryIt.spec.tsx | 56 ++++++++++++++++++- .../exampleGeneration/exampleGeneration.ts | 4 ++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/elements-core/src/components/TryIt/TryIt.spec.tsx b/packages/elements-core/src/components/TryIt/TryIt.spec.tsx index b1faf49d0..841478cec 100644 --- a/packages/elements-core/src/components/TryIt/TryIt.spec.tsx +++ b/packages/elements-core/src/components/TryIt/TryIt.spec.tsx @@ -1,7 +1,7 @@ import '@testing-library/jest-dom'; import { Provider as MosaicProvider } from '@stoplight/mosaic'; -import { HttpParamStyles, IHttpOperation } from '@stoplight/types'; +import { HttpParamStyles, IHttpOperation, IMediaTypeContent } from '@stoplight/types'; import { cleanup, render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import fetchMock from 'jest-fetch-mock'; @@ -609,6 +609,60 @@ describe('TryIt', () => { expect(bodyHeader).not.toBeInTheDocument(); }); + it('does not hide panel when request body has no schema but has content', async () => { + render( + , + ); + + let bodyHeader = screen.queryByText('Body'); + expect(bodyHeader).toBeInTheDocument(); + }); + + it('send content-type header when request body has no schema but has content', async () => { + render( + , + ); + + clickSend(); + await waitFor(() => expect(fetchMock).toHaveBeenCalled()); + const requestInit = fetchMock.mock.calls[0][1]!; + expect(requestInit.method).toMatch(/^post$/i); + const headers = new Headers(requestInit.headers); + expect(headers.get('Content-Type')).toBe('application/json'); + }); + it('statically generates request body basing on request body schema', () => { render(); diff --git a/packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts b/packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts index c8f9783c0..9c7807f0a 100644 --- a/packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts +++ b/packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts @@ -39,6 +39,7 @@ export const generateExampleFromMediaTypeContent = ( ) => { const textRequestBodySchema = mediaTypeContent?.schema; const textRequestBodyExamples = mediaTypeContent?.examples; + const textRequestBodyMediaType = mediaTypeContent?.mediaType; try { if (textRequestBodyExamples?.length) { @@ -57,6 +58,9 @@ export const generateExampleFromMediaTypeContent = ( console.warn(e); return `Example cannot be created for this schema\n${e}`; } + if (textRequestBodyMediaType) { + return ''; + } return undefined; };