Skip to content

Commit 54d1bfe

Browse files
authored
Merge pull request #965 from nikopavlica/fix-dictionary-composition
Fixes a bug that prevented using complex dictionaries within compositions
2 parents ec2c712 + c88f506 commit 54d1bfe

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

src/openApi/v3/parser/getModelComposition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export const getModelComposition = (
3131
const hasProperties = model.properties.length;
3232
const hasEnums = model.enums.length;
3333
const isObject = model.type === 'any';
34+
const isDictionary = model.export === 'dictionary';
3435
const isEmpty = isObject && !hasProperties && !hasEnums;
35-
return !isEmpty;
36+
return !isEmpty || isDictionary;
3637
})
3738
.forEach(model => {
3839
composition.imports.push(...model.imports);

test/__snapshots__/index.spec.ts.snap

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,10 @@ export type { CompositionWithAnyOf } from './models/CompositionWithAnyOf';
35073507
export type { CompositionWithAnyOfAndNullable } from './models/CompositionWithAnyOfAndNullable';
35083508
export type { CompositionWithAnyOfAnonymous } from './models/CompositionWithAnyOfAnonymous';
35093509
export type { CompositionWithOneOf } from './models/CompositionWithOneOf';
3510+
export type { CompositionWithOneOfAndComplexArrayDictionary } from './models/CompositionWithOneOfAndComplexArrayDictionary';
35103511
export type { CompositionWithOneOfAndNullable } from './models/CompositionWithOneOfAndNullable';
3512+
export type { CompositionWithOneOfAndSimpleArrayDictionary } from './models/CompositionWithOneOfAndSimpleArrayDictionary';
3513+
export type { CompositionWithOneOfAndSimpleDictionary } from './models/CompositionWithOneOfAndSimpleDictionary';
35113514
export type { CompositionWithOneOfAnonymous } from './models/CompositionWithOneOfAnonymous';
35123515
export type { CompositionWithOneOfDiscriminator } from './models/CompositionWithOneOfDiscriminator';
35133516
export type { DictionaryWithArray } from './models/DictionaryWithArray';
@@ -3568,7 +3571,10 @@ export { $CompositionWithAnyOf } from './schemas/$CompositionWithAnyOf';
35683571
export { $CompositionWithAnyOfAndNullable } from './schemas/$CompositionWithAnyOfAndNullable';
35693572
export { $CompositionWithAnyOfAnonymous } from './schemas/$CompositionWithAnyOfAnonymous';
35703573
export { $CompositionWithOneOf } from './schemas/$CompositionWithOneOf';
3574+
export { $CompositionWithOneOfAndComplexArrayDictionary } from './schemas/$CompositionWithOneOfAndComplexArrayDictionary';
35713575
export { $CompositionWithOneOfAndNullable } from './schemas/$CompositionWithOneOfAndNullable';
3576+
export { $CompositionWithOneOfAndSimpleArrayDictionary } from './schemas/$CompositionWithOneOfAndSimpleArrayDictionary';
3577+
export { $CompositionWithOneOfAndSimpleDictionary } from './schemas/$CompositionWithOneOfAndSimpleDictionary';
35723578
export { $CompositionWithOneOfAnonymous } from './schemas/$CompositionWithOneOfAnonymous';
35733579
export { $CompositionWithOneOfDiscriminator } from './schemas/$CompositionWithOneOfDiscriminator';
35743580
export { $DictionaryWithArray } from './schemas/$DictionaryWithArray';
@@ -3902,6 +3908,20 @@ export type CompositionWithOneOf = {
39023908
"
39033909
`;
39043910

3911+
exports[`v3 should generate: ./test/generated/v3/models/CompositionWithOneOfAndComplexArrayDictionary.ts 1`] = `
3912+
"/* istanbul ignore file */
3913+
/* tslint:disable */
3914+
/* eslint-disable */
3915+
3916+
/**
3917+
* This is a model that contains a dictionary of complex arrays (composited) within composition
3918+
*/
3919+
export type CompositionWithOneOfAndComplexArrayDictionary = {
3920+
propA?: (boolean | Record<string, Array<(number | string)>>);
3921+
};
3922+
"
3923+
`;
3924+
39053925
exports[`v3 should generate: ./test/generated/v3/models/CompositionWithOneOfAndNullable.ts 1`] = `
39063926
"/* istanbul ignore file */
39073927
/* tslint:disable */
@@ -3922,6 +3942,34 @@ export type CompositionWithOneOfAndNullable = {
39223942
"
39233943
`;
39243944

3945+
exports[`v3 should generate: ./test/generated/v3/models/CompositionWithOneOfAndSimpleArrayDictionary.ts 1`] = `
3946+
"/* istanbul ignore file */
3947+
/* tslint:disable */
3948+
/* eslint-disable */
3949+
3950+
/**
3951+
* This is a model that contains a dictionary of simple arrays within composition
3952+
*/
3953+
export type CompositionWithOneOfAndSimpleArrayDictionary = {
3954+
propA?: (boolean | Record<string, Array<boolean>>);
3955+
};
3956+
"
3957+
`;
3958+
3959+
exports[`v3 should generate: ./test/generated/v3/models/CompositionWithOneOfAndSimpleDictionary.ts 1`] = `
3960+
"/* istanbul ignore file */
3961+
/* tslint:disable */
3962+
/* eslint-disable */
3963+
3964+
/**
3965+
* This is a model that contains a simple dictionary within composition
3966+
*/
3967+
export type CompositionWithOneOfAndSimpleDictionary = {
3968+
propA?: (boolean | Record<string, number>);
3969+
};
3970+
"
3971+
`;
3972+
39253973
exports[`v3 should generate: ./test/generated/v3/models/CompositionWithOneOfAnonymous.ts 1`] = `
39263974
"/* istanbul ignore file */
39273975
/* tslint:disable */
@@ -4928,6 +4976,36 @@ export const $CompositionWithOneOf = {
49284976
} as const;"
49294977
`;
49304978

4979+
exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfAndComplexArrayDictionary.ts 1`] = `
4980+
"/* istanbul ignore file */
4981+
/* tslint:disable */
4982+
/* eslint-disable */
4983+
export const $CompositionWithOneOfAndComplexArrayDictionary = {
4984+
description: \`This is a model that contains a dictionary of complex arrays (composited) within composition\`,
4985+
properties: {
4986+
propA: {
4987+
type: 'one-of',
4988+
contains: [{
4989+
type: 'boolean',
4990+
}, {
4991+
type: 'dictionary',
4992+
contains: {
4993+
type: 'array',
4994+
contains: {
4995+
type: 'one-of',
4996+
contains: [{
4997+
type: 'number',
4998+
}, {
4999+
type: 'string',
5000+
}],
5001+
},
5002+
},
5003+
}],
5004+
},
5005+
},
5006+
} as const;"
5007+
`;
5008+
49315009
exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfAndNullable.ts 1`] = `
49325010
"/* istanbul ignore file */
49335011
/* tslint:disable */
@@ -4956,6 +5034,53 @@ export const $CompositionWithOneOfAndNullable = {
49565034
} as const;"
49575035
`;
49585036

5037+
exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfAndSimpleArrayDictionary.ts 1`] = `
5038+
"/* istanbul ignore file */
5039+
/* tslint:disable */
5040+
/* eslint-disable */
5041+
export const $CompositionWithOneOfAndSimpleArrayDictionary = {
5042+
description: \`This is a model that contains a dictionary of simple arrays within composition\`,
5043+
properties: {
5044+
propA: {
5045+
type: 'one-of',
5046+
contains: [{
5047+
type: 'boolean',
5048+
}, {
5049+
type: 'dictionary',
5050+
contains: {
5051+
type: 'array',
5052+
contains: {
5053+
type: 'boolean',
5054+
},
5055+
},
5056+
}],
5057+
},
5058+
},
5059+
} as const;"
5060+
`;
5061+
5062+
exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfAndSimpleDictionary.ts 1`] = `
5063+
"/* istanbul ignore file */
5064+
/* tslint:disable */
5065+
/* eslint-disable */
5066+
export const $CompositionWithOneOfAndSimpleDictionary = {
5067+
description: \`This is a model that contains a simple dictionary within composition\`,
5068+
properties: {
5069+
propA: {
5070+
type: 'one-of',
5071+
contains: [{
5072+
type: 'boolean',
5073+
}, {
5074+
type: 'dictionary',
5075+
contains: {
5076+
type: 'number',
5077+
},
5078+
}],
5079+
},
5080+
},
5081+
} as const;"
5082+
`;
5083+
49595084
exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionWithOneOfAnonymous.ts 1`] = `
49605085
"/* istanbul ignore file */
49615086
/* tslint:disable */

test/spec/v3.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,76 @@
20642064
}
20652065
}
20662066
},
2067+
"CompositionWithOneOfAndSimpleDictionary": {
2068+
"description": "This is a model that contains a simple dictionary within composition",
2069+
"type": "object",
2070+
"properties": {
2071+
"propA": {
2072+
"oneOf": [
2073+
{
2074+
"type": "boolean"
2075+
},
2076+
{
2077+
"type": "object",
2078+
"additionalProperties": {
2079+
"type": "number"
2080+
}
2081+
}
2082+
]
2083+
}
2084+
}
2085+
},
2086+
"CompositionWithOneOfAndSimpleArrayDictionary": {
2087+
"description": "This is a model that contains a dictionary of simple arrays within composition",
2088+
"type": "object",
2089+
"properties": {
2090+
"propA": {
2091+
"oneOf": [
2092+
{
2093+
"type": "boolean"
2094+
},
2095+
{
2096+
"type": "object",
2097+
"additionalProperties": {
2098+
"type": "array",
2099+
"items": {
2100+
"type": "boolean"
2101+
}
2102+
}
2103+
}
2104+
]
2105+
}
2106+
}
2107+
},
2108+
"CompositionWithOneOfAndComplexArrayDictionary": {
2109+
"description": "This is a model that contains a dictionary of complex arrays (composited) within composition",
2110+
"type": "object",
2111+
"properties": {
2112+
"propA": {
2113+
"oneOf": [
2114+
{
2115+
"type": "boolean"
2116+
},
2117+
{
2118+
"type": "object",
2119+
"additionalProperties": {
2120+
"type": "array",
2121+
"items": {
2122+
"oneOf": [
2123+
{
2124+
"type": "number"
2125+
},
2126+
{
2127+
"type": "string"
2128+
}
2129+
]
2130+
}
2131+
}
2132+
}
2133+
]
2134+
}
2135+
}
2136+
},
20672137
"CompositionWithAllOfAndNullable": {
20682138
"description": "This is a model with one property with a 'all of' relationship",
20692139
"type": "object",

0 commit comments

Comments
 (0)