Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix $ref resolution when used with $id #1286

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/core/src/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ describe('bundle', () => {
expect(parsedMeta).toMatchSnapshot();
});

// This test seems invalid, the bundle result looks wrong
it('should bundle refs using $anchors', async () => {
const testDocument = parseYamlToDocument(
outdent`
Expand Down Expand Up @@ -233,4 +234,62 @@ describe('bundle', () => {

`);
});

it('should bundle refs following $id resolution rules', async () => {
const testDocument = parseYamlToDocument(
outdent`
openapi: 3.0.0
components:
schemas:
Local:
type: string
LocalRef:
$ref: "#/components/schemas/Local"
External:
$id: "http://example.com/external.yaml"
$defs:
Id:
type: string
format: uuid
properties:
foo:
$ref: "#/$defs/Id"
`,
''
);

const config = await makeConfig({});

debugger;
const {
bundle: { parsed },
problems,
} = await bundleDocument({
document: testDocument,
config: config,
externalRefResolver: new BaseResolver(),
});

expect(problems).toHaveLength(0);
expect(parsed).toMatchInlineSnapshot(`
openapi: 3.0.0
components:
schemas:
Local:
type: string
LocalRef:
$ref: '#/components/schemas/Local'
External:
$id: http://example.com/external.yaml
$defs:
Id: &ref_0
type: string
format: uuid
properties:
foo:
$ref: '#/components/schemas/Id'
Id: *ref_0

`);
});
});
49 changes: 49 additions & 0 deletions packages/core/src/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,53 @@ describe('collect refs', () => {

expect(Array.from(resolvedRefs.values()).pop()!.error).toBeInstanceOf(Error);
});

it('should handle $id correctly', async () => {
const cwd = path.join(__dirname, 'fixtures/resolve');
const rootDocument = parseYamlToDocument(
outdent`
openapi: 3.0.0
components:
schemas:
Local:
type: string
LocalRef:
$ref: "#/components/schemas/Local"
External:
$id: "http://example.com/external.yaml"
$defs:
Id:
type: string
format: uuid
properties:
foo:
$ref: "#/$defs/Id"
bar:
$ref: "test.yaml"
`,
path.join(cwd, 'foobar')
);

const resolvedRefs = await resolveDocument({
rootDocument,
externalRefResolver: new BaseResolver(),
rootType: normalizeTypes(Oas3Types).Root,
});

expect(
Array.from(resolvedRefs.keys()).map(
(ref) => (ref.startsWith('http:') && ref) || ref.substring(cwd.length + 1)
)
).toEqual([
'foobar::#/components/schemas/Local',
'http://example.com/external.yaml::#/$defs/Id',
'http://example.com/external.yaml::test.yaml',
]);

expect(Array.from(resolvedRefs.values()).map((info) => info.node)).toEqual([
{ type: 'string' },
{ type: 'string', format: 'uuid' },
undefined, // unresolved ref
]);
});
});
13 changes: 13 additions & 0 deletions packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@ export async function resolveDocument(opts: {
return;
}

if (node.$id && node.$id !== nodeAbsoluteRef) {
const nestedDocument = {
source: new Source(
node.$id,
rootNodeDocument.source.body,
rootNodeDocument.source.mimeType
),
parsed: node,
};
resolveRefsInParallel(node, nestedDocument, '', type);
return;
}

const nodeId = `${type.name}::${nodeAbsoluteRef}`;
if (seedNodes.has(nodeId)) {
return;
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function walkDocument<T>(opts: {
key: string | number
) {
const resolve: ResolveFn = (ref, from = currentLocation.source.absoluteRef) => {
if (!isRef(ref)) return { location, node: ref };
if (!isRef(ref)) return { location: currentLocation, node: ref };
const refId = makeRefId(from, ref.$ref);
const resolvedRef = resolvedRefMap.get(refId);
if (!resolvedRef) {
Expand All @@ -147,6 +147,14 @@ export function walkDocument<T>(opts: {

const rawLocation = location;
let currentLocation = location;

if (node?.$id) {
currentLocation = new Location(
new Source(node.$id, location.source.body, location.source.mimeType),
location.pointer
);
}

const { node: resolvedNode, location: resolvedLocation, error } = resolve(node);
const enteredContexts: Set<VisitorLevelContext> = new Set();

Expand Down