From 285f479be8b401b9824087538a4d5d39173a4b1c Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:44:02 -0600 Subject: [PATCH 1/2] fix(designer): escape double quotes in string values of the key/value editor Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- .../base/utils/__test__/keyvalueitem.spec.ts | 33 +++++++++++++++++++ .../src/lib/editor/base/utils/keyvalueitem.ts | 10 +++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/libs/designer-ui/src/lib/editor/base/utils/__test__/keyvalueitem.spec.ts b/libs/designer-ui/src/lib/editor/base/utils/__test__/keyvalueitem.spec.ts index c6f1b756379..d97771b7b57 100644 --- a/libs/designer-ui/src/lib/editor/base/utils/__test__/keyvalueitem.spec.ts +++ b/libs/designer-ui/src/lib/editor/base/utils/__test__/keyvalueitem.spec.ts @@ -70,6 +70,39 @@ describe('keyvalueitem utilities', () => { expect(result).toMatch(/"DATE"\s*:\s*"/); }); + it('should escape double quotes in a string value so a JSON value yields valid JSON', () => { + const items = [ + { + id: '1', + key: [{ id: 'k1', type: ValueSegmentType.LITERAL, value: 'X-Payload' }], + value: [{ id: 'v1', type: ValueSegmentType.LITERAL, value: '{"a":1,"b":"two"}' }], + }, + ]; + + const segments = convertKeyValueItemToSegments(items, 'string', 'string'); + const result = convertSegmentsToString(segments); + + expect(JSON.parse(result)).toEqual({ 'X-Payload': '{"a":1,"b":"two"}' }); + }); + + it('should leave token segment values untouched when escaping a string value', () => { + const items = [ + { + id: '1', + key: [{ id: 'k1', type: ValueSegmentType.LITERAL, value: 'X-Id' }], + value: [ + { id: 'v1', type: ValueSegmentType.LITERAL, value: 'id-"' }, + { id: 'v2', type: ValueSegmentType.TOKEN, value: 'concat("a")' }, + ], + }, + ]; + + const segments = convertKeyValueItemToSegments(items, 'string', 'string'); + + expect(segments.find((s) => s.id === 'v1')?.value).toBe('id-\\"'); + expect(segments.find((s) => s.id === 'v2')?.value).toBe('concat("a")'); + }); + it('should handle empty items', () => { const items: { id: string; diff --git a/libs/designer-ui/src/lib/editor/base/utils/keyvalueitem.ts b/libs/designer-ui/src/lib/editor/base/utils/keyvalueitem.ts index 9d42bd8644d..50e32e89bcd 100644 --- a/libs/designer-ui/src/lib/editor/base/utils/keyvalueitem.ts +++ b/libs/designer-ui/src/lib/editor/base/utils/keyvalueitem.ts @@ -1,6 +1,7 @@ import constants from '../../../constants'; import { isEmpty } from '../../../dictionary/expandeddictionary'; import type { ValueSegment } from '../../models/parameter'; +import { ValueSegmentType } from '../../models/parameter'; import { containsTokenSegments, createLiteralValueSegment, insertQutationForStringType } from './helper'; import { convertSegmentsToString } from './parsesegments'; import { escapeBackslash } from '@microsoft/logic-apps-shared'; @@ -41,7 +42,7 @@ export const convertKeyValueItemToSegments = (items: KeyValueItem[], keyType?: s convertedValueType !== constants.SWAGGER.TYPE.STRING ? segment.value : typeof segment.value === 'string' - ? escapeBackslash(segment.value) + ? escapeStringValue(segment.value, segment.type) : JSON.stringify(segment.value), }; }); @@ -60,6 +61,13 @@ export const convertKeyValueItemToSegments = (items: KeyValueItem[], keyType?: s return parsedItems; }; +// Literal text that ends up inside a JSON string must also have its double quotes escaped, +// otherwise a value such as {"a":1} produces invalid JSON in the collapsed editor. +const escapeStringValue = (value: string, type: string): string => { + const escaped = escapeBackslash(value); + return type === ValueSegmentType.LITERAL ? escaped.replace(/"/g, '\\"') : escaped; +}; + // we want to default to string type when the value is type any export const convertValueType = (value: ValueSegment[], type?: string): string | undefined => { if (type && type !== constants.SWAGGER.TYPE.ANY) { From 03a2e8f61e51fec875b875f991f109d36cdc9390 Mon Sep 17 00:00:00 2001 From: Ym <173550318+MaxonT@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:27:46 -0600 Subject: [PATCH 2/2] fix(designer-ui): decode string values when expanding a collapsed any-typed dictionary Signed-off-by: Ym <173550318+MaxonT@users.noreply.github.com> --- .../serializecollapseddictionary.spec.ts | 50 +++++++++++++++++++ .../util/serializecollapseddictionary.ts | 6 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 libs/designer-ui/src/lib/dictionary/util/__test__/serializecollapseddictionary.spec.ts diff --git a/libs/designer-ui/src/lib/dictionary/util/__test__/serializecollapseddictionary.spec.ts b/libs/designer-ui/src/lib/dictionary/util/__test__/serializecollapseddictionary.spec.ts new file mode 100644 index 00000000000..04f803be2d9 --- /dev/null +++ b/libs/designer-ui/src/lib/dictionary/util/__test__/serializecollapseddictionary.spec.ts @@ -0,0 +1,50 @@ +import { $createParagraphNode, $createTextNode, $getRoot, createEditor } from 'lexical'; +import { describe, expect, it, vi } from 'vitest'; +import { serializeDictionary } from '../serializecollapseddictionary'; +import { convertSegmentsToString } from '../../../editor/base/utils/parsesegments'; +import { convertKeyValueItemToSegments } from '../../../editor/base/utils/keyvalueitem'; +import { ValueSegmentType } from '../../../editor/models/parameter'; + +const readItems = (text: string, valueType?: string) => { + const editor = createEditor({ + onError: (e) => { + throw e; + }, + }); + editor.update( + () => { + const paragraph = $createParagraphNode(); + paragraph.append($createTextNode(text)); + $getRoot().clear().append(paragraph); + }, + { discrete: true } + ); + const setItems = vi.fn(); + const setIsValid = vi.fn(); + serializeDictionary(editor, setItems, setIsValid, 'string', valueType); + return { setItems, setIsValid }; +}; + +describe('serializeDictionary', () => { + it('keeps a quoted string value unescaped when the value type is any, so collapse -> expand -> collapse is stable', () => { + const literal = (value: string) => ({ id: 'v', type: ValueSegmentType.LITERAL, value }); + const items = [{ id: '1', key: [literal('X-Say')], value: [literal('say "hello"')] }]; + + const collapsed = convertSegmentsToString(convertKeyValueItemToSegments(items, 'string', 'any')); + const { setItems, setIsValid } = readItems(collapsed, 'any'); + + expect(setIsValid).toHaveBeenCalledWith(true); + const expanded = setItems.mock.calls[0][0]; + expect(convertSegmentsToString(expanded[0].value)).toBe('say "hello"'); + + const recollapsed = convertSegmentsToString(convertKeyValueItemToSegments(expanded, 'string', 'any')); + expect(recollapsed).toBe(collapsed); + }); + + it('still serializes non-string values with JSON.stringify when the value type is any', () => { + const { setItems } = readItems('{"a": 5, "b": {"c": 1}}', 'any'); + const expanded = setItems.mock.calls[0][0]; + expect(convertSegmentsToString(expanded[0].value)).toBe('5'); + expect(convertSegmentsToString(expanded[1].value)).toBe('{"c":1}'); + }); +}); diff --git a/libs/designer-ui/src/lib/dictionary/util/serializecollapseddictionary.ts b/libs/designer-ui/src/lib/dictionary/util/serializecollapseddictionary.ts index 7d490161360..a313a82e4d4 100644 --- a/libs/designer-ui/src/lib/dictionary/util/serializecollapseddictionary.ts +++ b/libs/designer-ui/src/lib/dictionary/util/serializecollapseddictionary.ts @@ -24,7 +24,11 @@ export const serializeDictionary = ( for (const [key, value] of Object.entries(jsonEditor)) { const newKey = keyType === constants.SWAGGER.TYPE.STRING ? (key as string) : removeQuotes(JSON.stringify(key)); - const newValue = valueType === constants.SWAGGER.TYPE.STRING ? (value as string) : removeQuotes(JSON.stringify(value)); + // A string value is already decoded by JSON.parse; stringifying it again would leave literal backslashes before escaped characters. + const newValue = + valueType === constants.SWAGGER.TYPE.STRING || typeof value === 'string' + ? (value as string) + : removeQuotes(JSON.stringify(value)); returnItems.push({ id: guid(), key: convertStringToSegments(newKey, nodeMap, { tokensEnabled: true }),