Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect message names in the generated code for repeated fields #1073

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading