Skip to content

Commit 9a39e52

Browse files
fix: #157 allOf w/ properties (#158)
* Reconcile a schema with properties & allOf * Additional vscode setting to avoid trimming white space in test snapshots. * Remove not needed import * Make sure required taken into account.
1 parent 4a14e5b commit 9a39e52

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"typescript.tsdk": "node_modules\\typescript\\lib"
2+
"typescript.tsdk": "node_modules\\typescript\\lib",
3+
"editor.trimAutoWhitespace": false,
4+
"files.trimTrailingWhitespace": false
35
}

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,74 @@ describe("schemaToTypeAliasDeclaration", () => {
621621
});
622622

623623
describe("allOf", () => {
624+
it("should combine properties and allOf", () => {
625+
const schema: SchemaObject = {
626+
allOf: [
627+
{ type: "object", properties: { foo: { type: "string" } } },
628+
{ type: "object", properties: { bar: { type: "number" } } },
629+
],
630+
properties: {
631+
foobar: {type: "string"}
632+
},
633+
required: ['foo', 'foobar']
634+
};
635+
636+
expect(printSchema(schema)).toMatchInlineSnapshot(`
637+
"export type Test = {
638+
foo: string;
639+
bar?: number;
640+
foobar: string;
641+
};"
642+
`);
643+
});
644+
645+
it("should combine additionalProperties and allOf", () => {
646+
const schema: SchemaObject = {
647+
allOf: [
648+
{ type: "object", properties: { foo: { type: "string" } } },
649+
{ type: "object", properties: { bar: { type: "number" } } },
650+
],
651+
additionalProperties: {
652+
type: "string"
653+
}
654+
};
655+
656+
expect(printSchema(schema)).toMatchInlineSnapshot(`
657+
"export type Test = {
658+
foo?: string;
659+
bar?: number;
660+
} & {
661+
[key: string]: string;
662+
};"
663+
`);
664+
});
665+
666+
it("should combine properties & additionalProperties & allOf", () => {
667+
const schema: SchemaObject = {
668+
allOf: [
669+
{ type: "object", properties: { foo: { type: "string" } } },
670+
{ type: "object", properties: { bar: { type: "number" } } },
671+
],
672+
additionalProperties: {
673+
type: "string"
674+
},
675+
properties: {
676+
foobar: {type: "string"}
677+
},
678+
required: ['bar', 'foobar']
679+
};
680+
681+
expect(printSchema(schema)).toMatchInlineSnapshot(`
682+
"export type Test = {
683+
foo?: string;
684+
bar: number;
685+
foobar: string;
686+
} & {
687+
[key: string]: string;
688+
};"
689+
`);
690+
});
691+
624692
it("should combine inline types", () => {
625693
const schema: SchemaObject = {
626694
allOf: [

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,24 @@ export const getType = (
128128
}
129129

130130
if (schema.allOf) {
131-
return getAllOf(schema.allOf, context);
131+
const adHocSchemas: Array<SchemaObject> = [];
132+
if (schema.properties) {
133+
adHocSchemas.push({
134+
type: 'object',
135+
properties: schema.properties,
136+
required: schema.required
137+
});
138+
}
139+
if (schema.additionalProperties) {
140+
adHocSchemas.push({
141+
type: 'object',
142+
additionalProperties: schema.additionalProperties
143+
});
144+
}
145+
return getAllOf([
146+
...schema.allOf,
147+
...adHocSchemas
148+
], context);
132149
}
133150

134151
if (schema.enum) {

0 commit comments

Comments
 (0)