Skip to content

Commit 0749aff

Browse files
authored
closes astahmer#85. Adds support for extended types with allOf. (astahmer#86)
nice job 🙌
1 parent 1bae57a commit 0749aff

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

.changeset/plain-dolls-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typed-openapi": patch
3+
---
4+
5+
closes #85. Adds support for extended types with allOf.

packages/typed-openapi/src/openapi-schema-to-ts.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export const openApiSchemaToTs = ({ schema, meta: _inheritedMeta, ctx }: Openapi
5252
}
5353

5454
if (schema.allOf) {
55-
if (schema.allOf.length === 1) {
56-
return openApiSchemaToTs({ schema: schema.allOf[0]!, ctx, meta });
57-
}
58-
5955
const types = schema.allOf.map((prop) => openApiSchemaToTs({ schema: prop, ctx, meta }));
56+
const {allOf, externalDocs, example, examples, description, title, ...rest} = schema
57+
if (Object.keys(rest).length > 0) {
58+
types.push(openApiSchemaToTs({schema: rest, ctx, meta}))
59+
}
6060
return t.intersection(types);
6161
}
6262

packages/typed-openapi/tests/openapi-schema-to-ts.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import type { SchemasObject } from "openapi3-ts/oas31";
32
import { describe, expect, test } from "vitest";
43

@@ -450,6 +449,36 @@ describe("getSchemaBox with context", () => {
450449
);
451450
});
452451

452+
test("with ref and allOf", () => {
453+
const schemas = {
454+
Extends: {
455+
allOf: [{$ref: "#/components/schemas/Base"}],
456+
type: "object",
457+
properties: {
458+
str: { type: "string" },
459+
nb: { type: "number" },
460+
},
461+
required: ["str"],
462+
},
463+
Base: {
464+
type: "object",
465+
properties: {
466+
baseProp: { type: "string" },
467+
}
468+
},
469+
} satisfies SchemasObject;
470+
471+
const ctx = makeCtx(schemas);
472+
expect(openApiSchemaToTs({ schema: schemas["Extends"]!, ctx })).toMatchInlineSnapshot(
473+
`
474+
{
475+
"type": "intersection",
476+
"value": "(Base & { str: string, nb?: number | undefined })",
477+
}
478+
`,
479+
);
480+
});
481+
453482
test("with multiple nested refs", () => {
454483
const schemas = {
455484
Root2: {

0 commit comments

Comments
 (0)