Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Ensure we're not passing a proxy resource as pristineResource

Oliver Günther890e8b37ca5a
diff --git a/frontend/src/app/modules/fields/changeset/resource-changeset.ts b/frontend/src/app/modules/fields/changeset/resource-changeset.ts
index 6777869c752..a17013e6602 100644
--- a/frontend/src/app/modules/fields/changeset/resource-changeset.ts
+++ b/frontend/src/app/modules/fields/changeset/resource-changeset.ts
@@ -6,7 +6,8 @@ import {input, InputState} from "reactivestates";
import {IFieldSchema} from "core-app/modules/fields/field.base";
import {debugLog} from "core-app/helpers/debug_output";
import {take} from "rxjs/operators";
-import {Form} from "@angular/forms";
+
+export const PROXY_IDENTIFIER = '__is_changeset_proxy';
/**
* Temporary class living while a resource is being edited
@@ -28,22 +29,16 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
/** Flag whether this is currently being saved */
public inFlight = false;
+ /** Keep a reference to the original resource */
+ protected _pristineResource:T;
+
/** The projected resource, which will proxy values from the change set */
- public projectedResource:T = new Proxy(
- this.pristineResource,
- {
- get: (_, key:string) => this.proxyGet(key),
- set: (_, key:string, val:any) => {
- this.setValue(key, val);
- return true;
- },
- }
- );
+ public projectedResource:T;
- constructor(public pristineResource:T,
+ constructor(pristineResource:T,
public readonly state?:InputState<ResourceChangeset<T>>,
loadedForm:FormResource|null = null) {
-
+ this.updatePristineResource(pristineResource);
if (loadedForm) {
this.form$.putValue(loadedForm);
}
@@ -68,8 +63,33 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
.then(() => this.buildPayloadFromChanges());
}
+ /**
+ * Update the pristine resource in case it changed
+ *
+ * @param attribute
+ */
+ public updatePristineResource(resource:T) {
+ // Ensure we're not passing in a proxy
+ if ((resource as any)[PROXY_IDENTIFIER]) {
+ throw "You're trying to pass proxy object as a pristine resource. This will cause errors";
+ }
+ this._pristineResource = resource;
+ this.projectedResource = new Proxy(
+ this._pristineResource,
+ {
+ get: (_, key:string) => this.proxyGet(key),
+ set: (_, key:string, val:any) => {
+ this.setValue(key, val);
+ return true;
+ },
+ }
+ );
+ }
+ public get pristineResource():T {
+ return this._pristineResource;
+ }
public getSchemaName(attribute:string):string {
if (this.projectedResource.getSchemaName) {
@@ -187,6 +207,10 @@ export class ResourceChangeset<T extends HalResource|{ [key:string]:unknown; } =
return this.schema;
}
+ if (key === '__is_proxy') {
+ return true;
+ }
+
return this.value(key);
}
diff --git a/frontend/src/app/modules/fields/edit/field-types/formattable-edit-field.component.ts b/frontend/src/app/modules/fields/edit/field-types/formattable-edit-field.component.ts
index e9cc6984205..3aa4c86be63 100644
--- a/frontend/src/app/modules/fields/edit/field-types/formattable-edit-field.component.ts
+++ b/frontend/src/app/modules/fields/edit/field-types/formattable-edit-field.component.ts
@@ -114,7 +114,7 @@ export class FormattableEditFieldComponent extends EditFieldComponent implements
public get ckEditorContext():ICKEditorContext {
return {
- resource: this.resource,
+ resource: this.change.pristineResource,
macros: 'none' as 'none',
previewContext: this.previewContext,
options: { rtl: this.schema.options && this.schema.options.rtl }
diff --git a/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts b/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts
index c22d4684c58..d5ee93641f8 100644
--- a/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts
+++ b/frontend/src/app/modules/fields/edit/services/hal-resource-editing.service.ts
@@ -204,7 +204,7 @@ export class HalResourceEditingService extends StateCacheService<ResourceChanges
return this.edit<V, T>(resource);
}
- changeset.pristineResource = resource;
+ changeset.updatePristineResource(resource);
return changeset;
}
@@ -229,7 +229,7 @@ export class HalResourceEditingService extends StateCacheService<ResourceChanges
filter(([resource, _]) => !!resource),
map(([resource, change]) => {
if (change) {
- change.pristineResource = resource as V;
+ change.updatePristineResource(resource as V);
return change.projectedResource;
}