Skip to content

Commit 1ffdca6

Browse files
authored
Fix external $refs (#354)
1 parent fcde6da commit 1ffdca6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {
9696

9797
/** Convert $ref to TS ref */
9898
export function transformRef(ref: string, root = ""): string {
99+
// TODO: load external file
100+
const isExternalRef = !ref.startsWith("#"); // if # isn’t first character, we can assume this is a remote schema
101+
if (isExternalRef) return "any";
102+
99103
const parts = ref.replace(/^#\//, root).split("/");
100104
return `${parts[0]}["${parts.slice(1).join('"]["')}"]`;
101105
}

tests/v2/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ describe("transformation", () => {
316316
}`)
317317
);
318318
});
319+
320+
// TODO: allow import later
321+
it("external $ref", () => {
322+
const schema: OpenAPI2 = {
323+
swagger: "2.0",
324+
definitions: {
325+
externalRef: {
326+
$ref: "./external.yaml",
327+
},
328+
},
329+
};
330+
expect(swaggerToTS(schema)).toBe(
331+
format(`
332+
export interface definitions {
333+
externalRef: any;
334+
}`)
335+
);
336+
});
319337
});
320338

321339
describe("propertyMapper", () => {

tests/v3/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ describe("types", () => {
266266
}`)
267267
);
268268
});
269+
270+
// TODO: allow import later
271+
it("external $ref", () => {
272+
const schema: OpenAPI3 = {
273+
openapi: "3.0",
274+
components: {
275+
schemas: {
276+
externalRef: {
277+
$ref: "./external.yaml",
278+
},
279+
},
280+
},
281+
};
282+
expect(swaggerToTS(schema)).toBe(
283+
format(`
284+
export interface components {
285+
schemas: {
286+
externalRef: any;
287+
}
288+
}`)
289+
);
290+
});
269291
});
270292

271293
describe("OpenAPI3 features", () => {

0 commit comments

Comments
 (0)