Skip to content

Commit

Permalink
fix: Incorrect message names in the generated code for repeated field…
Browse files Browse the repository at this point in the history
…s in toJson, fromJson methods
  • Loading branch information
avlyalin committed Jul 10, 2024
1 parent 2078d1a commit 341d90b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2114,15 +2114,15 @@ function generateFromJson(ctx: Context, fullName: string, fullTypeName: string,
} else {
const fallback = noDefaultValue ? nullOrUndefined(options) : "[]";

const readValueSnippet = readSnippet("e");
if (readValueSnippet.toString() === code`e`.toString()) {
const needMap = readSnippet("e").toString() !== code`e`.toString();
if (!needMap) {
chunks.push(
code`${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`,
);
} else {
// Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
chunks.push(code`
${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readValueSnippet}): ${fallback},
${fieldKey}: ${ctx.utils.globalThis}.Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readSnippet("e")}): ${fallback},
`);
}
}
Expand Down Expand Up @@ -2332,8 +2332,8 @@ function generateToJson(
}
} else if (isRepeated(field)) {
// Arrays might need their elements transformed
const transformElement = readSnippet("e");
const maybeMap = transformElement.toCodeString([]) !== "e" ? code`.map(e => ${transformElement})` : "";
const needMap = readSnippet("e").toCodeString([]) !== "e";
const maybeMap = needMap ? code`.map(e => ${readSnippet("e")})` : "";
chunks.push(code`
if (${messageProperty}?.length) {
${jsonProperty} = ${messageProperty}${maybeMap};
Expand Down

0 comments on commit 341d90b

Please sign in to comment.