Skip to content

Commit

Permalink
ChoicesByUrl - A custom property value is lost fix #8783 (#8784)
Browse files Browse the repository at this point in the history
* ChoicesByUrl - A custom property value is lost fix #8783

* Fix compiling

* Fix compiling error #8783
  • Loading branch information
andrewtelnov authored Sep 6, 2024
1 parent 572829c commit 67c37bf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
63 changes: 22 additions & 41 deletions packages/survey-core/src/choicesRestful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]]) (<any>this)[properties[i]] = json[properties[i]];
}
private getAllPropertiesNames(): Array<string> {
const res = new Array<string>();
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 => {
(<any>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 ((<any>this)[properties[i]])
res[properties[i]] = (<any>this)[properties[i]];
}
return res;
const res: any = {};
let hasValue = false;
this.getAllPropertiesNames().forEach(name => {
const val = (<any>this)[name];
if(!this.isValueEmpty(val) && val !== this.getDefaultPropertyValue(name)) {
res[name] = val;
hasValue = true;
}
});
return hasValue ? res : null;
}
/**
* A RESTful service's URL.
Expand Down Expand Up @@ -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 ((<any>this)[properties[i]]) (<any>this)[properties[i]] = "";
}
this.setData(undefined);
}
protected beforeSendRequest() {
this.isRunningValue = true;
Expand Down
16 changes: 16 additions & 0 deletions packages/survey-core/tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <QuestionCheckboxModel>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");
});

0 comments on commit 67c37bf

Please sign in to comment.