Skip to content

Add support for custom ref resolvers #1307

Closed as not planned
Closed as not planned
@ianwremmel

Description

@ianwremmel

Description

I'd really like to be able to support relative file refs. The spec appears to allow them and absolute file paths are impractical for my use case.

Proposal

I was able to use this to deal with the first layer of refs:

const rawWithFullPaths = raw.replaceAll(
  /\$ref: '(\..*)'/g,
  (match, p1) => `$ref: 'file://${path.resolve(path.dirname(apiFileName), p1)}'`
);

const parsed = yml.load(rawWithFullPaths) as OpenAPI3;
const schema = await openapiTS(parsed);

but that breaks down if there are nested refs.

The docs sort of imply that passing a cwd should work

Specify current working directory (cwd) to resolve remote schemas on disk (not needed for remote URL schemas)

const parsed = yml.load(raw) as OpenAPI3;
const schema = await openapiTS(parsed, {
  cwd: path.dirname(apiFileName),
});

but that still throws

✘  Can’t resolve "../../../lib/@clc/health/schema/health-check-response.schema.json" from dynamic JSON. Load this schema from a URL instead.

Would it be practical for openapiTS() to accept a resolver function? By default, it wouldn't necessarily need to do anything more than maintain current behavior (though supporting e.g. node module resolve would be nice 🙂), but it would enable folks to write their own resolvers.

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs welcomePRs are welcome to solve this issue!enhancementNew feature or requestopenapi-tsRelevant to the openapi-typescript librarystale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions