From 4dc64111c05c6f458f89d362a949716660286c34 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Wed, 30 Nov 2022 18:35:32 -0800 Subject: [PATCH] Fix NPE on load. --- .../map-render/samples/rel-error.wxml | 2 + .../src/components/LocalStorageManager.ts | 26 +++--- .../src/components/RestPersistenceManager.ts | 79 +++++++++---------- .../src/components/import/FreemindImporter.ts | 2 +- .../persistence/XMLSerializerTango.ts | 13 +-- .../unit/export/expected/cdata-support.wxml | 2 +- .../test/unit/export/expected/complex.wxml | 2 +- .../test/unit/export/expected/issue.wxml | 2 +- .../test/unit/export/expected/npe.wxml | 2 +- .../test/unit/export/expected/process.mm | 2 - .../test/unit/export/expected/process.wxml | 2 +- packages/web2d/src/components/Point.js | 11 ++- 12 files changed, 78 insertions(+), 67 deletions(-) create mode 100644 packages/editor/test/playground/map-render/samples/rel-error.wxml diff --git a/packages/editor/test/playground/map-render/samples/rel-error.wxml b/packages/editor/test/playground/map-render/samples/rel-error.wxml new file mode 100644 index 00000000..4778da95 --- /dev/null +++ b/packages/editor/test/playground/map-render/samples/rel-error.wxml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/packages/mindplot/src/components/LocalStorageManager.ts b/packages/mindplot/src/components/LocalStorageManager.ts index e090e0e5..de336137 100644 --- a/packages/mindplot/src/components/LocalStorageManager.ts +++ b/packages/mindplot/src/components/LocalStorageManager.ts @@ -46,6 +46,18 @@ class LocalStorageManager extends PersistenceManager { } } + private buildHeader() { + const csrfToken = this.getCSRFToken(); + const result = { + 'Content-Type': 'text/plain', + Accept: 'application/xml', + }; + if (csrfToken) { + result['X-CSRF-Token'] = csrfToken; + } + return result; + } + loadMapDom(mapId: string): Promise { let result: Promise; let localStorate: string | null = null; @@ -55,20 +67,10 @@ class LocalStorageManager extends PersistenceManager { if (localStorate == null || this.forceLoad) { const url = this.documentUrl.replace('{id}', mapId); - const csrfToken = this.getCSRFToken(); + result = fetch(url, { method: 'get', - headers: - csrfToken != null - ? { - 'Content-Type': 'text/plain', - Accept: 'application/xml', - 'X-CSRF-Token': csrfToken, - } - : { - 'Content-Type': 'text/plain', - Accept: 'application/xml', - }, + headers: this.buildHeader(), }) .then((response: Response) => { if (!response.ok) { diff --git a/packages/mindplot/src/components/RestPersistenceManager.ts b/packages/mindplot/src/components/RestPersistenceManager.ts index c0c8f227..53cd9325 100644 --- a/packages/mindplot/src/components/RestPersistenceManager.ts +++ b/packages/mindplot/src/components/RestPersistenceManager.ts @@ -59,21 +59,21 @@ class RESTPersistenceManager extends PersistenceManager { }, 10000); const persistence = this; + const crfs = this.getCSRFToken(); + const headers = { + 'Content-Type': 'application/json; charset=utf-8', + Accept: 'application/json', + }; + if (crfs) { + headers['X-CSRF-Token'] = crfs; + } + fetch(`${this.documentUrl.replace('{id}', mapId)}?${query}`, { method: 'PUT', // Blob helps to resuce the memory on large payload. body: new Blob([JSON.stringify(data)], { type: 'text/plain' }), - headers: crfs - ? { - 'Content-Type': 'application/json; charset=utf-8', - Accept: 'application/json', - 'X-CSRF-Token': crfs, - } - : { - 'Content-Type': 'application/json; charset=utf-8', - Accept: 'application/json', - }, + headers, }) .then(async (response: Response) => { if (response.ok) { @@ -132,35 +132,33 @@ class RESTPersistenceManager extends PersistenceManager { } discardChanges(mapId: string): void { - const csrfToken = this.getCSRFToken(); + const crfs = this.getCSRFToken(); + const headers = { + 'Content-Type': 'application/json; charset=utf-8', + Accept: 'application/json', + }; + if (crfs) { + headers['X-CSRF-Token'] = crfs; + } fetch(this.revertUrl.replace('{id}', mapId), { method: 'POST', - headers: csrfToken - ? { - 'Content-Type': 'application/json; charset=utf-8', - Accept: 'application/json', - 'X-CSRF-Token': csrfToken, - } - : { - 'Content-Type': 'application/json; charset=utf-8', - Accept: 'application/json', - }, + headers, }); } unlockMap(mapId: string): void { - const csrfToken = this.getCSRFToken(); + const crfs = this.getCSRFToken(); + const headers = { + 'Content-Type': 'text/plain; charset=utf-8', + }; + if (crfs) { + headers['X-CSRF-Token'] = crfs; + } + fetch(this.lockUrl.replace('{id}', mapId), { method: 'PUT', - headers: csrfToken - ? { - 'Content-Type': 'text/plain', - 'X-CSRF-Token': csrfToken, - } - : { - 'Content-Type': 'text/plain', - }, + headers, body: 'false', }); } @@ -181,19 +179,18 @@ class RESTPersistenceManager extends PersistenceManager { loadMapDom(mapId: string): Promise { const url = `${this.documentUrl.replace('{id}', mapId)}/xml`; - const csrfToken = this.getCSRFToken(); + const crfs = this.getCSRFToken(); + const headers = { + 'Content-Type': 'text/plain; charset=utf-8', + Accept: 'application/xml', + }; + if (crfs) { + headers['X-CSRF-Token'] = crfs; + } + return fetch(url, { method: 'get', - headers: csrfToken - ? { - 'Content-Type': 'text/plain', - Accept: 'application/xml', - 'X-CSRF-Token': csrfToken, - } - : { - 'Content-Type': 'text/plain', - Accept: 'application/xml', - }, + headers, }) .then((response: Response) => { if (!response.ok) { diff --git a/packages/mindplot/src/components/import/FreemindImporter.ts b/packages/mindplot/src/components/import/FreemindImporter.ts index eca43a0a..d5fd6c31 100644 --- a/packages/mindplot/src/components/import/FreemindImporter.ts +++ b/packages/mindplot/src/components/import/FreemindImporter.ts @@ -1,4 +1,5 @@ import xmlFormatter from 'xml-formatter'; +import { off } from 'process'; import Importer from './Importer'; import Mindmap from '../model/Mindmap'; import RelationshipModel from '../model/RelationshipModel'; @@ -19,7 +20,6 @@ import NoteModel from '../model/NoteModel'; import FeatureModelFactory from '../model/FeatureModelFactory'; import FeatureModel from '../model/FeatureModel'; import XMLSerializerFactory from '../persistence/XMLSerializerFactory'; -import { off } from 'process'; export default class FreemindImporter extends Importer { private mindmap: Mindmap; diff --git a/packages/mindplot/src/components/persistence/XMLSerializerTango.ts b/packages/mindplot/src/components/persistence/XMLSerializerTango.ts index 0d387fdb..d5fe827f 100644 --- a/packages/mindplot/src/components/persistence/XMLSerializerTango.ts +++ b/packages/mindplot/src/components/persistence/XMLSerializerTango.ts @@ -475,7 +475,7 @@ class XMLSerializerTango implements XMLMindmapSerializer { return value !== null ? value : ''; } - static _deserializeRelationship(domElement: Element, mindmap: Mindmap): RelationshipModel { + static _deserializeRelationship(domElement: Element, mindmap: Mindmap): RelationshipModel | null { const srcId = Number.parseInt(domElement.getAttribute('srcTopicId')!, 10); const destId = Number.parseInt(domElement.getAttribute('destTopicId')!, 10); const lineType = Number.parseInt(domElement.getAttribute('lineType')!, 10); @@ -494,11 +494,14 @@ class XMLSerializerTango implements XMLMindmapSerializer { const model = mindmap.createRelationship(srcId, destId); model.setLineType(lineType); - if ($defined(srcCtrlPoint) && srcCtrlPoint !== '') { - model.setSrcCtrlPoint(Point.fromString(srcCtrlPoint)); + const spoint = Point.fromString(srcCtrlPoint); + if (spoint) { + model.setSrcCtrlPoint(spoint); } - if ($defined(destCtrlPoint) && destCtrlPoint !== '') { - model.setDestCtrlPoint(Point.fromString(destCtrlPoint)); + + const dpoint = Point.fromString(destCtrlPoint); + if (dpoint) { + model.setDestCtrlPoint(dpoint); } model.setEndArrow(false); model.setStartArrow(true); diff --git a/packages/mindplot/test/unit/export/expected/cdata-support.wxml b/packages/mindplot/test/unit/export/expected/cdata-support.wxml index fe1172ed..10aa4102 100644 --- a/packages/mindplot/test/unit/export/expected/cdata-support.wxml +++ b/packages/mindplot/test/unit/export/expected/cdata-support.wxml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/packages/mindplot/test/unit/export/expected/complex.wxml b/packages/mindplot/test/unit/export/expected/complex.wxml index 722b017d..d03e68a8 100644 --- a/packages/mindplot/test/unit/export/expected/complex.wxml +++ b/packages/mindplot/test/unit/export/expected/complex.wxml @@ -48,4 +48,4 @@ Attached below a sample of such screening questions]]> \ No newline at end of file + Again, decrease the learning curve.]]> \ No newline at end of file diff --git a/packages/mindplot/test/unit/export/expected/issue.wxml b/packages/mindplot/test/unit/export/expected/issue.wxml index 15e23d69..54346249 100644 --- a/packages/mindplot/test/unit/export/expected/issue.wxml +++ b/packages/mindplot/test/unit/export/expected/issue.wxml @@ -15,4 +15,4 @@ calculo navegar en internet, base de datos, etc.]]> \ No newline at end of file +escuchar música, ver videos, calcular, comunicarnos con otras personas, etc-]]> \ No newline at end of file diff --git a/packages/mindplot/test/unit/export/expected/npe.wxml b/packages/mindplot/test/unit/export/expected/npe.wxml index 9f021d19..13694870 100644 --- a/packages/mindplot/test/unit/export/expected/npe.wxml +++ b/packages/mindplot/test/unit/export/expected/npe.wxml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/packages/mindplot/test/unit/export/expected/process.mm b/packages/mindplot/test/unit/export/expected/process.mm index de5085b4..73dd081d 100644 --- a/packages/mindplot/test/unit/export/expected/process.mm +++ b/packages/mindplot/test/unit/export/expected/process.mm @@ -37,8 +37,6 @@ - - diff --git a/packages/mindplot/test/unit/export/expected/process.wxml b/packages/mindplot/test/unit/export/expected/process.wxml index c6a4f998..f172aa52 100644 --- a/packages/mindplot/test/unit/export/expected/process.wxml +++ b/packages/mindplot/test/unit/export/expected/process.wxml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/packages/web2d/src/components/Point.js b/packages/web2d/src/components/Point.js index e07ebaed..7fe9da23 100644 --- a/packages/web2d/src/components/Point.js +++ b/packages/web2d/src/components/Point.js @@ -42,7 +42,16 @@ class Point { Point.fromString = function pointFromString(point) { const values = point.split(','); - return new Point(Number.parseInt(values[0], 10), Number.parseInt(values[1], 10)); + let result = null; + if (values.lenght > 1) { + const x = Number.parseInt(values[0], 10); + const y = Number.parseInt(values[1], 10); + + if (!Number.isNaN(x) && !Number.isNaN(y)) { + result = new Point(x, y); + } + } + return result; }; export default Point;