Skip to content

Commit d7420e3

Browse files
pvanagtmaalPiet van Agtmaal
and
Piet van Agtmaal
authored
Fix externalizing external refs (#1278)
* Fix externalizing external refs * Add changeset --------- Co-authored-by: Piet van Agtmaal <[email protected]>
1 parent f834b26 commit d7420e3

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

.changeset/swift-badgers-notice.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Fix externalizing external refs

packages/openapi-typescript/src/load.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
264264

265265
// local $ref: convert into TS path
266266
if (ref.filename === ".") {
267-
if (subschemaID === ".") {
267+
if (subschemaID === "." || ref.path[0] === "external") {
268268
node.$ref = makeTSIndex(ref.path);
269269
} else {
270270
node.$ref = makeTSIndex(["external", subschemaID, ...ref.path]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: "3.0"
2+
info:
3+
title: test
4+
version: "1.0"
5+
paths:
6+
/:
7+
get:
8+
responses:
9+
200:
10+
description: OK
11+
components:
12+
schemas:
13+
obj:
14+
$ref: './anchor-with-ref-test.yaml#/components/schemas/anchorTest'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: "3.0"
2+
info:
3+
title: test
4+
version: "1.0"
5+
paths:
6+
/:
7+
get:
8+
responses:
9+
200:
10+
description: OK
11+
components:
12+
schemas:
13+
test:
14+
type: object
15+
properties: &testProperties
16+
metadata:
17+
$ref: '#/components/schemas/metadata'
18+
additionalProperties: false
19+
anchorTest:
20+
type: object
21+
additionalProperties: false
22+
properties: *testProperties
23+
metadata:
24+
type: object
25+
additionalProperties: true

packages/openapi-typescript/test/index.test.ts

+65
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,71 @@ export interface external {
393393
export type operations = Record<string, never>;
394394
`);
395395
});
396+
397+
test("anchor $refs", async () => {
398+
const generated = await openapiTS(new URL("./fixtures/anchor-with-ref-test-2.yaml", import.meta.url));
399+
expect(generated).toBe(`${BOILERPLATE}
400+
export interface paths {
401+
"/": {
402+
get: {
403+
responses: {
404+
/** @description OK */
405+
200: never;
406+
};
407+
};
408+
};
409+
}
410+
411+
export type webhooks = Record<string, never>;
412+
413+
export interface components {
414+
schemas: {
415+
obj: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["anchorTest"];
416+
};
417+
responses: never;
418+
parameters: never;
419+
requestBodies: never;
420+
headers: never;
421+
pathItems: never;
422+
}
423+
424+
export interface external {
425+
"anchor-with-ref-test.yaml": {
426+
paths: {
427+
"/": {
428+
get: {
429+
responses: {
430+
/** @description OK */
431+
200: never;
432+
};
433+
};
434+
};
435+
};
436+
webhooks: Record<string, never>;
437+
components: {
438+
schemas: {
439+
test: {
440+
metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
441+
};
442+
anchorTest: {
443+
metadata?: external["anchor-with-ref-test.yaml"]["components"]["schemas"]["metadata"];
444+
};
445+
metadata: {
446+
[key: string]: unknown;
447+
};
448+
};
449+
responses: never;
450+
parameters: never;
451+
requestBodies: never;
452+
headers: never;
453+
pathItems: never;
454+
};
455+
};
456+
}
457+
458+
export type operations = Record<string, never>;
459+
`);
460+
})
396461
});
397462

398463
describe("3.1", () => {

0 commit comments

Comments
 (0)