Skip to content

Commit abaa6f7

Browse files
Merge pull request ferdikoomen#6 from nicolas-chaulet/union-bugs
Introduce a couple of broken schema parsing
2 parents 76e6851 + f35bb12 commit abaa6f7

File tree

4 files changed

+291
-12
lines changed

4 files changed

+291
-12
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +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 isObject = model.type === 'any';
34-
const isDictionary = model.export === 'dictionary';
35-
const isEmpty = isObject && !hasProperties && !hasEnums;
36-
return !isEmpty || isDictionary;
37-
})
29+
.map(def => getModel(openApi, def, undefined, undefined, definition))
3830
.forEach(model => {
3931
composition.imports.push(...model.imports);
4032
composition.enums.push(...model.enums);

test/__snapshots__/index.spec.ts.snap

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,6 +3690,9 @@ export { OpenAPI } from './core/OpenAPI';
36903690
export type { OpenAPIConfig } from './core/OpenAPI';
36913691

36923692
export type { _default } from './models/_default';
3693+
export type { AnyOfAnyAndNull } from './models/AnyOfAnyAndNull';
3694+
export type { AnyOfArrays } from './models/AnyOfArrays';
3695+
export type { ArrayWithAnyOfProperties } from './models/ArrayWithAnyOfProperties';
36933696
export type { ArrayWithArray } from './models/ArrayWithArray';
36943697
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
36953698
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
@@ -3708,6 +3711,7 @@ export type { CompositionWithAllOfAndNullable } from './models/CompositionWithAl
37083711
export type { CompositionWithAnyOf } from './models/CompositionWithAnyOf';
37093712
export type { CompositionWithAnyOfAndNullable } from './models/CompositionWithAnyOfAndNullable';
37103713
export type { CompositionWithAnyOfAnonymous } from './models/CompositionWithAnyOfAnonymous';
3714+
export type { CompositionWithNestedAnyAndTypeNull } from './models/CompositionWithNestedAnyAndTypeNull';
37113715
export type { CompositionWithNestedAnyOfAndNull } from './models/CompositionWithNestedAnyOfAndNull';
37123716
export type { CompositionWithOneOf } from './models/CompositionWithOneOf';
37133717
export type { CompositionWithOneOfAndComplexArrayDictionary } from './models/CompositionWithOneOfAndComplexArrayDictionary';
@@ -3764,6 +3768,9 @@ export type { SimpleString } from './models/SimpleString';
37643768
export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
37653769

37663770
export { $_default } from './schemas/$_default';
3771+
export { $AnyOfAnyAndNull } from './schemas/$AnyOfAnyAndNull';
3772+
export { $AnyOfArrays } from './schemas/$AnyOfArrays';
3773+
export { $ArrayWithAnyOfProperties } from './schemas/$ArrayWithAnyOfProperties';
37673774
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
37683775
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
37693776
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
@@ -3782,6 +3789,7 @@ export { $CompositionWithAllOfAndNullable } from './schemas/$CompositionWithAllO
37823789
export { $CompositionWithAnyOf } from './schemas/$CompositionWithAnyOf';
37833790
export { $CompositionWithAnyOfAndNullable } from './schemas/$CompositionWithAnyOfAndNullable';
37843791
export { $CompositionWithAnyOfAnonymous } from './schemas/$CompositionWithAnyOfAnonymous';
3792+
export { $CompositionWithNestedAnyAndTypeNull } from './schemas/$CompositionWithNestedAnyAndTypeNull';
37853793
export { $CompositionWithNestedAnyOfAndNull } from './schemas/$CompositionWithNestedAnyOfAndNull';
37863794
export { $CompositionWithOneOf } from './schemas/$CompositionWithOneOf';
37873795
export { $CompositionWithOneOfAndComplexArrayDictionary } from './schemas/$CompositionWithOneOfAndComplexArrayDictionary';
@@ -3873,6 +3881,53 @@ export type _default = {
38733881
"
38743882
`;
38753883

3884+
exports[`v3 should generate: test/generated/v3/models/AnyOfAnyAndNull.ts 1`] = `
3885+
"/* generated using openapi-typescript-codegen -- do no edit */
3886+
/* istanbul ignore file */
3887+
/* tslint:disable */
3888+
/* eslint-disable */
3889+
export type AnyOfAnyAndNull = {
3890+
data?: (any | null);
3891+
};
3892+
3893+
"
3894+
`;
3895+
3896+
exports[`v3 should generate: test/generated/v3/models/AnyOfArrays.ts 1`] = `
3897+
"/* generated using openapi-typescript-codegen -- do no edit */
3898+
/* istanbul ignore file */
3899+
/* tslint:disable */
3900+
/* eslint-disable */
3901+
/**
3902+
* This is a simple array with any of properties
3903+
*/
3904+
export type AnyOfArrays = {
3905+
results?: Array<({
3906+
foo?: string;
3907+
} | {
3908+
bar?: string;
3909+
})>;
3910+
};
3911+
3912+
"
3913+
`;
3914+
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+
38763931
exports[`v3 should generate: test/generated/v3/models/ArrayWithArray.ts 1`] = `
38773932
"/* generated using openapi-typescript-codegen -- do no edit */
38783933
/* istanbul ignore file */
@@ -4135,6 +4190,23 @@ export type CompositionWithAnyOfAnonymous = {
41354190
"
41364191
`;
41374192

4193+
exports[`v3 should generate: test/generated/v3/models/CompositionWithNestedAnyAndTypeNull.ts 1`] = `
4194+
"/* generated using openapi-typescript-codegen -- do no edit */
4195+
/* istanbul ignore file */
4196+
/* tslint:disable */
4197+
/* eslint-disable */
4198+
import type { ModelWithArray } from './ModelWithArray';
4199+
import type { ModelWithDictionary } from './ModelWithDictionary';
4200+
/**
4201+
* This is a model with nested 'any of' property with a type null
4202+
*/
4203+
export type CompositionWithNestedAnyAndTypeNull = {
4204+
propA?: ((ModelWithDictionary | null) | (ModelWithArray | null));
4205+
};
4206+
4207+
"
4208+
`;
4209+
41384210
exports[`v3 should generate: test/generated/v3/models/CompositionWithNestedAnyOfAndNull.ts 1`] = `
41394211
"/* generated using openapi-typescript-codegen -- do no edit */
41404212
/* istanbul ignore file */
@@ -5054,6 +5126,86 @@ export const $_default = {
50545126
"
50555127
`;
50565128

5129+
exports[`v3 should generate: test/generated/v3/schemas/$AnyOfAnyAndNull.ts 1`] = `
5130+
"/* generated using openapi-typescript-codegen -- do no edit */
5131+
/* istanbul ignore file */
5132+
/* tslint:disable */
5133+
/* eslint-disable */
5134+
export const $AnyOfAnyAndNull = {
5135+
properties: {
5136+
data: {
5137+
type: 'any-of',
5138+
contains: [{
5139+
properties: {
5140+
},
5141+
}, {
5142+
type: 'null',
5143+
}],
5144+
},
5145+
},
5146+
} as const;
5147+
"
5148+
`;
5149+
5150+
exports[`v3 should generate: test/generated/v3/schemas/$AnyOfArrays.ts 1`] = `
5151+
"/* generated using openapi-typescript-codegen -- do no edit */
5152+
/* istanbul ignore file */
5153+
/* tslint:disable */
5154+
/* eslint-disable */
5155+
export const $AnyOfArrays = {
5156+
description: \`This is a simple array with any of properties\`,
5157+
properties: {
5158+
results: {
5159+
type: 'array',
5160+
contains: {
5161+
type: 'any-of',
5162+
contains: [{
5163+
properties: {
5164+
foo: {
5165+
type: 'string',
5166+
},
5167+
},
5168+
}, {
5169+
properties: {
5170+
bar: {
5171+
type: 'string',
5172+
},
5173+
},
5174+
}],
5175+
},
5176+
},
5177+
},
5178+
} as const;
5179+
"
5180+
`;
5181+
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+
50575209
exports[`v3 should generate: test/generated/v3/schemas/$ArrayWithArray.ts 1`] = `
50585210
"/* generated using openapi-typescript-codegen -- do no edit */
50595211
/* istanbul ignore file */
@@ -5386,6 +5538,37 @@ export const $CompositionWithAnyOfAnonymous = {
53865538
"
53875539
`;
53885540

5541+
exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithNestedAnyAndTypeNull.ts 1`] = `
5542+
"/* generated using openapi-typescript-codegen -- do no edit */
5543+
/* istanbul ignore file */
5544+
/* tslint:disable */
5545+
/* eslint-disable */
5546+
export const $CompositionWithNestedAnyAndTypeNull = {
5547+
description: \`This is a model with nested 'any of' property with a type null\`,
5548+
properties: {
5549+
propA: {
5550+
type: 'any-of',
5551+
contains: [{
5552+
type: 'any-of',
5553+
contains: [{
5554+
type: 'ModelWithDictionary',
5555+
}, {
5556+
type: 'null',
5557+
}],
5558+
}, {
5559+
type: 'any-of',
5560+
contains: [{
5561+
type: 'ModelWithArray',
5562+
}, {
5563+
type: 'null',
5564+
}],
5565+
}],
5566+
},
5567+
},
5568+
} as const;
5569+
"
5570+
`;
5571+
53895572
exports[`v3 should generate: test/generated/v3/schemas/$CompositionWithNestedAnyOfAndNull.ts 1`] = `
53905573
"/* generated using openapi-typescript-codegen -- do no edit */
53915574
/* istanbul ignore file */

test/spec/v3.json

Lines changed: 104 additions & 1 deletion
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",
@@ -2095,7 +2161,44 @@
20952161
}
20962162
}
20972163
},
2098-
"Enum1":{
2164+
"CompositionWithNestedAnyAndTypeNull": {
2165+
"description": "This is a model with nested 'any of' property with a type null",
2166+
"type": "object",
2167+
"properties": {
2168+
"propA": {
2169+
"type": "object",
2170+
"anyOf": [
2171+
{
2172+
"items": {
2173+
"anyOf": [
2174+
{
2175+
"$ref": "#/components/schemas/ModelWithDictionary"
2176+
},
2177+
{
2178+
"type": "null"
2179+
}
2180+
]
2181+
},
2182+
"type": "array"
2183+
},
2184+
{
2185+
"items": {
2186+
"anyOf": [
2187+
{
2188+
"$ref": "#/components/schemas/ModelWithArray"
2189+
},
2190+
{
2191+
"type": "null"
2192+
}
2193+
]
2194+
},
2195+
"type": "array"
2196+
}
2197+
]
2198+
}
2199+
}
2200+
},
2201+
"Enum1": {
20992202
"enum": [
21002203
"Bird",
21012204
"Dog"

0 commit comments

Comments
 (0)