Skip to content

Commit 0f7f409

Browse files
authored
Merge pull request #13 from hey-api/fix--update-external-ref-handling
fix: enhance bundling and dereferencing logic for external $ref
2 parents 5a801bf + cbf25e7 commit 0f7f409

File tree

7 files changed

+462
-133
lines changed

7 files changed

+462
-133
lines changed

lib/__tests__/bundle.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,18 @@ describe("bundle", () => {
1717
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "multiple-refs.json");
1818
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
1919

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
20+
// Both parameters should now be $ref to the same internal definition
3221
const firstParam = schema.paths["/test1/{pathId}"].get.parameters[0];
3322
const secondParam = schema.paths["/test2/{pathId}"].get.parameters[0];
3423

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

lib/__tests__/pointer.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe("pointer", () => {
77
const refParser = new $RefParser();
88
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "openapi-paths-ref.json");
99
const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
10+
console.log(JSON.stringify(schema, null, 2));
1011

1112
// The GET endpoint should have its schema defined inline
1213
const getSchema = schema.paths["/foo"].get.responses["200"].content["application/json"].schema;
@@ -16,11 +17,11 @@ describe("pointer", () => {
1617

1718
// The POST endpoint should have its schema inlined (copied) instead of a $ref
1819
const postSchema = schema.paths["/foo"].post.responses["200"].content["application/json"].schema;
19-
expect(postSchema.$ref).toBeUndefined();
20-
expect(postSchema.type).toBe("object");
21-
expect(postSchema.properties.bar.type).toBe("string");
20+
expect(postSchema.$ref).toBe("#/paths/~1foo/get/responses/200/content/application~1json/schema");
21+
expect(postSchema.type).toBeUndefined();
22+
expect(postSchema.properties?.bar?.type).toBeUndefined();
2223

2324
// Both schemas should be identical objects
24-
expect(postSchema).toEqual(getSchema);
25+
expect(postSchema).not.toBe(getSchema);
2526
});
2627
});

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)