Skip to content

Commit 39916e0

Browse files
committed
Fix remote schema maps
1 parent 6b9054b commit 39916e0

File tree

8 files changed

+459
-10
lines changed

8 files changed

+459
-10
lines changed

.changeset/gold-bugs-chew.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Fix remote schema maps

packages/openapi-typescript/examples/digital-ocean-api.ts

+418-9
Large diffs are not rendered by default.

packages/openapi-typescript/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
174174
subschemaOutput = transformResponseObject(subschema.schema, { path, ctx: { ...ctx, indentLv } });
175175
break;
176176
}
177+
case "SchemaMap": {
178+
subschemaOutput += "{\n";
179+
indentLv++;
180+
for (const [name, schemaObject] of getEntries(subschema.schema!)) {
181+
const c = getSchemaObjectComment(schemaObject, indentLv);
182+
if (c) subschemaOutput += indent(c, indentLv);
183+
subschemaOutput += indent(`${escObjKey(name)}: ${transformSchemaObject(schemaObject, { path: `${path}${name}`, ctx: { ...ctx, indentLv } })};\n`, indentLv);
184+
}
185+
indentLv--;
186+
subschemaOutput += indent("};", indentLv);
187+
break;
188+
}
177189
case "SchemaObject": {
178190
subschemaOutput = transformSchemaObject(subschema.schema, { path, ctx: { ...ctx, indentLv } });
179191
break;

packages/openapi-typescript/src/load.ts

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

218218
// hints help external partial schemas pick up where the root left off (for external complete/valid schemas, skip this)
219219
const isRemoteFullSchema = ref.path[0] === "paths" || ref.path[0] === "components"; // if the initial ref is "paths" or "components" this must be a full schema
220-
const hint = isRemoteFullSchema ? "OpenAPI3" : getHint([...nodePath, ...ref.path], options.hint);
220+
const hintPath = [...nodePath];
221+
if (ref.filename) hintPath.push(ref.filename);
222+
hintPath.push(...ref.path);
223+
const hint = isRemoteFullSchema ? "OpenAPI3" : getHint(hintPath, options.hint);
221224

222225
// if root schema is remote and this is a relative reference, treat as remote
223226
if (schema instanceof URL) {
@@ -400,6 +403,10 @@ function getHintFromResponseObject(path: (string | number)[]): Subschema["hint"]
400403
return "ResponseObject";
401404
}
402405
function getHintFromSchemaObject(path: (string | number)[]): Subschema["hint"] {
406+
// if this is pointing to a remote file and there’s an additional path, it’s a SchemaMap
407+
if (typeof path[0] === "string" && path[0].includes(".") && path[1]) {
408+
return "SchemaMap";
409+
}
403410
switch (path[0]) {
404411
case "allOf":
405412
case "anyOf":

packages/openapi-typescript/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ export type Subschema =
639639
}
640640
| { hint: "RequestBodyObject"; schema: RequestBodyObject }
641641
| { hint: "ResponseObject"; schema: ResponseObject }
642+
| { hint: "SchemaMap"; schema: NonNullable<ComponentsObject["schemas"]> }
642643
| { hint: "SchemaObject"; schema: SchemaObject };
643644

644645
/** Context passed to all submodules */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PartialType:
2+
type: object
3+
properties:
4+
foo:
5+
type: string
6+
required:
7+
- foo

packages/openapi-typescript/test/fixtures/remote-ref-test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ components:
1616
schemas:
1717
RemoteType:
1818
$ref: "./remote-ref-test-2.yaml#/components/schemas/SchemaType"
19+
RemotePartialType:
20+
$ref: '_schema-test-partial.yaml#/PartialType'

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

+6
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export type webhooks = Record<string, never>;
309309
export interface components {
310310
schemas: {
311311
RemoteType: external["remote-ref-test-2.yaml"]["components"]["schemas"]["SchemaType"];
312+
RemotePartialType: external["_schema-test-partial.yaml"]["PartialType"];
312313
};
313314
responses: never;
314315
parameters: never;
@@ -318,6 +319,11 @@ export interface components {
318319
}
319320
320321
export interface external {
322+
"_schema-test-partial.yaml": {
323+
PartialType: {
324+
foo: string;
325+
};
326+
};
321327
"remote-ref-test-2.yaml": {
322328
paths: {
323329
"/": {

0 commit comments

Comments
 (0)