Skip to content

Commit f54e3ac

Browse files
stephentoubCopilot
andauthored
Fix Go and Rust code generators (#1596)
Update the Go generator to avoid discriminator method collisions only when a generated variant field would otherwise conflict. Update the Rust generator to keep RPC methods using internal schemas crate-private and allow intentionally unused generated internal items. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9c4d637 commit f54e3ac

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

rust/src/generated/api_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Auto-generated from api.schema.json — do not edit manually.
22

33
#![allow(clippy::large_enum_variant)]
4+
#![allow(dead_code)]
45

56
use std::collections::HashMap;
67

rust/src/generated/rpc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
#![allow(missing_docs)]
99
#![allow(clippy::too_many_arguments)]
10+
#![allow(dead_code)]
1011

1112
use super::api_types::{rpc_methods, *};
1213
use super::session_events::SessionMode;

scripts/codegen/go.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,27 @@ function goVariantMatchFunctionLines(
17311731
return lines;
17321732
}
17331733

1734+
function goDiscriminatorMethodName(
1735+
typeName: string,
1736+
discriminatorProp: string,
1737+
discGoName: string,
1738+
variants: GoDiscriminatedUnionVariant[],
1739+
ctx: GoCodegenCtx
1740+
): string {
1741+
const collidesWithVariantField = variants.some((variant) => {
1742+
const resolved = resolveSchema(variant.schema, ctx.definitions) ?? variant.schema;
1743+
const objectSchema = resolveObjectSchema(resolved, ctx.definitions) ?? resolved;
1744+
return Object.keys(objectSchema.properties ?? {}).some((propName) => {
1745+
if (propName === discriminatorProp) {
1746+
return variant.discriminatorValues.length > 1 && discGoName === "Discriminator";
1747+
}
1748+
return toGoFieldName(propName) === discGoName;
1749+
});
1750+
});
1751+
1752+
return collidesWithVariantField ? `${toGoUnexportedIdentifier(typeName)}${discGoName}` : discGoName;
1753+
}
1754+
17341755
/**
17351756
* Emit a Go interface for a discriminated union (anyOf with const discriminator).
17361757
*/
@@ -1748,7 +1769,7 @@ function emitGoFlatDiscriminatedUnion(
17481769
const mapping = discriminator.mapping;
17491770
const unionVariants = [...discriminator.variants].sort((left, right) => compareGoTypeNames(left.typeName, right.typeName));
17501771
const discGoName = toGoFieldName(discriminatorProp);
1751-
const discriminatorMethodName = discGoName;
1772+
const discriminatorMethodName = goDiscriminatorMethodName(typeName, discriminatorProp, discGoName, unionVariants, ctx);
17521773
let discEnumName: string | undefined;
17531774
let discGoType = "bool";
17541775
if (discriminator.valueKind === "string") {

scripts/codegen/rust.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,7 @@ function generateApiTypesCode(
15451545
out.push("//! Auto-generated from api.schema.json — do not edit manually.");
15461546
out.push("");
15471547
out.push("#![allow(clippy::large_enum_variant)]");
1548+
out.push("#![allow(dead_code)]");
15481549
out.push("");
15491550
out.push("use std::collections::HashMap;");
15501551
out.push("");
@@ -1776,6 +1777,17 @@ function getResultTypeName(
17761777
return `${toPascalCase(method.rpcMethod)}Result`;
17771778
}
17781779

1780+
function methodUsesInternalSchema(
1781+
schema: JSONSchema7 | null | undefined,
1782+
defCollections: DefinitionCollections,
1783+
): boolean {
1784+
if (!schema) return false;
1785+
1786+
const nonNullable = getNullableInner(schema) ?? schema;
1787+
const resolved = resolveSchema(nonNullable, defCollections) ?? nonNullable;
1788+
return isSchemaInternal(resolved);
1789+
}
1790+
17791791
function pushNamespaceMethodBody(
17801792
out: string[],
17811793
constName: string,
@@ -1884,7 +1896,12 @@ function emitNamespaceMethod(
18841896
};
18851897

18861898
const paramArg = hasParams ? `, params: ${paramsTypeName}` : "";
1887-
const fnVis = method.visibility === "internal" ? "pub(crate)" : "pub";
1899+
const fnVis =
1900+
method.visibility === "internal" ||
1901+
methodUsesInternalSchema(method.params, defCollections) ||
1902+
methodUsesInternalSchema(method.result, defCollections)
1903+
? "pub(crate)"
1904+
: "pub";
18881905

18891906
if (hasParams && paramsInfo.optional) {
18901907
out.push(...buildDocs(false));
@@ -1949,6 +1966,7 @@ function generateRpcCode(apiSchema: ApiSchema): string {
19491966
out.push("");
19501967
out.push("#![allow(missing_docs)]");
19511968
out.push("#![allow(clippy::too_many_arguments)]");
1969+
out.push("#![allow(dead_code)]");
19521970
out.push("");
19531971
out.push("use super::api_types::{rpc_methods, *};");
19541972
const externalTypeRefs = new Map<string, Set<string>>();

0 commit comments

Comments
 (0)