Skip to content

Commit c290323

Browse files
committed
handle no schema request body
Signed-off-by: Nik Nasr <[email protected]> remove unused import Signed-off-by: Nik Nasr <[email protected]> fix Signed-off-by: Nik Nasr <[email protected]>
1 parent 3e326e7 commit c290323

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/elements-core/src/components/TryIt/TryIt.spec.tsx

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@testing-library/jest-dom';
22

33
import { Provider as MosaicProvider } from '@stoplight/mosaic';
4-
import { HttpParamStyles, IHttpOperation } from '@stoplight/types';
4+
import { HttpParamStyles, IHttpOperation, IMediaTypeContent } from '@stoplight/types';
55
import { cleanup, render, screen, waitFor } from '@testing-library/react';
66
import userEvent from '@testing-library/user-event';
77
import fetchMock from 'jest-fetch-mock';
@@ -609,6 +609,62 @@ describe('TryIt', () => {
609609
expect(bodyHeader).not.toBeInTheDocument();
610610
});
611611

612+
it('does not hide panel when request body has no schema but has content', async () => {
613+
render(
614+
<TryItWithPersistence
615+
httpOperation={{
616+
...requestBodyEmptySchema,
617+
request: {
618+
body: {
619+
id: '?http-request-body?',
620+
contents: [
621+
{
622+
...requestBodyEmptySchema.request?.body?.contents?.at(0),
623+
mediaType: 'application/json',
624+
schema: undefined,
625+
} as IMediaTypeContent,
626+
],
627+
},
628+
},
629+
method: 'POST',
630+
}}
631+
/>,
632+
);
633+
634+
let bodyHeader = screen.queryByText('Body');
635+
expect(bodyHeader).toBeInTheDocument();
636+
});
637+
638+
it('send content-type header when request body has no schema but has content', async () => {
639+
render(
640+
<TryItWithPersistence
641+
httpOperation={{
642+
...requestBodyEmptySchema,
643+
request: {
644+
body: {
645+
id: '?http-request-body?',
646+
contents: [
647+
{
648+
...requestBodyEmptySchema.request?.body?.contents?.at(0),
649+
mediaType: 'application/json',
650+
schema: undefined,
651+
} as IMediaTypeContent,
652+
],
653+
},
654+
},
655+
method: 'POST',
656+
}}
657+
/>,
658+
);
659+
660+
clickSend();
661+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
662+
const requestInit = fetchMock.mock.calls[0][1]!;
663+
expect(requestInit.method).toMatch(/^post$/i);
664+
const headers = new Headers(requestInit.headers);
665+
expect(headers.get('Content-Type')).toBe('application/json');
666+
});
667+
612668
it('statically generates request body basing on request body schema', () => {
613669
render(<TryItWithPersistence httpOperation={requestBody} />);
614670

packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const generateExampleFromMediaTypeContent = (
3939
) => {
4040
const textRequestBodySchema = mediaTypeContent?.schema;
4141
const textRequestBodyExamples = mediaTypeContent?.examples;
42+
const textRequestBodyMediaType = mediaTypeContent?.mediaType;
4243

4344
try {
4445
if (textRequestBodyExamples?.length) {
@@ -57,6 +58,9 @@ export const generateExampleFromMediaTypeContent = (
5758
console.warn(e);
5859
return `Example cannot be created for this schema\n${e}`;
5960
}
61+
if (textRequestBodyMediaType) {
62+
return '';
63+
}
6064
return undefined;
6165
};
6266

0 commit comments

Comments
 (0)