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: Content-Type header added despite no request body defined in POST/PUT/PATCH endpoints #2739

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/elements-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-core",
"version": "9.0.0",
"version": "9.0.1",
"sideEffects": [
"web-components.min.js",
"src/web-components/**",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { useGenerateExampleFromMediaTypeContent } from '../../../utils/exampleGe
export const useTextRequestBodyState = (
mediaTypeContent: IMediaTypeContent | undefined,
skipReadOnly: boolean,
): [string, React.Dispatch<React.SetStateAction<string>>] => {
): [string | undefined, React.Dispatch<React.SetStateAction<string | undefined>>] => {
const initialRequestBody = useGenerateExampleFromMediaTypeContent(mediaTypeContent, undefined, {
skipReadOnly,
});

const [textRequestBody, setTextRequestBody] = React.useState<string>(initialRequestBody);
const [textRequestBody, setTextRequestBody] = React.useState<string | undefined>(initialRequestBody);

React.useEffect(() => {
setTextRequestBody(initialRequestBody);
Expand Down
50 changes: 50 additions & 0 deletions packages/elements-core/src/components/TryIt/TryIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,56 @@ describe('TryIt', () => {
});
});

describe('No Request body', () => {
it('Adds no Content-type header with GET method', async () => {
render(<TryItWithPersistence httpOperation={basicOperation} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^get$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});

it('Adds no Content-type header with POST method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'POST' }} />);

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(null);
});

it('Adds no Content-type header with PATCH method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PATCH' }} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^patch$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});

it('Adds no Content-type header with PUT method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PUT' }} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^put$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});
});

describe('Mocking', () => {
it('Shows mock button', () => {
render(<TryItWithPersistence httpOperation={basicOperation} mockUrl="https://mock-todos.stoplight.io" />);
Expand Down
2 changes: 1 addition & 1 deletion packages/elements-core/src/components/TryIt/TryIt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const TryIt: React.FC<TryItProps> = ({
values={bodyParameterValues}
onChangeValues={setBodyParameterValues}
/>
) : mediaTypeContent ? (
) : mediaTypeContent && textRequestBody !== undefined ? (
<RequestBody
examples={mediaTypeContent.examples ?? []}
requestBody={textRequestBody}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const generateExampleFromMediaTypeContent = (
console.warn(e);
return `Example cannot be created for this schema\n${e}`;
}
return '';
return undefined;
};

export const generateExamplesFromJsonSchema = (schema: JSONSchema7 & { 'x-examples'?: unknown }): Example[] => {
Expand Down
4 changes: 2 additions & 2 deletions packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "3.0.0",
"version": "3.0.1",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down Expand Up @@ -64,9 +64,9 @@
]
},
"dependencies": {
"@stoplight/elements-core": "^9.0.1",
"@stoplight/markdown-viewer": "^5.7.1",
"@stoplight/mosaic": "^1.53.4",
"@stoplight/elements-core": "~9.0.0",
"@stoplight/path": "^1.3.2",
"@stoplight/types": "^14.0.0",
"classnames": "^2.2.6",
Expand Down
6 changes: 3 additions & 3 deletions packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements",
"version": "9.0.0",
"version": "9.0.1",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down Expand Up @@ -63,7 +63,7 @@
]
},
"dependencies": {
"@stoplight/elements-core": "~9.0.0",
"@stoplight/elements-core": "^9.0.1",
"@stoplight/http-spec": "^7.1.0",
"@stoplight/json": "^3.18.1",
"@stoplight/mosaic": "^1.53.4",
Expand Down Expand Up @@ -109,4 +109,4 @@
"release": {
"extends": "@stoplight/scripts/release"
}
}
}