Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 19, 2024
1 parent 7745695 commit 915bf8a
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,34 @@ function getTypeForProp(ctx: SchemaTSContext, prop: JSONSchema, required: string

}


function resolveRefType(ctx: SchemaTSContext, ref: string, schema: JSONSchema): t.TSType {
const path = ref.split('/');
const definitionName = path.pop();

// Check both $defs and definitions in the local schema
function getTypeReferenceFromSchema(schema: JSONSchema, definitionName: string): t.TSType | null {
if (definitionName) {
if (schema.$defs && schema.$defs[definitionName]) {
return t.tsTypeReference(t.identifier(toPascalCase(definitionName)));
} else if (schema.definitions && schema.definitions[definitionName]) {
return t.tsTypeReference(t.identifier(toPascalCase(definitionName)));
}
}
return null; // Return null if no type reference is found
}

// Check both $defs and definitions in the root schema
if (definitionName) {
if (ctx.root.$defs && ctx.root.$defs[definitionName]) {
return t.tsTypeReference(t.identifier(toPascalCase(definitionName)));
} else if (ctx.root.definitions && ctx.root.definitions[definitionName]) {
return t.tsTypeReference(t.identifier(toPascalCase(definitionName)));
}

function resolveRefType(ctx: SchemaTSContext, ref: string, schema: JSONSchema): t.TSType {
const path = ref.split('/');
const definitionName = path.pop();

// Try to resolve the type reference from the local schema
const localTypeReference = getTypeReferenceFromSchema(schema, definitionName);
if (localTypeReference) {
return localTypeReference;
}

// Try to resolve the type reference from the root schema
const rootTypeReference = getTypeReferenceFromSchema(ctx.root, definitionName);
if (rootTypeReference) {
return rootTypeReference;
}

// If no definitions are found, throw an error
// If no definitions are found in either schema, throw an error
throw new Error(`Reference ${ref} not found in definitions or $defs.`);
}

0 comments on commit 915bf8a

Please sign in to comment.