From b61f16aeff223405afd6264c7dfc16db16c6c001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Sat, 5 Oct 2024 14:16:07 -0500 Subject: [PATCH] Key-value pair enforce provision for `fromJSON` in `proto2` --- src/main.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index d87e32fcc..9c9dad9cb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2132,6 +2132,7 @@ function generateExtension(ctx: Context, message: DescriptorProto | undefined, e function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, messageDesc: DescriptorProto): Code { const { options, utils, currentFile } = ctx; const chunks: Code[] = []; + const keyValuePair = isKeyValuePair(messageDesc.field); // create the basic function declaration chunks.push(code` @@ -2368,7 +2369,7 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string, ? ${readSnippet(code`${jsonProperty}`)} : ${fallback}, `); - } else if (field.label === FieldDescriptorProto_Label.LABEL_REQUIRED) { + } else if (field.label === FieldDescriptorProto_Label.LABEL_REQUIRED || keyValuePair) { chunks.push(code`${fieldKey}: ${ readSnippet(code`${ctx.utils.assertSet}('${fullName}.${fieldName}', ${jsonProperty})`) },`); @@ -2583,6 +2584,7 @@ function generateToJson( function generateFromPartial(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code { const { options, utils } = ctx; const chunks: Code[] = []; + const keyValuePair = isKeyValuePair(messageDesc.field); // create the create function definition if (ctx.options.useExactTypes) { @@ -2752,7 +2754,7 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri field.label === FieldDescriptorProto_Label.LABEL_OPTIONAL // key-value pairs (the ones with the "Entry" suffix) have an optional label for some reason // so detect a pair and force it to be defined - && !isKeyValuePair(messageDesc.field) + && !keyValuePair ) ) { // An optimized case of the else below that works when `readSnippet` returns the plain input