From b60103767ff5fa64e1a3e3a9542c883009ae7a4c Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 6 Aug 2021 15:57:53 +0200 Subject: [PATCH] feat(js-yaml): update our main YAML parser to v4.1.0 Refs https://github.com/swagger-api/swagger-ui/pull/6804 --- package-lock.json | 75 +++++++++++++++++-- package.json | 2 +- .../editor/editor-plugins/json-to-yaml.js | 2 +- src/plugins/json-schema-validator/index.js | 4 +- .../forms/components/AddForm.jsx | 12 +-- .../components/ImportFileMenuItem.jsx | 4 +- src/standalone/topbar/topbar.jsx | 16 ++-- .../plugins/json-schema-validator/index.js | 10 +-- 8 files changed, 95 insertions(+), 30 deletions(-) diff --git a/package-lock.json b/package-lock.json index 447afd61de1..df7a1e41d9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3684,6 +3684,16 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -3773,6 +3783,16 @@ "path-exists": "^4.0.0" } }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -13259,6 +13279,16 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", @@ -21650,12 +21680,18 @@ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "js-yaml": { - "version": "3.14.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", - "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", "requires": { - "argparse": "^1.0.7", - "esprima": "^4.0.0" + "argparse": "^2.0.1" + }, + "dependencies": { + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + } } }, "jsbn": { @@ -21788,6 +21824,17 @@ "path-loader": "^1.0.10", "slash": "^3.0.0", "uri-js": "^4.2.2" + }, + "dependencies": { + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + } } }, "json-schema": { @@ -32959,6 +33006,15 @@ "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "qs": { "version": "6.10.1", "resolved": "https://registry.npmjs.org/qs/-/qs-6.10.1.tgz", @@ -33031,6 +33087,15 @@ "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==" }, + "js-yaml": { + "version": "3.14.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.1.tgz", + "integrity": "sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g==", + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", diff --git a/package.json b/package.json index fb937cbe97c..778dd86aa02 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "file-dialog": "0.0.8", "immutable": "^3.x.x", "is-json": "^2.0.1", - "js-yaml": "^3.13.1", + "js-yaml": "=4.1.0", "json-beautify": "^1.0.1", "json-refs": "^3.0.4", "lodash": "^4.17.21", diff --git a/src/plugins/editor/editor-plugins/json-to-yaml.js b/src/plugins/editor/editor-plugins/json-to-yaml.js index 0219e235835..12c71e6f9d0 100644 --- a/src/plugins/editor/editor-plugins/json-to-yaml.js +++ b/src/plugins/editor/editor-plugins/json-to-yaml.js @@ -9,7 +9,7 @@ export default function(editor) { let yamlString try { - yamlString = YAML.safeDump(YAML.safeLoad(originalStr), { + yamlString = YAML.dump(YAML.load(originalStr), { lineWidth: -1 // don't generate line folds }) } catch (e) { diff --git a/src/plugins/json-schema-validator/index.js b/src/plugins/json-schema-validator/index.js index 93eb2a2cc72..518a110b922 100644 --- a/src/plugins/json-schema-validator/index.js +++ b/src/plugins/json-schema-validator/index.js @@ -6,8 +6,8 @@ import debounce from "lodash/debounce" import swagger2SchemaYaml from "./swagger2-schema.yaml" import oas3SchemaYaml from "./oas3-schema.yaml" -const swagger2Schema = YAML.safeLoad(swagger2SchemaYaml) -const oas3Schema = YAML.safeLoad(oas3SchemaYaml) +const swagger2Schema = YAML.load(swagger2SchemaYaml) +const oas3Schema = YAML.load(oas3SchemaYaml) // Lazily created promise worker let _promiseWorker diff --git a/src/standalone/topbar-insert/forms/components/AddForm.jsx b/src/standalone/topbar-insert/forms/components/AddForm.jsx index 2370d25536e..c008a69fba5 100644 --- a/src/standalone/topbar-insert/forms/components/AddForm.jsx +++ b/src/standalone/topbar-insert/forms/components/AddForm.jsx @@ -9,7 +9,7 @@ class AddForm extends Component { this.state = { formErrors: false, - formData: this.props.existingData ? + formData: this.props.existingData ? this.props.getFormData( (newForm, path) => this.updateForm(newForm, path), [], this.props.existingData) : this.props.getFormData( (newForm, path) => this.updateForm(newForm, path), [] ) } @@ -40,15 +40,15 @@ class AddForm extends Component { // Update the spec string in the Swagger UI state with the new json. const currentJson = this.props.specSelectors.specJson() - this.props.specActions.updateSpec(YAML.safeDump(currentJson.toJS()), "insert") + this.props.specActions.updateSpec(YAML.dump(currentJson.toJS()), "insert") // Perform any parent component actions for the form. this.props.submit() } updateForm = (newFormData, path) => { - this.setState(prevState => ({ - formData: prevState.formData.setIn(path, newFormData) + this.setState(prevState => ({ + formData: prevState.formData.setIn(path, newFormData) })) } @@ -61,7 +61,7 @@ class AddForm extends Component {
-
+
{ this.state.formErrors &&
Please fix errors before submitting.
} @@ -69,7 +69,7 @@ class AddForm extends Component {
) - } + } } AddForm.propTypes = { diff --git a/src/standalone/topbar-menu-file-import_file/components/ImportFileMenuItem.jsx b/src/standalone/topbar-menu-file-import_file/components/ImportFileMenuItem.jsx index 3983f9a0ae1..2870cfb7673 100644 --- a/src/standalone/topbar-menu-file-import_file/components/ImportFileMenuItem.jsx +++ b/src/standalone/topbar-menu-file-import_file/components/ImportFileMenuItem.jsx @@ -14,9 +14,9 @@ export default class ImportFileMenuItem extends Component { fileReader.onload = fileLoadedEvent => { let content = fileLoadedEvent.target.result - + try { - const preparedContent = isJsonObject(content) ? YAML.safeDump(YAML.safeLoad(content)) : content + const preparedContent = isJsonObject(content) ? YAML.dump(YAML.load(content)) : content if (typeof onDocumentLoad === "function") { onDocumentLoad(preparedContent) diff --git a/src/standalone/topbar/topbar.jsx b/src/standalone/topbar/topbar.jsx index f46dd12c3d0..b960d64967f 100644 --- a/src/standalone/topbar/topbar.jsx +++ b/src/standalone/topbar/topbar.jsx @@ -66,7 +66,7 @@ export default class Topbar extends React.Component { .then(res => { this.setState({ clients: res.body || [] }) }) - + serverGetter({}, { // contextUrl is needed because swagger-client is curently // not building relative server URLs correctly @@ -97,7 +97,7 @@ export default class Topbar extends React.Component { .then(res => res.text()) .then(text => { this.props.specActions.updateSpec( - YAML.safeDump(YAML.safeLoad(text), { + YAML.dump(YAML.load(text), { lineWidth: -1 }) ) @@ -129,9 +129,9 @@ export default class Topbar extends React.Component { //// so convert and download // JSON String -> JS object - let jsContent = YAML.safeLoad(editorContent) + let jsContent = YAML.load(editorContent) // JS object -> YAML string - let yamlContent = YAML.safeDump(jsContent) + let yamlContent = YAML.dump(jsContent) this.downloadFile(yamlContent, `${fileName}.yaml`) } @@ -146,7 +146,7 @@ export default class Topbar extends React.Component { } // JSON or YAML String -> JS object - let jsContent = YAML.safeLoad(editorContent) + let jsContent = YAML.load(editorContent) // JS Object -> pretty JSON string let prettyJsonContent = beautifyJson(jsContent, null, 2) this.downloadFile(prettyJsonContent, `${fileName}.json`) @@ -164,8 +164,8 @@ export default class Topbar extends React.Component { convertToYaml = () => { // Editor content -> JS object -> YAML string let editorContent = this.props.specSelectors.specStr() - let jsContent = YAML.safeLoad(editorContent) - let yamlContent = YAML.safeDump(jsContent) + let jsContent = YAML.load(editorContent) + let yamlContent = YAML.dump(jsContent) this.props.specActions.updateSpec(yamlContent) } @@ -361,7 +361,7 @@ export default class Topbar extends React.Component {
  • - topbarActions.showModal("convert")} /> diff --git a/test/unit/plugins/json-schema-validator/index.js b/test/unit/plugins/json-schema-validator/index.js index da398dbe58f..42b559bc952 100644 --- a/test/unit/plugins/json-schema-validator/index.js +++ b/test/unit/plugins/json-schema-validator/index.js @@ -6,8 +6,8 @@ import path from 'path'; const swagger2SchemaYaml = fs.readFileSync(path.join(__dirname, '../../../../src/plugins/json-schema-validator/swagger2-schema.yaml')).toString(); const oas3SchemaYaml = fs.readFileSync(path.join(__dirname, '../../../../src/plugins/json-schema-validator/oas3-schema.yaml')).toString(); -const swagger2Schema = YAML.safeLoad(swagger2SchemaYaml); -const oas3Schema = YAML.safeLoad(oas3SchemaYaml); +const swagger2Schema = YAML.load(swagger2SchemaYaml); +const oas3Schema = YAML.load(oas3SchemaYaml); var testDocuments = fs @@ -19,7 +19,7 @@ var testDocuments = fs })) .map(doc => ({ path: doc.path, - content: YAML.safeLoad(doc.contentString) + content: YAML.load(doc.contentString) })); testDocuments.forEach(doc => { @@ -85,5 +85,5 @@ function assertCaseExpectations(currentCase, result) { itFn('should equal expected value', function() { expect(result).toEqual(currentCase.output.equal); }); - } -} \ No newline at end of file + } +}