Skip to content

Commit

Permalink
Allow to parse with json error on setting an object to an array prope…
Browse files Browse the repository at this point in the history
…rty fix #7254
  • Loading branch information
andrewtelnov committed Oct 30, 2023
1 parent 283fe5e commit ba8cee2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ export class JsonMetadataClass {
}
if (propInfo.baseClassName) {
prop.baseClassName = propInfo.baseClassName;
prop.isArray = true;
}
if (propInfo.classNamePart) {
prop.classNamePart = propInfo.classNamePart;
Expand Down Expand Up @@ -1482,6 +1483,11 @@ export class JsonRequiredPropertyError extends JsonError {
);
}
}
export class JsonRequiredArrayPropertyError extends JsonError {
constructor(public propertyName: string, public className: string) {
super("arrayproperty", "The property '" + propertyName + "' should be an array in '" + className + "'.");
}
}

export class JsonObject {
private static typePropertyName = "type";
Expand Down Expand Up @@ -1531,7 +1537,7 @@ export class JsonObject {
}
continue;
}
this.valueToObj(jsonObj[key], obj, property);
this.valueToObj(jsonObj[key], obj, property, jsonObj);
}
if (obj.endLoadingFromJson) {
obj.endLoadingFromJson();
Expand Down Expand Up @@ -1629,13 +1635,18 @@ export class JsonObject {
}
}
}
public valueToObj(value: any, obj: any, property: JsonObjectProperty) {
public valueToObj(value: any, obj: any, property: JsonObjectProperty, jsonObj?: any): void {
if (value === null || value === undefined) return;
this.removePos(property, value);
if (property != null && property.hasToUseSetValue) {
property.setValue(obj, value, this);
return;
}
if(property.isArray && !Array.isArray(value) && !!value) {
value = [value];
const propName = !!jsonObj && property.alternativeName && !!jsonObj[property.alternativeName] ? property.alternativeName : property.name;
this.addNewError(new JsonRequiredArrayPropertyError(propName, obj.getType()), !!jsonObj ? jsonObj: value, obj);
}
if (this.isValueArray(value)) {
this.valueToArray(value, obj, property.name, property);
return;
Expand Down
4 changes: 2 additions & 2 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7485,8 +7485,8 @@ Serializer.addClass("survey", [
{ name: "loadingHtml:html", serializationProperty: "locLoadingHtml" },
{ name: "pages:surveypages", className: "page" },
{
name: "questions",
alternativeName: "elements",
name: "elements",
alternativeName: "questions",
baseClassName: "question",
visible: false,
isLightSerializable: false,
Expand Down
27 changes: 27 additions & 0 deletions tests/jsonobjecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3032,4 +3032,31 @@ QUnit.test("Check that .toJSON returns clean structure for all question types",
}
}
}
});
QUnit.test("Add a quesition into page elements array", function (assert) {
const prop = Serializer.findProperty("page", "elements");
assert.equal(prop.isArray, true, "Elements is an array");
const survey = new SurveyModel({
pages: [
{
elements: { type: "text", name: "q1" }
}
]
});
assert.equal(survey.pages.length, 1, "There is one page");
assert.equal(survey.pages[0].elements.length, 1, "There is one element in the page");
assert.equal(survey.pages[0].elements[0].name, "q1", "Element has a correct name");
assert.equal(survey.jsonErrors.length, 1, "There is a JSON error");
});
QUnit.test("Add a quesition into survey questions array", function (assert) {
const prop = Serializer.findProperty("page", "elements");
assert.equal(prop.isArray, true, "Elements is an array");
const survey = new SurveyModel({
questions: { type: "text", name: "q1" }
});
assert.equal(survey.pages.length, 1, "There is one page");
assert.equal(survey.pages[0].elements.length, 1, "There is one element in the page");
assert.equal(survey.pages[0].elements[0].name, "q1", "Element has a correct name");
assert.equal(survey.jsonErrors.length, 1, "There is a JSON error");
assert.equal((<any>survey.jsonErrors[0]).propertyName, "questions", "Correct property name");
});

0 comments on commit ba8cee2

Please sign in to comment.