Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {

/** Convert $ref to TS ref */
export function transformRef(ref: string, root = ""): string {
// TODO: load external file
const isExternalRef = !ref.startsWith("#"); // if # isn’t first character, we can assume this is a remote schema
if (isExternalRef) return "any";

const parts = ref.replace(/^#\//, root).split("/");
return `${parts[0]}["${parts.slice(1).join('"]["')}"]`;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/v2/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,24 @@ describe("transformation", () => {
}`)
);
});

// TODO: allow import later
it("external $ref", () => {
const schema: OpenAPI2 = {
swagger: "2.0",
definitions: {
externalRef: {
$ref: "./external.yaml",
},
},
};
expect(swaggerToTS(schema)).toBe(
format(`
export interface definitions {
externalRef: any;
}`)
);
});
});

describe("propertyMapper", () => {
Expand Down
22 changes: 22 additions & 0 deletions tests/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ describe("types", () => {
}`)
);
});

// TODO: allow import later
it("external $ref", () => {
const schema: OpenAPI3 = {
openapi: "3.0",
components: {
schemas: {
externalRef: {
$ref: "./external.yaml",
},
},
},
};
expect(swaggerToTS(schema)).toBe(
format(`
export interface components {
schemas: {
externalRef: any;
}
}`)
);
});
});

describe("OpenAPI3 features", () => {
Expand Down