MCPcopy Index your code
hub / github.com/github/copilot-sdk / schemaTypeToJava

Function schemaTypeToJava

java/scripts/codegen/java.ts:520–672  ·  view source on GitHub ↗
(
    schema: JSONSchema7,
    required: boolean,
    context: string,
    propName: string,
    nestedTypes: Map<string, JavaClassDef>
)

Source from the content-addressed store, hash-verified

518}
519
520function schemaTypeToJava(
521 schema: JSONSchema7,
522 required: boolean,
523 context: string,
524 propName: string,
525 nestedTypes: Map<string, JavaClassDef>
526): JavaTypeResult {
527 const imports = new Set<string>();
528
529 // Resolve $ref first — register standalone types for generation
530 if (schema.$ref) {
531 // Handle cross-schema $ref (e.g. "session-events.schema.json#/definitions/Foo")
532 const crossSchemaMatch = schema.$ref.match(/^([^#]+)#\/definitions\/(.+)$/);
533 if (crossSchemaMatch) {
534 const [, schemaFile, typeName] = crossSchemaMatch;
535 const externalDefs = crossSchemaDefinitions.get(schemaFile);
536 if (externalDefs) {
537 const resolved = externalDefs[typeName];
538 if (resolved) {
539 // Save and swap currentDefinitions so recursive calls resolve against
540 // the external schema's definitions.
541 const savedDefs = currentDefinitions;
542 currentDefinitions = externalDefs;
543 const result = schemaTypeToJava(resolved, required, context, propName, nestedTypes);
544 currentDefinitions = savedDefs;
545 return result;
546 }
547 }
548 // Fallback: extract just the type name and warn
549 console.warn(`[codegen] Unresolved cross-schema $ref: ${schema.$ref}`);
550 return { javaType: typeName, imports };
551 }
552
553 const name = schema.$ref.replace(/^#\/definitions\//, "");
554 const resolved = currentDefinitions[name];
555 if (resolved) {
556 // Enum or object types → register for standalone generation, return ref name
557 if ((resolved.type === "string" && resolved.enum) ||
558 (resolved.type === "object" && resolved.properties)) {
559 pendingStandaloneTypes.set(name, resolved);
560 return { javaType: name, imports };
561 }
562 // Other types (primitives, arrays, maps, anyOf unions) → resolve and recurse
563 return schemaTypeToJava(resolved, required, context, propName, nestedTypes);
564 }
565 // Unresolved $ref — return name as-is
566 console.warn(`[codegen] Unresolved $ref: ${schema.$ref}`);
567 return { javaType: name, imports };
568 }
569
570 if (schema.anyOf) {
571 const hasNull = schema.anyOf.some((s) => typeof s === "object" && (s as JSONSchema7).type === "null");
572 const nonNull = schema.anyOf.filter((s) => typeof s === "object" && (s as JSONSchema7).type !== "null");
573 if (nonNull.length === 1) {
574 const result = schemaTypeToJava(nonNull[0] as JSONSchema7, required && !hasNull, context, propName, nestedTypes);
575 return result;
576 }
577 // Multi-branch anyOf: fall through to Object, matching the C# generator's

Callers 6

renderNestedTypeFunction · 0.85
generateRpcClassFunction · 0.85
generateRpcTypesFunction · 0.85
wrapperResultClassNameFunction · 0.85

Calls 4

toPascalCaseFunction · 0.70
getMethod · 0.45
setMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…