Skip to content

Commit

Permalink
Handle invalid json in bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Feb 12, 2024
1 parent d884c6e commit 92b8987
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This package follows standard semvar, `<major>.<minor>.<build>`. No breaking cha

## 2.2
* The `table` view is now the default as the `tree` view becomes deprecated. To display the `tree` view, use the `tree` attribute on the openapi-explorer html element.
* Handle invalid JSON parsing in request bodies

## 2.1
* Add `x-locale` vendor extension to specify the locale of the spec.
Expand Down
6 changes: 4 additions & 2 deletions src/components/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,12 @@ export default class ApiRequest extends LitElement {
if (exampleTextAreaEl && exampleTextAreaEl.value) {
fetchOptions.body = exampleTextAreaEl.value;
if (requestBodyType.includes('json')) {
fetchOptions.body = JSON.stringify(json5.parse(exampleTextAreaEl.value));
try {
fetchOptions.body = JSON.stringify(json5.parse(exampleTextAreaEl.value));
curlData = ` \\\n -d '${fetchOptions.body}'`;
} catch (err) { /* Ignore unparseable JSON */ }
} catch (err) {
/* Ignore unparseable JSON, falls back automatically to the original value, which is better than nothing */
}
}

if (!curlData) {
Expand Down

0 comments on commit 92b8987

Please sign in to comment.