Skip to content

Commit

Permalink
Incorrect JSON Schema for property with choices fixed #7167 (#7210)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Oct 21, 2023
1 parent 7d89457 commit eaebc6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ export class JsonMetadata {
if (prop.hasChoices) {
const enumRes = prop.getChoices(null);
if(Array.isArray(enumRes) && enumRes.length > 0) {
res.enum = enumRes;
res.enum = this.getChoicesValues(enumRes);
}
}
if(!!refType) {
Expand Down Expand Up @@ -1388,6 +1388,17 @@ export class JsonMetadata {
res.required = chemaProps.required;
}
}
private getChoicesValues(enumRes: Array<any>): Array<any> {
const res = new Array<any>();
enumRes.forEach(item => {
if(typeof item === "object" && item.value !== undefined) {
res.push(item.value);
} else {
res.push(item);
}
});
return res;
}
}
export class JsonError {
public description: string = "";
Expand Down
15 changes: 13 additions & 2 deletions tests/jsonSchemaTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { SurveyModel } from "../src/survey";
export default QUnit.module("JsonSchemaTests");

QUnit.test("generate survey schema", function (assert) {
Serializer.findClass;
var schema = Serializer.generateSchema();
Serializer.addProperty("survey", {
name: "customSurveyProperty2",
category: "general",
visibleIndex: 0,
choices: [{ text: "Value A", value: "a" }, "b"]
});
const schema = Serializer.generateSchema();
assert.equal(schema.title, "SurveyJS Library json schema");
assert.equal(schema.properties.surveyId.type, "string", "surveyId is string");
assert.equal(
Expand Down Expand Up @@ -192,4 +197,10 @@ QUnit.test("generate survey schema", function (assert) {
assert.equal(lostStringProp.default.type, "string", "has default in locString properties");
assert.equal(lostStringProp.en.type, "string", "has en in locString properties");
assert.equal(lostStringProp.fr.type, "string", "has fr in locString properties");

const customProp2 = schema.properties.customSurveyProperty2;
assert.ok(customProp2);
assert.equal(customProp2.type, "string", "customProp2.type");
assert.deepEqual(customProp2.enum, ["a", "b"], "customProp2.enum");
Serializer.removeProperty("survey", "customSurveyProperty2");
});

0 comments on commit eaebc6b

Please sign in to comment.