From 55b1019d0483c5d00f9ecec1a4da50bd673f4699 Mon Sep 17 00:00:00 2001 From: Philip Linde Date: Thu, 1 Oct 2026 13:52:50 +0200 Subject: [PATCH] feat: optionally disable RPC string parsing (#419) The new setup option enablePlainRpcStrings, when enabled, will prevent parsing strings and strings in string collections as JSON when converting typed RPC data. --- README.md | 1 + src/converters/fromRpcTypedData.ts | 7 +++++ src/setup.ts | 5 ++++ test/converters/fromRpcTypedData.test.ts | 33 ++++++++++++++++++++++++ test/setup.test.ts | 21 ++++++++++++++- test/types/index.test.ts | 10 +++++++ types/setup.d.ts | 5 ++++ 7 files changed, 81 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 84f4da0..a4d52f2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ npm install @azure/functions - _**Runtime**_: Defines underlying behavior of Azure Functions and is shared across all languages. - The programming model version is strictly tied to the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package, and is versioned independently of the [runtime](https://learn.microsoft.com/azure/azure-functions/functions-versions?pivots=programming-language-javascript). Both the runtime and the programming model use "4" as their latest major version, but that is purely a coincidence. - You can't mix the v3 and v4 programming models in the same function app. As soon as you register one v4 function in your app, any v3 functions registered in _function.json_ files are ignored. +- By default, trigger metadata strings will be converted to JSON if they can successfully be interpreted as JSON. This behavior can be disabled by setting the `enablePlainRpcStrings` option to `true` in the setup. ## Usage diff --git a/src/converters/fromRpcTypedData.ts b/src/converters/fromRpcTypedData.ts index fa1a919..622cc25 100644 --- a/src/converters/fromRpcTypedData.ts +++ b/src/converters/fromRpcTypedData.ts @@ -4,12 +4,16 @@ import { RpcTypedData } from '@azure/functions-core'; import { ResourceFactoryResolver } from '@azure/functions-extensions-base'; import { HttpRequest } from '../http/HttpRequest'; +import { enablePlainRpcStrings } from '../setup'; import { isDefined } from '../utils/nonNull'; export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown { if (!data) { return undefined; } else if (isDefined(data.string)) { + if (enablePlainRpcStrings) { + return data.string; + } return tryJsonParse(data.string); } else if (isDefined(data.json)) { return JSON.parse(data.json); @@ -26,6 +30,9 @@ export function fromRpcTypedData(data: RpcTypedData | null | undefined): unknown } else if (data.collectionBytes && isDefined(data.collectionBytes.bytes)) { return data.collectionBytes.bytes.map((d) => Buffer.from(d)); } else if (data.collectionString && isDefined(data.collectionString.string)) { + if (enablePlainRpcStrings) { + return data.collectionString.string; + } return data.collectionString.string.map(tryJsonParse); } else if (data.collectionDouble && isDefined(data.collectionDouble.double)) { return data.collectionDouble.double; diff --git a/src/setup.ts b/src/setup.ts index d6b54c3..ede38ea 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -13,6 +13,7 @@ export function lockSetup(): void { } export let enableHttpStream = false; +export let enablePlainRpcStrings = false; export const capabilities: Record = {}; export function setup(opts: SetupOptions): void { @@ -33,6 +34,10 @@ export function setup(opts: SetupOptions): void { enableHttpStream = opts.enableHttpStream; } + if (isDefined(opts.enablePlainRpcStrings)) { + enablePlainRpcStrings = opts.enablePlainRpcStrings; + } + if (opts.capabilities) { for (let [key, val] of Object.entries(opts.capabilities)) { if (isDefined(val)) { diff --git a/test/converters/fromRpcTypedData.test.ts b/test/converters/fromRpcTypedData.test.ts index 17d06a9..08d62f8 100644 --- a/test/converters/fromRpcTypedData.test.ts +++ b/test/converters/fromRpcTypedData.test.ts @@ -10,6 +10,7 @@ import Long = require('long'); import { ResourceFactoryResolver } from '@azure/functions-extensions-base'; import sinon = require('sinon'); import { RpcTypedData } from '@azure/functions-core'; +import { setup } from '../../src/setup'; describe('fromRpcTypedData', () => { it('null', () => { @@ -26,6 +27,20 @@ describe('fromRpcTypedData', () => { expect(fromRpcTypedData({ string: 'false' })).to.equal(false); expect(fromRpcTypedData({ string: '1' })).to.equal(1); expect(fromRpcTypedData({ string: '0' })).to.equal(0); + + setup({ enablePlainRpcStrings: true }); + try { + expect(fromRpcTypedData({ string: 'test' })).to.equal('test'); + expect(fromRpcTypedData({ string: '{ "a": "b" }' })).to.equal('{ "a": "b" }'); + expect(fromRpcTypedData({ string: '{ "a": "b" ' })).to.equal('{ "a": "b" '); + expect(fromRpcTypedData({ string: '[1,2]' })).to.equal('[1,2]'); + expect(fromRpcTypedData({ string: 'true' })).to.equal('true'); + expect(fromRpcTypedData({ string: 'false' })).to.equal('false'); + expect(fromRpcTypedData({ string: '1' })).to.equal('1'); + expect(fromRpcTypedData({ string: '0' })).to.equal('0'); + } finally { + setup({ enablePlainRpcStrings: false }); + } }); it('json', () => { @@ -99,6 +114,24 @@ describe('fromRpcTypedData', () => { '{"a": "b"', 'test2', ]); + + setup({ enablePlainRpcStrings: true }); + try { + expect(fromRpcTypedData({ collectionString: { string: ['test1', 'test2'] } })).to.deep.equal([ + 'test1', + 'test2', + ]); + expect(fromRpcTypedData({ collectionString: { string: ['{"a": "b"}', 'test2'] } })).to.deep.equal([ + '{"a": "b"}', + 'test2', + ]); + expect(fromRpcTypedData({ collectionString: { string: ['{"a": "b"', 'test2'] } })).to.deep.equal([ + '{"a": "b"', + 'test2', + ]); + } finally { + setup({ enablePlainRpcStrings: false }); + } }); it('collectionDouble', () => { diff --git a/test/setup.test.ts b/test/setup.test.ts index a650341..35c9345 100644 --- a/test/setup.test.ts +++ b/test/setup.test.ts @@ -3,7 +3,7 @@ import 'mocha'; import { expect } from 'chai'; -import { capabilities, enableHttpStream, setup } from '../src/setup'; +import { capabilities, enableHttpStream, enablePlainRpcStrings, setup } from '../src/setup'; describe('setup', () => { it('enableHttpStream', () => { @@ -25,6 +25,25 @@ describe('setup', () => { expect(enableHttpStream).to.equal(false); }); + it('enablePlainRpcStrings', () => { + // default + expect(enablePlainRpcStrings).to.equal(false); + + // set to true + setup({ enablePlainRpcStrings: true }); + expect(enablePlainRpcStrings).to.equal(true); + + // don't change if not explicitly set + setup({}); + expect(enablePlainRpcStrings).to.equal(true); + setup({ capabilities: {} }); + expect(enablePlainRpcStrings).to.equal(true); + + // set to false + setup({ enablePlainRpcStrings: false }); + expect(enablePlainRpcStrings).to.equal(false); + }); + it('capabilities', () => { // default expect(capabilities).to.deep.equal({}); diff --git a/test/types/index.test.ts b/test/types/index.test.ts index 94ea8f7..3406536 100644 --- a/test/types/index.test.ts +++ b/test/types/index.test.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import type { CosmosDBv4ChangeFeedMode } from '../../types'; +import type { SetupOptions } from '../../types'; // This file will be compiled by multiple versions of TypeScript as decribed in ./test/TypesTests.ts to verify there are no errors interface TodoItem { @@ -92,6 +93,13 @@ type IsInvalidHandlerAssignableToAvad = InvalidAvadHandler extends ExpectedAvadH const validHandlerAssignableToAvad: IsValidHandlerAssignableToAvad = true; const invalidHandlerAssignableToAvad: IsInvalidHandlerAssignableToAvad = false; +const setupOptions: SetupOptions = { + enableHttpStream: false, + capabilities: { a: 'b', c: false }, + enablePlainRpcStrings: false, +}; +const emptySetupOptions: SetupOptions = {}; + void latestVersionOptions; void allVersionsAndDeletesOptions; void deprecatedFullFidelityOptions; @@ -101,3 +109,5 @@ void invalidServiceBusTopicTriggerOptions; void registerServiceBusTopic; void validHandlerAssignableToAvad; void invalidHandlerAssignableToAvad; +void setupOptions; +void emptySetupOptions; diff --git a/types/setup.d.ts b/types/setup.d.ts index 92af251..b16b941 100644 --- a/types/setup.d.ts +++ b/types/setup.d.ts @@ -13,4 +13,9 @@ export interface SetupOptions { * This will be merged with existing capabilities specified by the Node.js worker and library. */ capabilities?: Record; + + /** + * Don't convert RPC strings to JSON just because they have the shape of valid JSON. + */ + enablePlainRpcStrings?: boolean; }