Skip to content

Commit 9937043

Browse files
committed
fix: enhance bundling and dereferencing logic for external $ref
From now on $ref to external files are hoisted to the main spec as a reusable component with type name as a prefix
1 parent f05ba07 commit 9937043

File tree

6 files changed

+242
-145
lines changed

6 files changed

+242
-145
lines changed

lib/__tests__/bundle.test.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,20 @@ describe("bundle", () => {
1616
const refParser = new $RefParser();
1717
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "multiple-refs.json");
1818
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
19+
// console.log(JSON.stringify(schema, null, 2));
1920

20-
// First reference should be fully resolved (no $ref)
21-
expect(schema.paths["/test1/{pathId}"].get.parameters[0].name).toBe("pathId");
22-
expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.type).toBe("string");
23-
expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.format).toBe("uuid");
24-
expect(schema.paths["/test1/{pathId}"].get.parameters[0].$ref).toBeUndefined();
25-
26-
// Second reference should be remapped to point to the first reference
27-
expect(schema.paths["/test2/{pathId}"].get.parameters[0].$ref).toBe(
28-
"#/paths/~1test1~1%7BpathId%7D/get/parameters/0",
29-
);
30-
31-
// Both should effectively resolve to the same data
21+
// Both parameters should now be $ref to the same internal definition
3222
const firstParam = schema.paths["/test1/{pathId}"].get.parameters[0];
3323
const secondParam = schema.paths["/test2/{pathId}"].get.parameters[0];
3424

35-
// The second parameter should resolve to the same data as the first
36-
expect(secondParam.$ref).toBeDefined();
37-
expect(firstParam).toEqual({
25+
// The $ref should match the output structure in file_context_0
26+
expect(firstParam.$ref).toBe("#/components/parameters/path-parameter_pathId");
27+
expect(secondParam.$ref).toBe("#/components/parameters/path-parameter_pathId");
28+
29+
// The referenced parameter should exist and match the expected structure
30+
expect(schema.components).toBeDefined();
31+
expect(schema.components.parameters).toBeDefined();
32+
expect(schema.components.parameters["path-parameter_pathId"]).toEqual({
3833
name: "pathId",
3934
in: "path",
4035
required: true,

lib/__tests__/spec/multiple-refs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"summary": "First endpoint using the same pathId schema",
66
"parameters": [
77
{
8-
"$ref": "path-parameter.json#/pathId"
8+
"$ref": "path-parameter.json#/components/parameters/pathId"
99
}
1010
],
1111
"responses": {
@@ -20,7 +20,7 @@
2020
"summary": "Second endpoint using the same pathId schema",
2121
"parameters": [
2222
{
23-
"$ref": "path-parameter.json#/pathId"
23+
"$ref": "path-parameter.json#/components/parameters/pathId"
2424
}
2525
],
2626
"responses": {
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
2-
"pathId": {
3-
"name": "pathId",
4-
"in": "path",
5-
"required": true,
6-
"schema": {
7-
"type": "string",
8-
"format": "uuid",
9-
"description": "Unique identifier for the path"
2+
"components": {
3+
"parameters": {
4+
"pathId": {
5+
"name": "pathId",
6+
"in": "path",
7+
"required": true,
8+
"schema": {
9+
"type": "string",
10+
"format": "uuid",
11+
"description": "Unique identifier for the path"
12+
}
13+
}
1014
}
1115
}
1216
}

0 commit comments

Comments
 (0)