Skip to content

Commit bbd18ba

Browse files
authored
Merge pull request #15 from hey-api/fix/self-reference-circular
fix: do not overwrite in circular self references
2 parents e043602 + e58322a commit bbd18ba

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

lib/__tests__/bundle.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@ describe("bundle", () => {
99
const refParser = new $RefParser();
1010
const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "circular-ref-with-description.json");
1111
const schema = await refParser.bundle({ pathOrUrlOrSchema });
12-
expect(schema).not.toBeUndefined();
12+
expect(schema).toEqual({
13+
schemas: {
14+
Bar: {
15+
$ref: '#/schemas/Foo',
16+
description: 'ok',
17+
},
18+
Foo: {
19+
$ref: '#/schemas/Bar',
20+
},
21+
},
22+
});
1323
});
1424

1525
it("bundles multiple references to the same file correctly", async () => {

lib/__tests__/pointer.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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));
1110

1211
// The GET endpoint should have its schema defined inline
1312
const getSchema = schema.paths["/foo"].get.responses["200"].content["application/json"].schema;

lib/bundle.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,11 @@ function remap(parser: $RefParser, inventory: InventoryEntry[]) {
579579
continue;
580580
}
581581

582-
// Keep internal refs internal
582+
// Keep internal refs internal. However, if the $ref extends the resolved value
583+
// (i.e. it has additional properties in addition to "$ref"), then we must
584+
// preserve the original $ref rather than rewriting it to the resolved hash.
583585
if (!entry.external) {
584-
if (entry.$ref && typeof entry.$ref === "object") {
586+
if (!entry.extended && entry.$ref && typeof entry.$ref === "object") {
585587
entry.$ref.$ref = entry.hash;
586588
}
587589
continue;

0 commit comments

Comments
 (0)