From 67c37bfc2458b12846818fbf3e1cf339d776f219 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 6 Sep 2024 13:59:18 +0100 Subject: [PATCH] ChoicesByUrl - A custom property value is lost fix #8783 (#8784) * ChoicesByUrl - A custom property value is lost fix #8783 * Fix compiling * Fix compiling error #8783 --- packages/survey-core/src/choicesRestful.ts | 63 +++++++------------ .../tests/question_baseselecttests.ts | 16 +++++ 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/packages/survey-core/src/choicesRestful.ts b/packages/survey-core/src/choicesRestful.ts index d11132f7e7..788960c799 100644 --- a/packages/survey-core/src/choicesRestful.ts +++ b/packages/survey-core/src/choicesRestful.ts @@ -297,40 +297,29 @@ export class ChoicesRestful extends Base { } return res; } - public setData(json: any) { - this.clear(); - if (json.url) this.url = json.url; - if (json.path) this.path = json.path; - if (json.valueName) this.valueName = json.valueName; - if (json.titleName) this.titleName = json.titleName; - if (json.imageLinkName) this.imageLinkName = json.imageLinkName; - if (json.allowEmptyResponse !== undefined) - this.allowEmptyResponse = json.allowEmptyResponse; - if (json.attachOriginalItems !== undefined) - this.attachOriginalItems = json.attachOriginalItems; - var properties = this.getCustomPropertiesNames(); - for (var i = 0; i < properties.length; i++) { - if (json[properties[i]]) (this)[properties[i]] = json[properties[i]]; - } + private getAllPropertiesNames(): Array { + const res = new Array(); + Serializer.getPropertiesByObj(this).forEach(prop => res.push(prop.name)); + this.getCustomPropertiesNames().forEach(prop => res.push(prop)); + return res; + } + public setData(json: any): void { + if(!json) json = {}; + this.getAllPropertiesNames().forEach(name => { + (this)[name] = json[name]; + }); } public getData(): any { - if (this.isEmpty) return null; - var res: any = {}; - if (this.url) res["url"] = this.url; - if (this.path) res["path"] = this.path; - if (this.valueName) res["valueName"] = this.valueName; - if (this.titleName) res["titleName"] = this.titleName; - if (this.imageLinkName) res["imageLinkName"] = this.imageLinkName; - if (this.allowEmptyResponse) - res["allowEmptyResponse"] = this.allowEmptyResponse; - if (this.attachOriginalItems) - res["attachOriginalItems"] = this.attachOriginalItems; - var properties = this.getCustomPropertiesNames(); - for (var i = 0; i < properties.length; i++) { - if ((this)[properties[i]]) - res[properties[i]] = (this)[properties[i]]; - } - return res; + const res: any = {}; + let hasValue = false; + this.getAllPropertiesNames().forEach(name => { + const val = (this)[name]; + if(!this.isValueEmpty(val) && val !== this.getDefaultPropertyValue(name)) { + res[name] = val; + hasValue = true; + } + }); + return hasValue ? res : null; } /** * A RESTful service's URL. @@ -454,15 +443,7 @@ export class ChoicesRestful extends Base { return prop.type; } public clear(): void { - this.url = undefined; - this.path = undefined; - this.valueName = undefined; - this.titleName = undefined; - this.imageLinkName = undefined; - var properties = this.getCustomPropertiesNames(); - for (var i = 0; i < properties.length; i++) { - if ((this)[properties[i]]) (this)[properties[i]] = ""; - } + this.setData(undefined); } protected beforeSendRequest() { this.isRunningValue = true; diff --git a/packages/survey-core/tests/question_baseselecttests.ts b/packages/survey-core/tests/question_baseselecttests.ts index 95aece61ec..2994f1a1ea 100644 --- a/packages/survey-core/tests/question_baseselecttests.ts +++ b/packages/survey-core/tests/question_baseselecttests.ts @@ -2213,3 +2213,19 @@ QUnit.test("Add condition custom property", function (assert) { Serializer.removeProperty("itemvalue", "customExp"); }); +QUnit.test("question checkbox add a custom property into choicesByUrl, Bug#8783", (assert) => { + Serializer.addProperty("choicesByUrl", "jsonpath"); + const survey = new SurveyModel({ + elements: [ + { type: "checkbox", name: "q1", + choicesByUrl: { valueName: "name", jsonpath: "mypath" } + } + ] + }); + const q1 = survey.getQuestionByName("q1"); + assert.equal(q1.choicesByUrl.valueName, "name", "valueName"); + assert.equal(q1.choicesByUrl["jsonpath"], "mypath", "load jsonpath"); + q1.choicesByUrl["jsonpath"] = "newpath"; + assert.deepEqual(q1.toJSON(), { name: "q1", choicesByUrl: { valueName: "name", jsonpath: "newpath" } }, "#2"); + Serializer.removeProperty("choicesByUrl", "jsonpath"); +});