Skip to content

Commit

Permalink
fix: Incorrect message names in the generated code for repeated fields (
Browse files Browse the repository at this point in the history
#1073)

Fixes #1072
  • Loading branch information
avlyalin authored Jul 13, 2024
1 parent 2078d1a commit 8a95d8e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2114,15 +2114,17 @@ 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 +2334,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 8a95d8e

Please sign in to comment.