Skip to content

Commit 6659d43

Browse files
Fix codegen and build failures after @github/copilot update
Handle cross-schema $ref values (e.g. session-events.schema.json#/definitions/TypeName) in the Java code generator. Previously the codegen emitted the raw $ref string as a Java type name, causing compilation errors. Now cross-schema refs are resolved by loading the referenced schema's definitions and recursing into schemaTypeToJava with the resolved schema. Automated fix applied by codegen-agentic-fix workflow. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7a4ea8d commit 6659d43

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

scripts/codegen/java.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ interface JavaTypeResult {
102102
let currentDefinitions: Record<string, JSONSchema7> = {};
103103
const pendingStandaloneTypes = new Map<string, JSONSchema7>();
104104

105+
// Cross-schema definitions: keyed by schema filename (e.g. "session-events.schema.json"),
106+
// value is the definitions map from that schema. Populated by generateRpcTypes so that
107+
// cross-schema $ref values like "session-events.schema.json#/definitions/Foo" can be resolved.
108+
const crossSchemaDefinitions = new Map<string, Record<string, JSONSchema7>>();
109+
105110
/**
106111
* Resolve a $ref in a JSON Schema against the current definitions.
107112
* Returns the resolved schema, or the original if no $ref is present.
@@ -131,6 +136,28 @@ function schemaTypeToJava(
131136

132137
// Resolve $ref first — register standalone types for generation
133138
if (schema.$ref) {
139+
// Handle cross-schema $ref (e.g. "session-events.schema.json#/definitions/Foo")
140+
const crossSchemaMatch = schema.$ref.match(/^([^#]+)#\/definitions\/(.+)$/);
141+
if (crossSchemaMatch) {
142+
const [, schemaFile, typeName] = crossSchemaMatch;
143+
const externalDefs = crossSchemaDefinitions.get(schemaFile);
144+
if (externalDefs) {
145+
const resolved = externalDefs[typeName];
146+
if (resolved) {
147+
// Save and swap currentDefinitions so recursive calls resolve against
148+
// the external schema's definitions.
149+
const savedDefs = currentDefinitions;
150+
currentDefinitions = externalDefs;
151+
const result = schemaTypeToJava(resolved, required, context, propName, nestedTypes);
152+
currentDefinitions = savedDefs;
153+
return result;
154+
}
155+
}
156+
// Fallback: extract just the type name and warn
157+
console.warn(`[codegen] Unresolved cross-schema $ref: ${schema.$ref}`);
158+
return { javaType: typeName, imports };
159+
}
160+
134161
const name = schema.$ref.replace(/^#\/definitions\//, "");
135162
const resolved = currentDefinitions[name];
136163
if (resolved) {
@@ -883,6 +910,19 @@ async function generateRpcTypes(schemaPath: string): Promise<void> {
883910
// Set module-level definitions for $ref resolution
884911
currentDefinitions = (schema.definitions ?? {}) as Record<string, JSONSchema7>;
885912
pendingStandaloneTypes.clear();
913+
crossSchemaDefinitions.clear();
914+
915+
// Load cross-schema definitions (session-events) so that cross-schema $ref values
916+
// like "session-events.schema.json#/definitions/Foo" can be resolved.
917+
try {
918+
const sessionEventsSchemaPath = await getSessionEventsSchemaPath();
919+
const sessionEventsContent = await fs.readFile(sessionEventsSchemaPath, "utf-8");
920+
const sessionEventsSchema = JSON.parse(sessionEventsContent) as JSONSchema7;
921+
crossSchemaDefinitions.set("session-events.schema.json",
922+
(sessionEventsSchema.definitions ?? {}) as Record<string, JSONSchema7>);
923+
} catch (e) {
924+
console.warn(`[codegen] Could not load session-events schema for cross-ref resolution: ${e}`);
925+
}
886926

887927
const packageName = "com.github.copilot.sdk.generated.rpc";
888928
const packageDir = `src/generated/java/com/github/copilot/sdk/generated/rpc`;

src/generated/java/com/github/copilot/sdk/generated/rpc/PendingPermissionRequest.java

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generated/java/com/github/copilot/sdk/generated/rpc/SessionEventLogReadResult.java

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)