Skip to content

Commit f35bb12

Browse files
committed
fix(any-of): handle more cases
1 parent 41743e8 commit f35bb12

File tree

4 files changed

+134
-69
lines changed

4 files changed

+134
-69
lines changed

src/openApi/v3/parser/getModel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const getModel = (
1313
openApi: OpenApi,
1414
definition: OpenApiSchema,
1515
isDefinition: boolean = false,
16-
name: string = ''
16+
name: string = '',
17+
parentDefinition: OpenApiSchema | null = null
1718
): Model => {
1819
const model: Model = {
1920
name,
@@ -82,7 +83,7 @@ export const getModel = (
8283
model.imports.push(...arrayItems.imports);
8384
model.default = getModelDefault(definition, model);
8485
return model;
85-
} else if (definition.items.anyOf) {
86+
} else if (definition.items.anyOf && parentDefinition) {
8687
return getModel(openApi, definition.items);
8788
} else {
8889
const arrayItems = getModel(openApi, definition.items);

src/openApi/v3/parser/getModelComposition.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,7 @@ export const getModelComposition = (
2626
const properties: Model[] = [];
2727

2828
definitions
29-
.map(definition => getModel(openApi, definition))
30-
.filter(model => {
31-
const hasProperties = model.properties.length;
32-
const hasEnums = model.enums.length;
33-
const hasLink = typeof model.link !== 'undefined' && model.link !== null;
34-
console.log(model.name, model)
35-
const isObject = model.type === 'any';
36-
const isDictionary = model.export === 'dictionary';
37-
const isEmpty = isObject && !hasProperties && !hasEnums && !hasLink;
38-
return !isEmpty || isDictionary;
39-
})
29+
.map(def => getModel(openApi, def, undefined, undefined, definition))
4030
.forEach(model => {
4131
composition.imports.push(...model.imports);
4232
composition.enums.push(...model.enums);

test/__snapshots__/index.spec.ts.snap

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,7 @@ export type { OpenAPIConfig } from './core/OpenAPI';
36923692
export type { _default } from './models/_default';
36933693
export type { AnyOfAnyAndNull } from './models/AnyOfAnyAndNull';
36943694
export type { AnyOfArrays } from './models/AnyOfArrays';
3695+
export type { ArrayWithAnyOfProperties } from './models/ArrayWithAnyOfProperties';
36953696
export type { ArrayWithArray } from './models/ArrayWithArray';
36963697
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
36973698
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
@@ -3769,6 +3770,7 @@ export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
37693770
export { $_default } from './schemas/$_default';
37703771
export { $AnyOfAnyAndNull } from './schemas/$AnyOfAnyAndNull';
37713772
export { $AnyOfArrays } from './schemas/$AnyOfArrays';
3773+
export { $ArrayWithAnyOfProperties } from './schemas/$ArrayWithAnyOfProperties';
37723774
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
37733775
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
37743776
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -3885,7 +3887,7 @@ exports[`v3 should generate: test/generated/v3/models/AnyOfAnyAndNull.ts 1`] = `
38853887
/* tslint:disable */
38863888
/* eslint-disable */
38873889
export type AnyOfAnyAndNull = {
3888-
data?: any | null;
3890+
data?: (any | null);
38893891
};
38903892

38913893
"
@@ -3910,6 +3912,22 @@ export type AnyOfArrays = {
39103912
"
39113913
`;
39123914

3915+
exports[`v3 should generate: test/generated/v3/models/ArrayWithAnyOfProperties.ts 1`] = `
3916+
"/* generated using openapi-typescript-codegen -- do no edit */
3917+
/* istanbul ignore file */
3918+
/* tslint:disable */
3919+
/* eslint-disable */
3920+
/**
3921+
* This is a simple array with any of properties
3922+
*/
3923+
export type ArrayWithAnyOfProperties = Array<({
3924+
foo?: string;
3925+
} | {
3926+
bar?: string;
3927+
})>;
3928+
"
3929+
`;
3930+
39133931
exports[`v3 should generate: test/generated/v3/models/ArrayWithArray.ts 1`] = `
39143932
"/* generated using openapi-typescript-codegen -- do no edit */
39153933
/* istanbul ignore file */
@@ -5118,6 +5136,9 @@ export const $AnyOfAnyAndNull = {
51185136
data: {
51195137
type: 'any-of',
51205138
contains: [{
5139+
properties: {
5140+
},
5141+
}, {
51215142
type: 'null',
51225143
}],
51235144
},
@@ -5135,26 +5156,56 @@ export const $AnyOfArrays = {
51355156
description: \`This is a simple array with any of properties\`,
51365157
properties: {
51375158
results: {
5138-
type: 'any-of',
5139-
contains: [{
5140-
properties: {
5141-
foo: {
5142-
type: 'string',
5159+
type: 'array',
5160+
contains: {
5161+
type: 'any-of',
5162+
contains: [{
5163+
properties: {
5164+
foo: {
5165+
type: 'string',
5166+
},
51435167
},
5144-
},
5145-
}, {
5146-
properties: {
5147-
bar: {
5148-
type: 'string',
5168+
}, {
5169+
properties: {
5170+
bar: {
5171+
type: 'string',
5172+
},
51495173
},
5150-
},
5151-
}],
5174+
}],
5175+
},
51525176
},
51535177
},
51545178
} as const;
51555179
"
51565180
`;
51575181

5182+
exports[`v3 should generate: test/generated/v3/schemas/$ArrayWithAnyOfProperties.ts 1`] = `
5183+
"/* generated using openapi-typescript-codegen -- do no edit */
5184+
/* istanbul ignore file */
5185+
/* tslint:disable */
5186+
/* eslint-disable */
5187+
export const $ArrayWithAnyOfProperties = {
5188+
type: 'array',
5189+
contains: {
5190+
type: 'any-of',
5191+
contains: [{
5192+
properties: {
5193+
foo: {
5194+
type: 'string',
5195+
},
5196+
},
5197+
}, {
5198+
properties: {
5199+
bar: {
5200+
type: 'string',
5201+
},
5202+
},
5203+
}],
5204+
},
5205+
} as const;
5206+
"
5207+
`;
5208+
51585209
exports[`v3 should generate: test/generated/v3/schemas/$ArrayWithArray.ts 1`] = `
51595210
"/* generated using openapi-typescript-codegen -- do no edit */
51605211
/* istanbul ignore file */

test/spec/v3.json

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,72 @@
16831683
}
16841684
}
16851685
},
1686+
"ArrayWithAnyOfProperties": {
1687+
"description": "This is a simple array with any of properties",
1688+
"type": "array",
1689+
"items": {
1690+
"anyOf": [
1691+
{
1692+
"type": "object",
1693+
"properties": {
1694+
"foo": {
1695+
"type": "string"
1696+
}
1697+
}
1698+
},
1699+
{
1700+
"type": "object",
1701+
"properties": {
1702+
"bar": {
1703+
"type": "string"
1704+
}
1705+
}
1706+
}
1707+
]
1708+
}
1709+
},
1710+
"AnyOfAnyAndNull": {
1711+
"type": "object",
1712+
"properties": {
1713+
"data": {
1714+
"anyOf": [
1715+
{},
1716+
{
1717+
"type": "null"
1718+
}
1719+
]
1720+
}
1721+
}
1722+
},
1723+
"AnyOfArrays": {
1724+
"description": "This is a simple array with any of properties",
1725+
"type": "object",
1726+
"properties": {
1727+
"results": {
1728+
"items": {
1729+
"anyOf": [
1730+
{
1731+
"type": "object",
1732+
"properties": {
1733+
"foo": {
1734+
"type": "string"
1735+
}
1736+
}
1737+
},
1738+
{
1739+
"type": "object",
1740+
"properties": {
1741+
"bar": {
1742+
"type": "string"
1743+
}
1744+
}
1745+
}
1746+
]
1747+
},
1748+
"type": "array"
1749+
}
1750+
}
1751+
},
16861752
"DictionaryWithString": {
16871753
"description": "This is a string dictionary",
16881754
"type": "object",
@@ -2327,49 +2393,6 @@
23272393
}
23282394
}
23292395
},
2330-
"AnyOfAnyAndNull": {
2331-
"type": "object",
2332-
"properties": {
2333-
"data": {
2334-
"anyOf": [
2335-
{},
2336-
{
2337-
"type": "null"
2338-
}
2339-
]
2340-
}
2341-
}
2342-
},
2343-
"AnyOfArrays": {
2344-
"description": "This is a simple array with any of properties",
2345-
"type": "object",
2346-
"properties": {
2347-
"results": {
2348-
"items": {
2349-
"anyOf": [
2350-
{
2351-
"type": "object",
2352-
"properties": {
2353-
"foo": {
2354-
"type": "string"
2355-
}
2356-
}
2357-
},
2358-
{
2359-
"type": "object",
2360-
"properties": {
2361-
"bar": {
2362-
"type": "string"
2363-
}
2364-
}
2365-
}
2366-
]
2367-
},
2368-
"type": "array",
2369-
"title": "Results"
2370-
}
2371-
}
2372-
},
23732396
"CompositionBaseModel": {
23742397
"description": "This is a base model with two simple optional properties",
23752398
"type": "object",

0 commit comments

Comments
 (0)