Skip to content

Commit

Permalink
fix(any-of): handle more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlubos committed Feb 1, 2024
1 parent 41743e8 commit f35bb12
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 69 deletions.
5 changes: 3 additions & 2 deletions src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const getModel = (
openApi: OpenApi,
definition: OpenApiSchema,
isDefinition: boolean = false,
name: string = ''
name: string = '',
parentDefinition: OpenApiSchema | null = null
): Model => {
const model: Model = {
name,
Expand Down Expand Up @@ -82,7 +83,7 @@ export const getModel = (
model.imports.push(...arrayItems.imports);
model.default = getModelDefault(definition, model);
return model;
} else if (definition.items.anyOf) {
} else if (definition.items.anyOf && parentDefinition) {
return getModel(openApi, definition.items);
} else {
const arrayItems = getModel(openApi, definition.items);
Expand Down
12 changes: 1 addition & 11 deletions src/openApi/v3/parser/getModelComposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ export const getModelComposition = (
const properties: Model[] = [];

definitions
.map(definition => getModel(openApi, definition))
.filter(model => {
const hasProperties = model.properties.length;
const hasEnums = model.enums.length;
const hasLink = typeof model.link !== 'undefined' && model.link !== null;
console.log(model.name, model)
const isObject = model.type === 'any';
const isDictionary = model.export === 'dictionary';
const isEmpty = isObject && !hasProperties && !hasEnums && !hasLink;
return !isEmpty || isDictionary;
})
.map(def => getModel(openApi, def, undefined, undefined, definition))
.forEach(model => {
composition.imports.push(...model.imports);
composition.enums.push(...model.enums);
Expand Down
77 changes: 64 additions & 13 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3692,6 +3692,7 @@ export type { OpenAPIConfig } from './core/OpenAPI';
export type { _default } from './models/_default';
export type { AnyOfAnyAndNull } from './models/AnyOfAnyAndNull';
export type { AnyOfArrays } from './models/AnyOfArrays';
export type { ArrayWithAnyOfProperties } from './models/ArrayWithAnyOfProperties';
export type { ArrayWithArray } from './models/ArrayWithArray';
export type { ArrayWithBooleans } from './models/ArrayWithBooleans';
export type { ArrayWithNumbers } from './models/ArrayWithNumbers';
Expand Down Expand Up @@ -3769,6 +3770,7 @@ export type { SimpleStringWithPattern } from './models/SimpleStringWithPattern';
export { $_default } from './schemas/$_default';
export { $AnyOfAnyAndNull } from './schemas/$AnyOfAnyAndNull';
export { $AnyOfArrays } from './schemas/$AnyOfArrays';
export { $ArrayWithAnyOfProperties } from './schemas/$ArrayWithAnyOfProperties';
export { $ArrayWithArray } from './schemas/$ArrayWithArray';
export { $ArrayWithBooleans } from './schemas/$ArrayWithBooleans';
export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers';
Expand Down Expand Up @@ -3885,7 +3887,7 @@ exports[`v3 should generate: test/generated/v3/models/AnyOfAnyAndNull.ts 1`] = `
/* tslint:disable */
/* eslint-disable */
export type AnyOfAnyAndNull = {
data?: any | null;
data?: (any | null);
};

"
Expand All @@ -3910,6 +3912,22 @@ export type AnyOfArrays = {
"
`;

exports[`v3 should generate: test/generated/v3/models/ArrayWithAnyOfProperties.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
/**
* This is a simple array with any of properties
*/
export type ArrayWithAnyOfProperties = Array<({
foo?: string;
} | {
bar?: string;
})>;
"
`;

exports[`v3 should generate: test/generated/v3/models/ArrayWithArray.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down Expand Up @@ -5118,6 +5136,9 @@ export const $AnyOfAnyAndNull = {
data: {
type: 'any-of',
contains: [{
properties: {
},
}, {
type: 'null',
}],
},
Expand All @@ -5135,26 +5156,56 @@ export const $AnyOfArrays = {
description: \`This is a simple array with any of properties\`,
properties: {
results: {
type: 'any-of',
contains: [{
properties: {
foo: {
type: 'string',
type: 'array',
contains: {
type: 'any-of',
contains: [{
properties: {
foo: {
type: 'string',
},
},
},
}, {
properties: {
bar: {
type: 'string',
}, {
properties: {
bar: {
type: 'string',
},
},
},
}],
}],
},
},
},
} as const;
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$ArrayWithAnyOfProperties.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
export const $ArrayWithAnyOfProperties = {
type: 'array',
contains: {
type: 'any-of',
contains: [{
properties: {
foo: {
type: 'string',
},
},
}, {
properties: {
bar: {
type: 'string',
},
},
}],
},
} as const;
"
`;

exports[`v3 should generate: test/generated/v3/schemas/$ArrayWithArray.ts 1`] = `
"/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
Expand Down
109 changes: 66 additions & 43 deletions test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,72 @@
}
}
},
"ArrayWithAnyOfProperties": {
"description": "This is a simple array with any of properties",
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "string"
}
}
}
]
}
},
"AnyOfAnyAndNull": {
"type": "object",
"properties": {
"data": {
"anyOf": [
{},
{
"type": "null"
}
]
}
}
},
"AnyOfArrays": {
"description": "This is a simple array with any of properties",
"type": "object",
"properties": {
"results": {
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "string"
}
}
}
]
},
"type": "array"
}
}
},
"DictionaryWithString": {
"description": "This is a string dictionary",
"type": "object",
Expand Down Expand Up @@ -2327,49 +2393,6 @@
}
}
},
"AnyOfAnyAndNull": {
"type": "object",
"properties": {
"data": {
"anyOf": [
{},
{
"type": "null"
}
]
}
}
},
"AnyOfArrays": {
"description": "This is a simple array with any of properties",
"type": "object",
"properties": {
"results": {
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "string"
}
}
}
]
},
"type": "array",
"title": "Results"
}
}
},
"CompositionBaseModel": {
"description": "This is a base model with two simple optional properties",
"type": "object",
Expand Down

0 comments on commit f35bb12

Please sign in to comment.