From b226ee5430e4a73e18ab9ae53b09fb211b5a0e63 Mon Sep 17 00:00:00 2001 From: TXyy2023 <145091426+TXyy2023@users.noreply.github.com> Date: Tue, 20 Oct 2026 11:53:02 +0800 Subject: [PATCH 1/2] fix(manager-web): decode stored dictionaries before editing (#3450) --- .../src/components/ModelEditDialog.vue | 11 +++ .../tests/modelEditDialog.test.mjs | 98 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 main/manager-web/tests/modelEditDialog.test.mjs diff --git a/main/manager-web/src/components/ModelEditDialog.vue b/main/manager-web/src/components/ModelEditDialog.vue index adf193759..c5c64b253 100644 --- a/main/manager-web/src/components/ModelEditDialog.vue +++ b/main/manager-web/src/components/ModelEditDialog.vue @@ -330,6 +330,17 @@ export default { if (!configJson.hasOwnProperty(field.prop)) { configJson[field.prop] = ""; } else if (field.type === "json-textarea") { + // 兼容已保存为 JSON 字符串的字典,避免再次序列化后无法编辑。 + if (typeof configJson[field.prop] === "string") { + try { + const parsed = JSON.parse(configJson[field.prop]); + if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) { + configJson[field.prop] = parsed; + } + } catch { + // 保留非法值的显示,由现有校验提示用户修正。 + } + } this.$set( this.fieldJsonMap, field.prop, diff --git a/main/manager-web/tests/modelEditDialog.test.mjs b/main/manager-web/tests/modelEditDialog.test.mjs new file mode 100644 index 000000000..a78dbfa56 --- /dev/null +++ b/main/manager-web/tests/modelEditDialog.test.mjs @@ -0,0 +1,98 @@ +import Vue from 'vue'; +import { parseComponent } from 'vue-template-compiler'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import source from '../src/components/ModelEditDialog.vue?raw'; + +// The unit-test runner has no Vue SFC plugin. Load the real component's script +// with its API and child-dialog dependencies stubbed; keep Vue's reactivity. +const script = parseComponent(source).script.content + .replace(/^import .*;$/gm, '') + .replace('export default', 'return'); +const component = new Function('CustomDialog', 'Api', script)({}, {}); + +let vm; +let saved; + +function openModel(value) { + vm.processModelData({ + id: 'test-model', + modelName: 'Test model', + configJson: { type: 'test-provider', prosody: value }, + }); +} + +beforeEach(() => { + vi.useFakeTimers(); + vm = new Vue({ + ...component, + propsData: { modelType: 'TTS', modelData: { id: 'test-model' } }, + }); + vm.$message = { error: vi.fn() }; + vm.dynamicCallInfoFields = [{ prop: 'prosody', type: 'json-textarea' }]; + saved = vi.fn(); + vm.$on('save', saved); +}); + +afterEach(() => { + vm.$destroy(); + vi.clearAllTimers(); + vi.useRealTimers(); +}); + +describe('stored dictionary fields', () => { + it('opens a JSON string as an editable dictionary instead of a quoted string', () => { + openModel('{"speed":1,"volume":10}'); + + expect(JSON.parse(vm.fieldJsonMap.prosody)).toEqual({ speed: 1, volume: 10 }); + expect(vm.form.configJson.prosody).toEqual({ speed: 1, volume: 10 }); + }); + + it('preserves a stored JSON string when saving without changes', () => { + openModel('{"speed":1,"volume":10}'); + vm.handleSave(); + + expect(vm.$message.error).not.toHaveBeenCalled(); + expect(saved.mock.calls[0][0].formData.configJson.prosody).toEqual({ + speed: 1, + volume: 10, + }); + }); + + it('keeps edits after saving and reopening the returned object', () => { + openModel('{"speed":1,"volume":10}'); + vm.fieldJsonMap.prosody = '{"speed":2,"volume":5}'; + vm.handleSave(); + const config = saved.mock.calls[0][0].formData.configJson; + + vm.resetForm(); + openModel(JSON.parse(JSON.stringify(config.prosody))); + vm.handleSave(); + + expect(JSON.parse(vm.fieldJsonMap.prosody)).toEqual({ speed: 2, volume: 5 }); + expect(saved.mock.calls[1][0].formData.configJson.prosody).toEqual({ + speed: 2, + volume: 5, + }); + expect(vm.$message.error).not.toHaveBeenCalled(); + }); + + it('preserves nested values and masked secrets in existing dictionary objects', () => { + const value = { speed: 0, enabled: false, nested: { api_key: 'ab****yz' }, tags: ['a'] }; + openModel(value); + vm.handleSave(); + + expect(saved.mock.calls[0][0].formData.configJson.prosody).toEqual(value); + expect(vm.$message.error).not.toHaveBeenCalled(); + }); + + it.each(['not json', 'null', '[]', '1', 'true', '"text"'])( + 'does not reinterpret %s as a dictionary', + (value) => { + openModel(value); + + expect(JSON.parse(vm.fieldJsonMap.prosody)).toBe(value); + expect(vm.validateJson(vm.fieldJsonMap.prosody)).toBeNull(); + expect(vm.$message.error).toHaveBeenCalled(); + }, + ); +}); From 6f958ee4a69cfa92563562beb8eb43026dfb2d99 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Tue, 20 Oct 2026 15:34:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E6=96=B0=E5=A2=9E=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2dict=E5=AD=97=E6=AE=B5=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/AddModelDialog.vue | 25 +++++- .../manager-web/tests/modelAddDialog.test.mjs | 79 +++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 main/manager-web/tests/modelAddDialog.test.mjs diff --git a/main/manager-web/src/components/AddModelDialog.vue b/main/manager-web/src/components/AddModelDialog.vue index c9e91a669..c15ec9b34 100644 --- a/main/manager-web/src/components/AddModelDialog.vue +++ b/main/manager-web/src/components/AddModelDialog.vue @@ -77,7 +77,10 @@
- + + @@ -161,7 +164,7 @@ export default { fields: JSON.parse(item.fields || '[]').map(f => ({ label: f.label, prop: f.key, - type: f.type === 'password' ? 'password' : 'text', + type: f.type === 'dict' ? 'json-textarea' : f.type === 'password' ? 'password' : 'text', placeholder: `请输入${f.key}` })) })) @@ -204,6 +207,24 @@ export default { return; } + // dict 字段必须解析为 JSON 对象再提交,否则会以字符串入库 + for (const field of this.providerFields) { + if (field.type !== 'json-textarea') continue; + const raw = this.formData.configJson[field.prop]; + if (!raw || typeof raw === 'object') continue; + try { + const parsed = JSON.parse(raw); + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { + throw new Error('not object'); + } + this.formData.configJson[field.prop] = parsed; + } catch (e) { + this.$message.error('必须输入字典格式(如 {"key":"value"})'); + this.saving = false; + return; + } + } + const submitData = { id: this.formData.id || '', modelName: this.formData.modelName || '', diff --git a/main/manager-web/tests/modelAddDialog.test.mjs b/main/manager-web/tests/modelAddDialog.test.mjs new file mode 100644 index 000000000..2529da4e3 --- /dev/null +++ b/main/manager-web/tests/modelAddDialog.test.mjs @@ -0,0 +1,79 @@ +import Vue from 'vue'; +import { parseComponent } from 'vue-template-compiler'; +import { describe, expect, it, vi } from 'vitest'; +import source from '../src/components/AddModelDialog.vue?raw'; + +// The unit-test runner has no Vue SFC plugin. Load the real component's script +// with its API and dialog dependencies stubbed; keep Vue's reactivity. +const script = parseComponent(source).script.content + .replace(/^import .*;$/gm, '') + .replace('export default', 'return'); +const component = new Function('CustomDialog', 'Api', script)({}, {}); + +function makeVm() { + const vm = new Vue({ + ...component, + propsData: { visible: false, modelType: 'TTS' }, + }); + vm.$message = { error: vi.fn() }; + vm.$t = (key) => key; + vm.providerFields = [ + { label: '音量', prop: 'volume', type: 'json-textarea', placeholder: '请输入volume' }, + { label: '密钥', prop: 'api_key', type: 'password', placeholder: '请输入api_key' }, + ]; + vm.formData.supplier = 'test-provider'; + return vm; +} + +function submitted(vm) { + let captured; + vm.$on('confirm', (data) => { + captured = data; + }); + vm.confirm(); + return captured; +} + +describe('add dialog dict fields', () => { + it('parses valid JSON dict input into an object before submit', () => { + const vm = makeVm(); + vm.formData.configJson = { volume: '{"speed":1,"volume":10}', api_key: 'sk-xxx' }; + + const data = submitted(vm); + + expect(data.configJson.volume).toEqual({ speed: 1, volume: 10 }); + expect(data.configJson.api_key).toBe('sk-xxx'); + expect(vm.$message.error).not.toHaveBeenCalled(); + }); + + it('blocks invalid JSON input and does not emit confirm', () => { + const vm = makeVm(); + vm.formData.configJson = { volume: '{bad json', api_key: 'sk-xxx' }; + + expect(submitted(vm)).toBeUndefined(); + expect(vm.$message.error).toHaveBeenCalled(); + }); + + it.each(['[1,2]', 'null', '"text"', '1'])( + 'blocks non-object JSON %s', + (value) => { + const vm = makeVm(); + vm.formData.configJson = { volume: value, api_key: 'sk-xxx' }; + + expect(submitted(vm)).toBeUndefined(); + expect(vm.$message.error).toHaveBeenCalled(); + }, + ); + + it('leaves empty strings and existing object values untouched', () => { + const vm = makeVm(); + const obj = { speed: 2 }; + vm.formData.configJson = { volume: obj, api_key: '' }; + + const data = submitted(vm); + + expect(data.configJson.volume).toBe(obj); + expect(data.configJson.api_key).toBe(''); + expect(vm.$message.error).not.toHaveBeenCalled(); + }); +});