Skip to content

Fix externalizing external refs #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2023
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
5 changes: 5 additions & 0 deletions .changeset/swift-badgers-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Fix externalizing external refs
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export default async function load(schema: URL | Subschema | Readable, options:

// local $ref: convert into TS path
if (ref.filename === ".") {
if (subschemaID === ".") {
if (subschemaID === "." || ref.path[0] === "external") {
node.$ref = makeTSIndex(ref.path);
} else {
node.$ref = makeTSIndex(["external", subschemaID, ...ref.path]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: "3.0"
info:
title: test
version: "1.0"
paths:
/:
get:
responses:
200:
description: OK
components:
schemas:
obj:
$ref: './anchor-with-ref-test.yaml#/components/schemas/anchorTest'
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: "3.0"
info:
title: test
version: "1.0"
paths:
/:
get:
responses:
200:
description: OK
components:
schemas:
test:
type: object
properties: &testProperties
metadata:
$ref: '#/components/schemas/metadata'
additionalProperties: false
anchorTest:
type: object
additionalProperties: false
properties: *testProperties
metadata:
type: object
additionalProperties: true
65 changes: 65 additions & 0 deletions packages/openapi-typescript/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,71 @@ export interface external {
export type operations = Record<string, never>;
`);
});

test("anchor $refs", async () => {
const generated = await openapiTS(new URL("./fixtures/anchor-with-ref-test-2.yaml", import.meta.url));
expect(generated).toBe(`${BOILERPLATE}
export interface paths {
"/": {
get: {
responses: {
/** @description OK */
200: never;
};
};
};
}

export type webhooks = Record<string, never>;

export interface components {
schemas: {
obj: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["anchorTest"];
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}

export interface external {
"anchor-with-ref-test.yaml": {
paths: {
"/": {
get: {
responses: {
/** @description OK */
200: never;
};
};
};
};
webhooks: Record<string, never>;
components: {
schemas: {
test: {
metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
};
anchorTest: {
metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
};
metadata: {
[key: string]: unknown;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
};
};
}

export type operations = Record<string, never>;
`);
})
});

describe("3.1", () => {
Expand Down