Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect JSON Schema for property with choices fixed #7167 #7210

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
Loading