100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fix required booleans causing asset is not valid error (#2439)
Koen7f4fcfbc8369
diff --git a/ui/component/or-json-forms/src/controls/control-input-element.ts b/ui/component/or-json-forms/src/controls/control-input-element.tsindex 3137e0a40..a0d447229 100644--- a/ui/component/or-json-forms/src/controls/control-input-element.ts+++ b/ui/component/or-json-forms/src/controls/control-input-element.ts@@ -60,6 +60,10 @@ export class ControlInputElement extends ControlBaseElement {this.inputType = InputType.JSON;} else if (isBooleanControl(uischema, schema, context)) {this.inputType = InputType.CHECKBOX;+ if (typeof this.data !== "boolean") {+ this.data = Boolean(this.data);+ this.handleChange(this.path, this.data);+ }} else if (isNumberControl(uischema, schema, context) || isIntegerControl(uischema, schema, context)) {step = isNumberControl(uischema, schema, context) ? 0.1 : 1;this.inputType = InputType.NUMBER;diff --git a/ui/component/or-json-forms/test/fixtures/index.ts b/ui/component/or-json-forms/test/fixtures/index.tsindex 7b863b45f..44e3ce559 100644--- a/ui/component/or-json-forms/test/fixtures/index.ts+++ b/ui/component/or-json-forms/test/fixtures/index.ts@@ -1,169 +1,256 @@import { withPage, ct as base, expect, Page, Locator, SharedComponentTestFixtures } from "@openremote/test";+import { JsonSchema7, OrJSONForms } from "@openremote/or-json-forms";import * as Util from "@openremote/core/lib/util";+interface WalkFormOptions {+ /**+ * Whether all possible object properties should be added.+ * If falsy, then only the specified `or:test:props` are added.+ * @example+ * ```ts+ * "or:test:props": ["prop1", "prop2"]+ * ```+ */+ selectAllProps?: boolean;+}++export interface JsonSchema extends JsonSchema7 {+ /**+ * Describes what properties should be manually selected by {@link JsonForms#walkForm}+ */+ "or:test:props"?: string[];+ /**+ * Describes what value to set in {@link JsonForms#walkForm}+ */+ "or:test:value"?: any;+ /**+ * Describes how many items to add to an array control {@link JsonForms#walkForm}+ */+ "or:test:item:count"?: number;+ /**+ * The following overwrites the {@link JsonSchema7} types with {@link JsonSchema}+ */+ /***/+ additionalItems?: boolean | JsonSchema;+ items?: JsonSchema | JsonSchema[];+ additionalProperties?: boolean | JsonSchema;+ /**+ * Holds simple JSON Schema definitions for+ * referencing from elsewhere.+ */+ definitions?: {+ [key: string]: JsonSchema;+ };+ /**+ * The keys that can exist on the object with the+ * json schema that should validate their value+ */+ properties?: {+ [property: string]: JsonSchema;+ };+ /**+ * The key of this object is a regex for which+ * properties the schema applies to+ */+ patternProperties?: {+ [pattern: string]: JsonSchema;+ };+ /**+ * If the key is present as a property then the+ * string of properties must also be present.+ * If the value is a JSON Schema then it must+ * also be valid for the object if the key is+ * present.+ */+ dependencies?: {+ [key: string]: JsonSchema | string[];+ };+ allOf?: JsonSchema[];+ anyOf?: JsonSchema[];+ oneOf?: JsonSchema[];+ /**+ * The entity being validated must not match this schema+ */+ not?: JsonSchema;+ contains?: JsonSchema;+ propertyNames?: JsonSchema;+ if?: JsonSchema;+ then?: JsonSchema;+ else?: JsonSchema;+}++type Path = (string | number)[];+type Parent = "array" | "object"; // TODO: remove when or-json-forms-array-control always renders titles+export class JsonForms {- constructor(private readonly page: Page) {}-- /**- * Exhaust every `or-json-forms` option based on the provided JSONSchema. The following is done:- * - Adds new items to arrays and parameters for objects- * - TODO: tests validation of each field- * - TODO: builds an expected output and compares this with the JSON at the end- * - TODO: moves specific fields- * - TODO: removes specific fields- *- * @param locator The root element of the `or-json-forms` instance to test.- * @param schema The same JSONSchema used to generate the form.- * @param options Options to fill out in the forms without explicitly defining the values.- * @param path The path to the current node.- * @param item The item index used to locate array items.- * @param expected The expected JSON output built up during recursion.- */- async walkForm(- locator: Locator,- schema: any,- options: {- selectAllProps?: boolean;- itemCount?: number;- } = {- selectAllProps: false,- itemCount: 0,- },- path: (string | number)[] = [],- item = 0,- expected?: any- ) {- const dialog = this.page.locator("or-mwc-dialog");-- switch (schema.type) {- case "array": {+ constructor(private readonly page: Page, private dialog: Locator) {+ this.dialog = this.page.locator("or-mwc-dialog");+ }++ /**+ * Exhaust every `or-json-forms` option based on the provided JSONSchema. The following is done:+ * - Adds new items to arrays and parameters for objects+ * @todo builds an expected output and compares this with the JSON at the end+ * @todo moves specific fields+ * @todo removes specific fields+ *+ * @param locator The root element of the `or-json-forms` instance to test.+ * @param schema The same JSONSchema used to generate the form.+ * @param options Options to fill out in the forms without explicitly defining the values.+ * @param path The path to the current node.+ * @param item The item index used to locate array items.+ * @param expected not implemented - The expected JSON output built up during recursion.+ */+ async walkForm(+ locator: Locator,+ schema: JsonSchema,+ options?: WalkFormOptions,+ path: Path = [],+ item = 0,+ parent?: Parent, // TODO: remove when or-json-forms-array-control always renders titles+ expected?: any+ ) {+ switch (schema.type) {+ case "array": {+ locator = await this.walkArray(locator, schema, path, item);+ break;+ }+ case "object": {+ locator = await this.walkObject(locator, schema, options, path, item);+ break;+ }+ case "string":+ case "number":+ case "integer":+ case "boolean": {+ await this.walkPrimitive(locator, schema, item, parent);+ break;+ }+ }++ if (schema.type === "array") {+ if (!Array.isArray(schema?.items) && schema.items?.oneOf) {+ let i = 0;+ for (const prop of Object.values(schema.items.oneOf)) {+ await this.walkForm(locator, prop as JsonSchema, options, [...path], i, "array");+ i++;+ }+ } else {+ for (let i = 0; i < (schema?.["or:test:item:count"] ?? 0); i++) {+ await this.walkForm(locator, schema.items as JsonSchema, options, [...path], i, "array");+ }+ }+ // await locator.getByRole("button", { name: "json" }).first().click();+ } else if (schema.type === "object" && !schema.patternProperties) {+ let arrayControls = 0;+ let verticalLayouts = 0;+ for (const [key, prop] of Object.entries<any>(schema.properties ?? {})) {+ if (schema["or:test:props"] && !schema["or:test:props"].includes(key)) continue;+ if (["type", "id"].includes(key)) continue;+ if (prop.type === "array") {+ await this.walkForm(locator, prop, options, [...path], arrayControls);+ arrayControls++;+ } else if (prop.type === "object") {+ await this.walkForm(locator, prop, options, [...path], verticalLayouts);+ verticalLayouts++;+ } else {+ await this.walkForm(locator, prop, options, [...path]);+ }+ }+ // await locator.getByRole("button", { name: "json" }).first().click();+ }++ if (schema?.type === "array" || schema?.type === "object") {+ // await expect(locator.locator("or-ace-editor")).toContainText(JSON.stringify(expected, null, 2));+ }+ }++ public async getValidity(form: Locator) {+ return form.evaluate((el: OrJSONForms) => el.checkValidity());+ }++ private async walkArray(locator: Locator, schema: JsonSchema, path: Path, item: number) {locator = locator.locator("or-json-forms-array-control").nth(item);path.push("or-json-forms-array-control", item);await locator.locator("or-collapsible-panel").click();- if (schema.items.oneOf) {- for (const prop of Object.values<any>(schema.items.oneOf)) {- await locator.getByRole("button", { name: "Add Item" }).click();- await dialog.locator("li").getByText(prop.title, { exact: true }).click();- await dialog.getByRole("button", { name: "Add", exact: true }).click();- }+ if (!Array.isArray(schema?.items) && schema.items?.oneOf) {+ for (const prop of Object.values<any>(schema.items.oneOf)) {+ await locator.getByRole("button", { name: "Add Item" }).click();+ await this.dialog.locator("li").getByText(prop.title, { exact: true }).click();+ await this.dialog.getByRole("button", { name: "Add", exact: true }).click();+ }} else {- for (let i = 0; i < schema["or:test:item:count"]; i++) {- await locator.getByRole("button", { name: "Add Item" }).click();- }+ for (let i = 0; i < (schema?.["or:test:item:count"] ?? 0); i++) {+ await locator.getByRole("button", { name: "Add Item" }).click();+ }}- break;- }- case "object": {+ return locator;+ }++ private async walkObject(locator: Locator, schema: JsonSchema, options: WalkFormOptions, path: Path, item: number) {locator = locator.locator("or-json-forms-vertical-layout").nth(item);path.push("or-json-forms-vertical-layout", item);await locator.locator("or-collapsible-panel").click();+if (schema.patternProperties) {- await locator.getByRole("button", { name: "Add Parameter" }).click();- await this.page.waitForTimeout(1000);- await this.page- .getByRole("alertdialog", { name: " - Add" })- .locator("label", { hasText: "Key" })- .pressSequentially("test");- await dialog.getByRole("button", { name: "Add", exact: true }).click();- await this.page.waitForTimeout(2000);- } else {- for (const key of options?.selectAllProps ? Object.keys(schema.properties) : schema["or:test:props"]) {- if (key === "type" || key === "id" || schema.required?.includes(key)) continue;await locator.getByRole("button", { name: "Add Parameter" }).click();- await dialog- .locator("or-mwc-list li")- .getByText(Util.camelCaseToSentenceCase(key), { exact: true })- .click();- const anyOfPicker = dialog.locator("#schema-picker or-mwc-input");- if (await anyOfPicker.isVisible()) {- await anyOfPicker.click();- await anyOfPicker.locator("li").first().click();+ await this.page+ .getByRole("alertdialog", { name: " - Add" })+ .locator("label", { hasText: "Key" })+ .pressSequentially("test");+ await this.dialog.getByRole("button", { name: "Add", exact: true }).click();+ } else {+ const properties = options?.selectAllProps+ ? Object.keys(schema.properties ?? {})+ : schema?.["or:test:props"] ?? [];++ for (const key of properties) {+ if (key === "type" || key === "id" || schema.required?.includes(key)) continue;+ await locator.getByRole("button", { name: "Add Parameter" }).click();++ const name = Util.camelCaseToSentenceCase(key);+ await this.dialog.locator("or-mwc-list li").getByText(name, { exact: true }).click();+ const anyOfPicker = this.dialog.locator("#schema-picker or-mwc-input");+ if (await anyOfPicker.isVisible()) {+ await anyOfPicker.click();+ await anyOfPicker.locator("li").first().click();+ }+ await this.dialog.getByRole("button", { name: "Add", exact: true }).click();}- await dialog.getByRole("button", { name: "Add", exact: true }).click();- }}- break;- }- case "string": {- /// TODO: remove condition when or-json-forms-array-control always renders titles- const options = !path.length ? { name: schema.title } : {};- locator = locator.getByRole("textbox", options).nth(item);- await expect(locator).toBeVisible();- await locator.fill(schema["or:test:value"] ?? "test");- await expect(locator).toHaveValue(schema["or:test:value"] ?? "test");- break;- }- case "number": {- const options = !path.length ? { name: schema.title } : {};- locator = locator.getByRole("spinbutton", options).nth(item);- await expect(locator).toBeVisible();- await locator.fill(`${schema["or:test:value"] ?? 0}`);- await expect(locator).toHaveValue(`${schema["or:test:value"] ?? 0}`);- break;- }- case "integer": {- const options = !path.length ? { name: schema.title } : {};- locator = locator.getByRole("spinbutton", options).nth(item);- await expect(locator).toBeVisible();- await locator.fill(`${schema["or:test:value"] ?? 0}`);- await expect(locator).toHaveValue(`${schema["or:test:value"] ?? 0}`);- break;- }- case "boolean": {- const options = !path.length ? { name: schema.title } : {};- locator = locator.getByRole("checkbox", options).nth(item);- await expect(locator).toBeVisible();- await expect(locator).not.toBeChecked();- if (schema["or:test:value"]) {- await locator.check();- await expect(locator).toBeChecked();- }- break;- }+ return locator;}- if (schema.type === "array") {- if (schema.items.oneOf) {- let i = 0;- for (const prop of Object.values(schema.items.oneOf)) {- await this.walkForm(locator, prop, options, [...path], i);- i++;- }- } else {- for (let i = 0; i < schema["or:test:item:count"]; i++) {- await this.walkForm(locator, schema.items, options, [...path], i);- }- }- // await locator.getByRole("button", { name: "json" }).first().click();- } else if (schema.type === "object" && !schema.patternProperties) {- let arrayControls = 0;- let verticalLayouts = 0;- for (const [key, prop] of Object.entries<any>(schema.properties)) {- if (schema["or:test:props"] && !schema["or:test:props"].includes(key)) continue;- if (["type", "id"].includes(key)) continue;- if (prop.type === "array") {- await this.walkForm(locator, prop, options, [...path], arrayControls);- arrayControls++;- } else if (prop.type === "object") {- await this.walkForm(locator, prop, options, [...path], verticalLayouts);- verticalLayouts++;+ private async walkPrimitive(locator: Locator, schema: JsonSchema, item: number, parent?: Parent) {+ const { type, role, fallback } = [+ { type: "boolean", role: "checkbox", fallback: false },+ { type: "integer", role: "spinbutton", fallback: 0 },+ { type: "number", role: "spinbutton", fallback: 0 },+ { type: "string", role: "textbox", fallback: "test" },+ ].find(({ type }) => type === schema.type)!;++ // TODO: remove first condition when or-json-forms-array-control always renders titles+ const options = parent !== "array" && schema.title ? { name: schema.title, exact: true } : {};+ locator = locator.getByRole(role as any, options).nth(item);+ await expect(locator).toBeVisible();++ if (type === "boolean") {+ await expect(locator).not.toBeChecked();+ if (schema["or:test:value"]) {+ await locator.check();+ await expect(locator).toBeChecked();+ }} else {- await this.walkForm(locator, prop, options, [...path]);+ await locator.fill(String(schema["or:test:value"] ?? fallback));+ await expect(locator).toHaveValue(String(schema["or:test:value"] ?? fallback));}- }- // await locator.getByRole("button", { name: "json" }).first().click();- }-- if (schema.type === "array" && schema.type === "object") {- // await expect(locator.locator("or-ace-editor")).toContainText(JSON.stringify(expected, null, 2));}- }}interface ComponentFixtures extends SharedComponentTestFixtures {- jsonForms: JsonForms;+ jsonForms: JsonForms;}export const ct = base.extend<ComponentFixtures>({- jsonForms: withPage(JsonForms),+ jsonForms: withPage(JsonForms),});diff --git a/ui/component/or-json-forms/test/fixtures/schemas.ts b/ui/component/or-json-forms/test/fixtures/schemas.tsindex 499cf1553..89f405641 100644--- a/ui/component/or-json-forms/test/fixtures/schemas.ts+++ b/ui/component/or-json-forms/test/fixtures/schemas.ts@@ -1,103 +1,111 @@-export const schemas = [- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "String",- type: "string",- "or:test:value": "test",- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Boolean",- type: "boolean",- "or:test:value": true,- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Integer",- type: "integer",- "or:test:value": 1,- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Number",- type: "number",- "or:test:value": 1,- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Object",- type: "object",- "or:test:props": ["value"],- properties: {- value: {+import { JsonSchema } from ".";++export const schemas: JsonSchema[] = [+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "String",type: "string","or:test:value": "test",- },},- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Array",- type: "array",- "or:test:item:count": 1,- items: {- title: "String",- type: "string",- "or:test:value": "test",+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "Boolean",+ type: "boolean",+ "or:test:value": true,},- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "3 Dimensional Array",- type: "array",- "or:test:item:count": 2,- items: {- title: "2 Dimensional Array",- type: "array",- "or:test:item:count": 2,- items: {+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "Integer",+ type: "integer",+ "or:test:value": 1,+ },+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "Number",+ type: "number",+ "or:test:value": 1,+ },+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "Object",+ type: "object",+ "or:test:props": ["value"],+ properties: {+ value: {+ type: "string",+ "or:test:value": "test",+ },+ },+ },+ {+ $schema: "http://json-schema.org/draft-07/schema#",title: "Array",type: "array",- "or:test:item:count": 2,+ "or:test:item:count": 1,items: {- title: "String",- type: "string",- "or:test:value": "test",- },- },- },- },- {- $schema: "http://json-schema.org/draft-07/schema#",- title: "Complex",- type: "array",- "or:test:item:count": 2,- items: {- title: "Object",- type: "object",- "or:test:props": ["string", "array", "boolean"],- properties: {- string: {- title: "String",- type: "string",- "or:test:value": "test",- },- boolean: {- title: "Boolean",- type: "boolean",- "or:test:value": true,- },- array: {- title: "Array",- type: "array",- "or:test:item:count": 2,- items: {title: "String",type: "string","or:test:value": "test",- },},- },},- },+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "3 Dimensional Array",+ type: "array",+ "or:test:item:count": 2,+ items: {+ title: "2 Dimensional Array",+ type: "array",+ "or:test:item:count": 2,+ items: {+ title: "Array",+ type: "array",+ "or:test:item:count": 2,+ items: {+ title: "String",+ type: "string",+ "or:test:value": "test",+ },+ },+ },+ },+ {+ $schema: "http://json-schema.org/draft-07/schema#",+ title: "Complex",+ type: "array",+ "or:test:item:count": 2,+ items: {+ title: "Object",+ type: "object",+ required: ["unresolvedBoolean"],+ "or:test:props": ["string", "array", "boolean"],+ properties: {+ string: {+ title: "String",+ type: "string",+ "or:test:value": "test",+ },+ boolean: {+ title: "Boolean",+ type: "boolean",+ "or:test:value": true,+ },+ // Should resolve to false+ unresolvedBoolean: {+ title: "Unresolved boolean",+ type: "boolean",+ },+ array: {+ title: "Array",+ type: "array",+ "or:test:item:count": 2,+ items: {+ title: "String",+ type: "string",+ "or:test:value": "test",+ },+ },+ },+ },+ },];diff --git a/ui/component/or-json-forms/test/or-json-forms.test.ts b/ui/component/or-json-forms/test/or-json-forms.test.tsindex 9efef59a9..3bc80215a 100644--- a/ui/component/or-json-forms/test/or-json-forms.test.ts+++ b/ui/component/or-json-forms/test/or-json-forms.test.ts@@ -1,36 +1,38 @@import { ct } from "./fixtures/index";+import { expect } from "@openremote/test";import { OrJSONForms, StandardRenderers } from "@openremote/or-json-forms";import { schemas } from "./fixtures/schemas";const typeValueMap = new Map()- .set("array", [])- .set("boolean", false)- .set("object", {})- .set("number", 0)- .set("integer", 0)- .set("string", "");+ .set("array", [])+ .set("boolean", false)+ .set("object", {})+ .set("number", 0)+ .set("integer", 0)+ .set("string", "");ct.beforeEach(async ({ shared }) => {- await shared.fonts();- await shared.locales();+ await shared.fonts();+ await shared.locales();});for (const schema of schemas) {- ct(`Should render form for: ${schema.title}`, async ({ mount, jsonForms }) => {- const component = await mount(OrJSONForms, {- props: {- uischema: { type: "Control", scope: "#" } as any,- schema,- data: typeValueMap.get(schema.type),- renderers: StandardRenderers,- onChange: () => null,- readonly: false,- label: schema.title,- required: false,- },- on: {},+ ct(`Should render form for: ${schema.title}`, async ({ mount, jsonForms }) => {+ const component = await mount(OrJSONForms, {+ props: {+ uischema: { type: "Control", scope: "#" } as any,+ schema,+ data: typeValueMap.get(schema.type),+ renderers: StandardRenderers,+ onChange: () => null,+ readonly: false,+ label: schema.title,+ required: false,+ },+ on: {},+ });+ await jsonForms.walkForm(component, schema);+ await expect(jsonForms.getValidity(component)).resolves.toBeTruthy();});- await jsonForms.walkForm(component, schema);- });}