Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Show actual query field types in Builder (#6597)
Oleg Isonen2c7772c38608
diff --git a/apps/builder/app/builder/features/settings-panel/asset-query-form.test.tsx b/apps/builder/app/builder/features/settings-panel/asset-query-form.test.tsxindex bc75bc025c..18b96c11a1 100644--- a/apps/builder/app/builder/features/settings-panel/asset-query-form.test.tsx+++ b/apps/builder/app/builder/features/settings-panel/asset-query-form.test.tsx@@ -258,6 +258,11 @@ test("loads the external query schema declared by OpenAPI", async () => {types: ["string"],occurrences: 1,},+ draft: {+ queryPath: ["properties", "draft"],+ types: ["boolean"],+ occurrences: 1,+ },},},});@@ -276,6 +281,26 @@ test("loads the external query schema declared by OpenAPI", async () => {expect(definition.fields).toContainEqual(expect.objectContaining({ path: ["properties", "title"] }));+ const configuration =+ createDefaultStructuredAssetQueryResourceConfiguration();+ const where = {+ field: ["properties", "draft"],+ operator: "ne" as const,+ };+ const validate = (value: string) =>+ getAssetQueryConfigurationValidation({+ configuration: { ...configuration, where: { ...where, value } },+ scope: {},+ definition,+ });+ expect((await validate("true")).issues).toEqual([]);+ expect((await validate('"true"')).issues).toEqual([+ expect.objectContaining({+ severity: "warning",+ code: "INCOMPATIBLE_OBSERVED_VALUE",+ path: ["query", "where", "value"],+ }),+ ]);const manyControls = configureAssetQueryDefinition({definition,paths: [],diff --git a/packages/protocol/src/asset-resource-api.test.ts b/packages/protocol/src/asset-resource-api.test.tsindex 6cedafb7f4..47df7d494b 100644--- a/packages/protocol/src/asset-resource-api.test.ts+++ b/packages/protocol/src/asset-resource-api.test.ts@@ -39,7 +39,11 @@ const createDocument = () =>"https://api.example.com/rest/assets/query-schema.json?projectId=project",});-const loadConfiguration = (catalog = defaultCatalog) =>+const loadConfiguration = (+ catalog: Parameters<+ typeof createAssetQueryJsonSchema+ >[0]["catalog"] = defaultCatalog+) =>loadOpenApiQueryDefinition({document: createDocument(),documentUrl: "https://api.example.com/rest/assets/openapi.json",@@ -162,6 +166,47 @@ describe("Assets OpenAPI description", () => {}});+ test("preserves observed field types in the query definition", async () => {+ const configuration = await loadConfiguration({+ ...defaultCatalog,+ fields: {+ draft: {+ queryPath: ["properties", "draft"],+ types: ["boolean"],+ occurrences: 1,+ },+ empty: {+ queryPath: ["properties", "empty"],+ types: ["null"],+ occurrences: 1,+ },+ score: {+ queryPath: ["properties", "score"],+ types: ["number", "null"],+ occurrences: 1,+ },+ },+ });++ expect(configuration.fields).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ path: ["size"], types: ["number"] }),+ expect.objectContaining({+ path: ["properties", "draft"],+ types: ["boolean"],+ }),+ expect.objectContaining({+ path: ["properties", "empty"],+ types: ["null"],+ }),+ expect.objectContaining({+ path: ["properties", "score"],+ types: ["number", "null"],+ }),+ ])+ );+ });+test("describes callable REST paths and stable share-token authentication", () => {const document = createDocument();diff --git a/packages/protocol/src/asset-resource-api.ts b/packages/protocol/src/asset-resource-api.tsindex a17026a8b8..761db55f47 100644--- a/packages/protocol/src/asset-resource-api.ts+++ b/packages/protocol/src/asset-resource-api.ts@@ -475,15 +475,17 @@ const decorateAssetQuerySchema = ({{operators: AssetQueryOperator[];fields: AssetOpenApiField[];+ types: readonly AssetObservedFieldType[];reference: string;}>();for (const field of fields) {const operators = getAssetQueryOperatorsForFieldTypes(field.types);- const key = operators.join(",");+ const key = `${operators.join(",")}:${field.types.join(",")}`;const group = fieldGroups.get(key) ?? {operators,fields: [],+ types: field.types,reference: definitionReference(`AssetQueryField${fieldGroups.size}`),};group.fields.push(field);@@ -512,28 +514,31 @@ const decorateAssetQuerySchema = ({],},],- ...[...fieldGroups.values()].map(({ fields: groupFields }, index) => [- `AssetQueryField${index}`,- {- type: "array",- minItems: 1,- maxItems: assetResourceLimits.fieldPathDepth,- items: { type: "string", minLength: 1 },- oneOf: [- ...groupFields.flatMap((field) =>- field.label === undefined- ? []- : [{ const: field.path, title: field.label }]- ),- ...(() => {- const paths = groupFields.flatMap((field) =>- field.label === undefined ? [field.path] : []- );- return paths.length === 0 ? [] : [{ enum: paths }];- })(),- ],- },- ]),+ ...[...fieldGroups.values()].map(+ ({ fields: groupFields, types }, index) => [+ `AssetQueryField${index}`,+ {+ type: "array",+ "x-webstudio-field-types": types,+ minItems: 1,+ maxItems: assetResourceLimits.fieldPathDepth,+ items: { type: "string", minLength: 1 },+ oneOf: [+ ...groupFields.flatMap((field) =>+ field.label === undefined+ ? []+ : [{ const: field.path, title: field.label }]+ ),+ ...(() => {+ const paths = groupFields.flatMap((field) =>+ field.label === undefined ? [field.path] : []+ );+ return paths.length === 0 ? [] : [{ enum: paths }];+ })(),+ ],+ },+ ]+ ),]);const visit = (value: unknown, property?: string): unknown => {diff --git a/packages/query-builder/src/openapi.ts b/packages/query-builder/src/openapi.tsindex ea53726131..2590185ce6 100644--- a/packages/query-builder/src/openapi.ts+++ b/packages/query-builder/src/openapi.ts@@ -119,9 +119,18 @@ const getChoices = (document: JsonObject, schema: JsonObject) => {const getFieldChoices = (document: JsonObject,- schema: JsonObject-): Array<{ path: string[]; label: string }> => {+ schema: JsonObject,+ inheritedTypes?: string[]+): Array<{ path: string[]; label: string; types?: string[] }> => {const resolved = resolveSchema(document, schema);+ const types =+ Array.isArray(resolved["x-webstudio-field-types"]) &&+ resolved["x-webstudio-field-types"].length > 0 &&+ resolved["x-webstudio-field-types"].every(+ (type) => typeof type === "string" && type.length > 0+ )+ ? (resolved["x-webstudio-field-types"] as string[])+ : inheritedTypes;if (Array.isArray(resolved.const) &&resolved.const.every((segment) => typeof segment === "string")@@ -129,6 +138,7 @@ const getFieldChoices = (return [{path: resolved.const,+ types,label:typeof resolved.title === "string"? resolved.title@@ -147,6 +157,7 @@ const getFieldChoices = (return resolved.enum.map((value) => ({path: value as string[],label: (value as string[]).join(" / "),+ types,}));}const choices = Array.isArray(resolved.oneOf)@@ -157,7 +168,7 @@ const getFieldChoices = (return choices === undefined? []: choices.flatMap((choice) =>- isObject(choice) ? getFieldChoices(document, choice) : []+ isObject(choice) ? getFieldChoices(document, choice, types) : []);};@@ -622,7 +633,7 @@ const createDefinition = ({fields.set(fieldKey, {path: choice.path,label: choice.label,- types: ["string"],+ types: choice.types ?? existing?.types ?? ["string"],operators: [...new Set([...(existing?.operators ?? []),@@ -697,7 +708,7 @@ const createDefinition = ({fields.set(fieldKey, {path: choice.path,label: choice.label,- types: ["string"],+ types: choice.types ?? ["string"],});}}