Skip to content

Commit

Permalink
Merge pull request #499 from hey-api/fix/additional-properties-union
Browse files Browse the repository at this point in the history
fix: handle additional properties union
  • Loading branch information
mrlubos authored Apr 27, 2024
2 parents 0ca8b75 + 519b646 commit e24eece
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-ducks-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: handle additional properties union
5 changes: 5 additions & 0 deletions examples/openapi-ts-fetch/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export type Order = {
complete?: boolean;
};

/**
* Order Status
*/
export type status = 'placed' | 'approved' | 'delivered';

export type Customer = {
id?: number;
username?: string;
Expand Down
12 changes: 12 additions & 0 deletions packages/openapi-ts/src/openApi/v3/parser/getModelProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { escapeName } from '../../../utils/escape';
import { unique } from '../../../utils/unique';
import type { Model } from '../../common/interfaces/client';
import { getDefault } from '../../common/parser/getDefault';
import { getPattern } from '../../common/parser/getPattern';
Expand Down Expand Up @@ -38,6 +39,17 @@ export const getAdditionalPropertiesModel = (
}

if (definition.additionalProperties && definition.properties) {
const additionalPropertiesType =
typeof definition.additionalProperties === 'object' &&
definition.additionalProperties.type &&
!Array.isArray(definition.additionalProperties.type)
? definition.additionalProperties.type
: apModel.base;
const additionalProperties = [
additionalPropertiesType,
...model.properties.map((property) => property.base),
];
apModel.base = additionalProperties.filter(unique).join(' | ');
apModel.default = getDefault(definition, model);
apModel.export = 'generic';
apModel.isRequired = definition.additionalProperties === true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string'
type: 'number'
},
bar: {
type: 'boolean'
}
},
additionalProperties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ export type DictionaryWithString = {
};

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: (string) | undefined;
foo?: number;
bar?: boolean;
[key: string]: (string | number | boolean) | undefined;
};

/**
Expand Down Expand Up @@ -677,7 +678,7 @@ export type ModelWithAdditionalPropertiesEqTrue = {
* This is a simple string property
*/
prop?: string;
[key: string]: unknown;
[key: string]: unknown | string;
};

export type NestedAnyOfArraysNullable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ export type DictionaryWithString = {
};

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: (string) | undefined;
foo?: number;
bar?: boolean;
[key: string]: (string | number | boolean) | undefined;
};

/**
Expand Down Expand Up @@ -677,7 +678,7 @@ export type ModelWithAdditionalPropertiesEqTrue = {
* This is a simple string property
*/
prop?: string;
[key: string]: unknown;
[key: string]: unknown | string;
};

export type NestedAnyOfArraysNullable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ export type DictionaryWithString = {
};

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: (string) | undefined;
foo?: number;
bar?: boolean;
[key: string]: (string | number | boolean) | undefined;
};

/**
Expand Down Expand Up @@ -677,7 +678,7 @@ export type ModelWithAdditionalPropertiesEqTrue = {
* This is a simple string property
*/
prop?: string;
[key: string]: unknown;
[key: string]: unknown | string;
};

export type NestedAnyOfArraysNullable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ export type DictionaryWithString = {
};

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: (string) | undefined;
foo?: number;
bar?: boolean;
[key: string]: (string | number | boolean) | undefined;
};

/**
Expand Down Expand Up @@ -677,7 +678,7 @@ export type ModelWithAdditionalPropertiesEqTrue = {
* This is a simple string property
*/
prop?: string;
[key: string]: unknown;
[key: string]: unknown | string;
};

export type NestedAnyOfArraysNullable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ export type DictionaryWithString = {
};

export type DictionaryWithPropertiesAndAdditionalProperties = {
foo?: string;
[key: string]: (string) | undefined;
foo?: number;
bar?: boolean;
[key: string]: (string | number | boolean) | undefined;
};

/**
Expand Down Expand Up @@ -677,7 +678,7 @@ export type ModelWithAdditionalPropertiesEqTrue = {
* This is a simple string property
*/
prop?: string;
[key: string]: unknown;
[key: string]: unknown | string;
};

export type NestedAnyOfArraysNullable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string'
type: 'number'
},
bar: {
type: 'boolean'
}
},
additionalProperties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export const $DictionaryWithPropertiesAndAdditionalProperties = {
type: 'object',
properties: {
foo: {
type: 'string'
type: 'number'
},
bar: {
type: 'boolean'
}
},
additionalProperties: {
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi-ts/test/sample.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ const main = async () => {
enums: 'javascript',
input: './test/spec/v3.json',
output: './test/generated/v3/',
schemas: {
export: false,
},
services: {
export: false,
},
types: {
include: '^DictionaryWithPropertiesAndAdditionalProperties',
},
};

const { createClient } = await import(
path.resolve(process.cwd(), 'dist/index.js')
path.resolve(process.cwd(), 'dist/node/index.cjs')
);
await createClient(config);
};
Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-ts/test/spec/v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,10 @@
"type": "object",
"properties": {
"foo": {
"type": "string"
"type": "number"
},
"bar": {
"type": "boolean"
}
},
"additionalProperties": {
Expand Down

0 comments on commit e24eece

Please sign in to comment.