Skip to content

Commit 60ff605

Browse files
committed
Fix codegen for internal runtime schemas
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: f0193925-3c4a-4142-b5c0-4ea48168a24b
1 parent 811adc0 commit 60ff605

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

scripts/codegen/csharp.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
isSchemaExperimental,
4141
isSchemaInternal,
4242
isOpaqueJson,
43+
isOpaqueInProcess,
4344
isObjectSchema,
4445
isVoidSchema,
4546
getNullableInner,
@@ -355,10 +356,10 @@ function failUnmappable(context: string, schema: JSONSchema7): never {
355356
);
356357
}
357358

358-
function omitUntypedInternalProperties(value: unknown): void {
359+
function omitUnrepresentableInternalProperties(value: unknown): void {
359360
if (!value || typeof value !== "object") return;
360361
if (Array.isArray(value)) {
361-
value.forEach(omitUntypedInternalProperties);
362+
value.forEach(omitUnrepresentableInternalProperties);
362363
return;
363364
}
364365

@@ -377,16 +378,16 @@ function omitUntypedInternalProperties(value: unknown): void {
377378
schema.enum !== undefined ||
378379
schema.const !== undefined ||
379380
isOpaqueJson(schema);
380-
if (isSchemaInternal(schema) && !hasType) {
381+
if (isSchemaInternal(schema) && (!hasType || isOpaqueInProcess(schema))) {
381382
delete (properties as Record<string, unknown>)[name];
382383
} else {
383-
omitUntypedInternalProperties(property);
384+
omitUnrepresentableInternalProperties(property);
384385
}
385386
}
386387
}
387388

388389
for (const [name, child] of Object.entries(node)) {
389-
if (name !== "properties") omitUntypedInternalProperties(child);
390+
if (name !== "properties") omitUnrepresentableInternalProperties(child);
390391
}
391392
}
392393

@@ -2604,7 +2605,7 @@ function generateRpcCode(
26042605
externalValueTypes: Set<string> = new Set()
26052606
): string {
26062607
schema = cloneSchemaForCodegen(schema);
2607-
omitUntypedInternalProperties(schema);
2608+
omitUnrepresentableInternalProperties(schema);
26082609
emittedRpcClassSchemas.clear();
26092610
emittedRpcEnumResultTypes.clear();
26102611
experimentalRpcTypes.clear();

scripts/codegen/typescript.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,39 @@ export function normalizeSchemaForTypeScript(
383383
root.definitions = definitions;
384384
delete root.$defs;
385385

386-
const rewrite = (value: unknown): unknown => {
386+
const internalDefinitionNames = new Set(
387+
Object.entries(definitions)
388+
.filter(([, definition]) => typeof definition === "object" && definition !== null && isSchemaInternal(definition as JSONSchema7))
389+
.map(([name]) => name)
390+
);
391+
const isInternalUnionVariant = (value: unknown): boolean => {
392+
if (!value || typeof value !== "object") return false;
393+
const variant = value as JSONSchema7;
394+
if (isSchemaInternal(variant)) return true;
395+
const match = variant.$ref?.match(/^#\/(?:definitions|\$defs)\/([^/]+)$/);
396+
return match ? internalDefinitionNames.has(match[1]) : false;
397+
};
398+
399+
const rewrite = (value: unknown, withinInternalDefinition = false): unknown => {
387400
if (Array.isArray(value)) {
388-
return value.map(rewrite);
401+
return value.map((item) => rewrite(item, withinInternalDefinition));
389402
}
390403
if (!value || typeof value !== "object") {
391404
return value;
392405
}
393406

407+
const source = value as Record<string, unknown>;
408+
const isInternal = withinInternalDefinition || isSchemaInternal(source as JSONSchema7);
394409
const rewritten = Object.fromEntries(
395-
Object.entries(value as Record<string, unknown>).map(([key, child]) => [key, rewrite(child)])
410+
Object.entries(source).map(([key, child]) => {
411+
const publicChild =
412+
!isInternal &&
413+
(key === "anyOf" || key === "oneOf") &&
414+
Array.isArray(child)
415+
? child.filter((variant) => !isInternalUnionVariant(variant))
416+
: child;
417+
return [key, rewrite(publicChild, isInternal)];
418+
})
396419
) as Record<string, unknown>;
397420

398421
if (isBareSchemaNode(rewritten as JSONSchema7)) {

0 commit comments

Comments
 (0)