diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd7229..b384063 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [1.0.10] - 2026-09-04 + +### Added + +### Changed + +### Fixed +- Re-added map entries and object list items are seeded with their schema defaults, including a value for every non-nullable boolean. + ## [1.0.9] - 2026-08-24 ### Added diff --git a/package.json b/package.json index 1a2377f..b61f40c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "stalwart-webui", "private": true, - "version": "1.0.9", + "version": "1.0.10", "description": "Stalwart WebUI", "type": "module", "scripts": { diff --git a/src/components/forms/FieldWidget.tsx b/src/components/forms/FieldWidget.tsx index 130d45b..0e1e351 100644 --- a/src/components/forms/FieldWidget.tsx +++ b/src/components/forms/FieldWidget.tsx @@ -8,7 +8,6 @@ import { useState, useEffect, useMemo, type KeyboardEvent } from 'react'; import { useTranslation } from 'react-i18next'; import { useBufferedValue, useResetOnChange } from '@/hooks/useBufferedValue'; import ReactMarkdown from 'react-markdown'; - import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Button } from '@/components/ui/button'; @@ -21,14 +20,10 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/comp import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Combobox, type ComboboxOption } from '@/components/ui/combobox'; import { Calendar } from '@/components/ui/calendar'; - import { Plus, X, Eye, EyeOff, Loader2, Search, Check, ChevronRight, Calendar as CalendarIcon } from 'lucide-react'; - import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; - import { ExpressionEditor } from '@/components/expression/ExpressionEditor'; import { OtpAuthField } from '@/components/forms/OtpAuthField'; - import { bytesToHuman, humanToBytes, @@ -39,7 +34,13 @@ import { SIZE_UNITS, DURATION_UNITS, } from '@/lib/durationFormat'; -import { resolveSchema, resolveVariantForm, resolveObject, buildEmbeddedDefaults } from '@/lib/schemaResolver'; +import { + resolveSchema, + resolveVariantForm, + resolveObject, + buildEmbeddedDefaults, + buildNewObjectValue, +} from '@/lib/schemaResolver'; import { cn } from '@/lib/utils'; import { useAccountStore } from '@/stores/accountStore'; import { useEffectiveEdition } from '@/components/forms/FormEditionContext'; @@ -1539,16 +1540,7 @@ function ObjectListField({ const addItem = () => { const nextIndex = entries.length > 0 ? Math.max(...entries.map(([k]) => parseInt(k))) + 1 : 0; - let defaults: Record = {}; - if (resolvedSchema.type === 'single' && resolvedSchema.fields.defaults) { - defaults = { ...resolvedSchema.fields.defaults }; - } else if (resolvedSchema.type === 'multiple' && resolvedSchema.variants[0]) { - defaults = { '@type': resolvedSchema.variants[0].name }; - if (resolvedSchema.variants[0].fields?.defaults) { - defaults = { ...defaults, ...resolvedSchema.variants[0].fields.defaults }; - } - } - onChange({ ...mapValue, [String(nextIndex)]: defaults }); + onChange({ ...mapValue, [String(nextIndex)]: buildNewObjectValue(schema, objectName) }); }; const removeItem = (key: string) => { @@ -1790,10 +1782,10 @@ function EnumMultiSelect({ enumName, items, onChange, readOnly, schema, minItems if (variants.length > 10) { const filtered = searchQuery ? variants.filter( - (v) => - v.label.toLowerCase().includes(searchQuery.toLowerCase()) || - v.name.toLowerCase().includes(searchQuery.toLowerCase()), - ) + (v) => + v.label.toLowerCase().includes(searchQuery.toLowerCase()) || + v.name.toLowerCase().includes(searchQuery.toLowerCase()), + ) : variants; return ( @@ -2021,7 +2013,7 @@ function MapField({ keyClass, valueClass, value, onChange, readOnly, schema, min if (valueClass.type === 'number') { defaultValue = 0; } else if (valueClass.type === 'object') { - defaultValue = {}; + defaultValue = buildNewObjectValue(schema, valueClass.objectName); } onChange({ ...mapValue, [key]: defaultValue }); diff --git a/src/lib/schemaResolver.test.ts b/src/lib/schemaResolver.test.ts index edbeb2b..5c9781e 100644 --- a/src/lib/schemaResolver.test.ts +++ b/src/lib/schemaResolver.test.ts @@ -15,6 +15,7 @@ import { deepMerge, buildCreateDefaults, buildEmbeddedDefaults, + buildNewObjectValue, } from './schemaResolver'; import { getDisplayProperty } from './schemaResolver'; @@ -865,3 +866,115 @@ describe('getDisplayProperty', () => { expect(getDisplayProperty(schema, 'x:NoLabel')).toBe('title'); }); }); + +const structSchema: Schema = { + objects: {}, + schemas: { + 'x:Service': { type: 'single', schemaName: 'x:Service' }, + 'x:Listener': { type: 'single', schemaName: 'x:Listener' }, + 'x:Tls': { type: 'single', schemaName: 'x:Tls' }, + 'x:Store': { + type: 'multiple', + variants: [ + { name: 'S3', label: 'S3', schemaName: 'x:S3Store' }, + { name: 'Manual', label: 'Manual' }, + ], + }, + }, + fields: { + 'x:Service': { + properties: { + hostname: { + description: '', + type: { type: 'string', format: 'string', nullable: true }, + update: 'mutable', + }, + cleartext: { description: '', type: { type: 'boolean' }, update: 'mutable' }, + }, + }, + 'x:Listener': { + properties: { + enabled: { description: '', type: { type: 'boolean' }, update: 'mutable' }, + proxied: { description: '', type: { type: 'boolean' }, update: 'mutable' }, + readOnly: { description: '', type: { type: 'boolean' }, update: 'serverSet' }, + tls: { description: '', type: { type: 'object', objectName: 'x:Tls' }, update: 'mutable' }, + fallback: { + description: '', + type: { type: 'object', objectName: 'x:Tls', nullable: true }, + update: 'mutable', + }, + }, + defaults: { + enabled: true, + }, + }, + 'x:Tls': { + properties: { + implicit: { description: '', type: { type: 'boolean' }, update: 'mutable' }, + certificateId: { + description: '', + type: { type: 'string', format: 'string', nullable: true }, + update: 'mutable', + }, + }, + }, + 'x:S3Store': { + properties: { + bucket: { description: '', type: { type: 'string', format: 'string' }, update: 'mutable' }, + allowInvalidCerts: { description: '', type: { type: 'boolean' }, update: 'mutable' }, + }, + defaults: { + bucket: 'stalwart', + }, + }, + }, + forms: {}, + lists: {}, + enums: {}, + dashboards: [], + layouts: [], +}; + +describe('buildNewObjectValue', () => { + it('seeds non-nullable booleans a struct has no defaults for', () => { + expect(buildNewObjectValue(structSchema, 'x:Service')).toEqual({ cleartext: false }); + }); + + it('keeps schema defaults and only fills the missing booleans', () => { + const result = buildNewObjectValue(structSchema, 'x:Listener'); + expect(result.enabled).toBe(true); + expect(result.proxied).toBe(false); + }); + + it('skips serverSet properties', () => { + expect(buildNewObjectValue(structSchema, 'x:Listener')).not.toHaveProperty('readOnly'); + }); + + it('recurses into non-nullable embedded objects and skips nullable ones', () => { + const result = buildNewObjectValue(structSchema, 'x:Listener'); + expect(result.tls).toEqual({ implicit: false }); + expect(result).not.toHaveProperty('fallback'); + }); + + it('seeds the first variant with its @type, defaults and booleans', () => { + expect(buildNewObjectValue(structSchema, 'x:Store')).toEqual({ + '@type': 'S3', + bucket: 'stalwart', + allowInvalidCerts: false, + }); + }); + + it('honours an explicit variant name', () => { + expect(buildNewObjectValue(structSchema, 'x:Store', 'Manual')).toEqual({ '@type': 'Manual' }); + }); + + it('returns an empty object for an unknown object name', () => { + expect(buildNewObjectValue(structSchema, 'x:Unknown')).toEqual({}); + }); + + it('still merges parent defaults into embedded children', () => { + const result = buildNewObjectValue(embeddedSchema, 'x:Model', 'FtrlCcfh'); + expect(result.featureL2Normalize).toBe(true); + expect((result.parameters as Record).numFeatures).toBe('20'); + }); +}); diff --git a/src/lib/schemaResolver.ts b/src/lib/schemaResolver.ts index d878628..c3ce487 100644 --- a/src/lib/schemaResolver.ts +++ b/src/lib/schemaResolver.ts @@ -260,6 +260,62 @@ export function buildEmbeddedDefaults( return result; } +export function buildNewObjectValue(schema: Schema, objectName: string, variantName?: string): Record { + const result = buildEmbeddedDefaults(schema, objectName, {}, variantName); + return completeStructDefaults(schema, objectName, (result['@type'] as string | undefined) ?? variantName, result); +} + +function completeStructDefaults( + schema: Schema, + objectName: string, + variantName: string | undefined, + target: Record, +): Record { + const resolved = resolveSchema(schema, objectName); + if (!resolved) return target; + + const fields = + resolved.type === 'single' + ? resolved.fields + : ((variantName ? resolved.variants.find((v) => v.name === variantName) : resolved.variants[0])?.fields ?? null); + if (!fields) return target; + + for (const [propName, propDef] of Object.entries(fields.properties)) { + if (propDef.update === 'serverSet') continue; + const t = propDef.type; + + if (t.type === 'boolean') { + if (!(propName in target)) { + target[propName] = false; + } + continue; + } + + if (t.type !== 'object' || t.nullable) continue; + + const current = target[propName]; + if (current !== undefined && !isPlainRecord(current)) continue; + + const overrides = isPlainRecord(current) ? current : {}; + const nestedEntry = schema.schemas[t.objectName]; + const nestedVariant = + nestedEntry?.type === 'multiple' + ? ((overrides['@type'] as string | undefined) ?? nestedEntry.variants[0]?.name) + : undefined; + + const nested = completeStructDefaults(schema, t.objectName, nestedVariant, { ...overrides }); + if (Object.keys(nested).length > 0) { + target[propName] = nested; + } + } + + return target; +} + +function isPlainRecord(value: unknown): value is Record { + return value !== null && typeof value === 'object' && !Array.isArray(value); +} + export function getDisplayProperty(schema: Schema, objectName: string): string { const list = schema.lists[objectName]; if (list?.labelProperty) return list.labelProperty;