Skip to content

Commit

Permalink
Merge pull request #392 from hey-api/fix/prefer-unknown-over-any
Browse files Browse the repository at this point in the history
fix: prefer unknown type over any
  • Loading branch information
jordanshatford authored Apr 16, 2024
2 parents a9a8783 + 90c383a commit a2f68f6
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-rice-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

fix: prefer unknown type over any
9 changes: 9 additions & 0 deletions docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ This config option is deprecated and will be removed.

## v0.39.0

### Prefer `unknown` over `any`

Types that cannot be determined will now be generated as `unknown` instead of `any`.

```js
200: any // [!code --]
200: unknown // [!code ++]
```

### Single `enums.gen.ts` file

Enums are now exported from a separate file. If you use imports from `models.ts`, you can change them to `enums.gen.ts`.
Expand Down
8 changes: 4 additions & 4 deletions packages/openapi-ts/src/openApi/v2/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getModel = (
): Model => {
const model: Model = {
$refs: [],
base: 'any',
base: 'unknown',
description: definition.description || null,
enum: [],
enums: [],
Expand All @@ -42,7 +42,7 @@ export const getModel = (
pattern: getPattern(definition.pattern),
properties: [],
template: null,
type: 'any',
type: 'unknown',
uniqueItems: definition.uniqueItems,
};

Expand Down Expand Up @@ -120,8 +120,8 @@ export const getModel = (

if (definition.type === 'object') {
model.export = 'interface';
model.type = 'any';
model.base = 'any';
model.type = 'unknown';
model.base = 'unknown';

if (definition.properties) {
const modelProperties = getModelProperties(openApi, definition, getModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const getModelComposition = (
.filter(model => {
const hasProperties = model.properties.length;
const hasEnums = model.enums.length;
const isObject = model.type === 'any';
const isObject = model.type === 'unknown';
const isEmpty = isObject && !hasProperties && !hasEnums;
return !isEmpty;
})
Expand Down Expand Up @@ -69,7 +69,7 @@ export const getModelComposition = (
if (properties.length) {
composition.properties.push({
$refs: [],
base: 'any',
base: 'unknown',
description: '',
enum: [],
enums: [],
Expand All @@ -83,7 +83,7 @@ export const getModelComposition = (
name: 'properties',
properties,
template: null,
type: 'any',
type: 'unknown',
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getModel } from './getModel';
export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => {
const operationParameter: OperationParameter = {
$refs: [],
base: 'any',
base: 'unknown',
description: parameter.description || null,
enum: [],
enums: [],
Expand Down Expand Up @@ -41,7 +41,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
prop: parameter.name,
properties: [],
template: null,
type: 'any',
type: 'unknown',
uniqueItems: parameter.uniqueItems,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getOperationResponse = (
): OperationResponse => {
const operationResponse: OperationResponse = {
$refs: [],
base: responseCode !== 204 ? 'any' : 'void',
base: responseCode !== 204 ? 'unknown' : 'void',
code: responseCode,
description: response.description || null,
enum: [],
Expand All @@ -30,7 +30,7 @@ export const getOperationResponse = (
name: '',
properties: [],
template: null,
type: responseCode !== 204 ? 'any' : 'void',
type: responseCode !== 204 ? 'unknown' : 'void',
};

// If this response has a schema, then we need to check two things:
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi-ts/src/openApi/v3/parser/getModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ export const getModel = (

if (definition.type === 'object' || definition.properties) {
if (definition.properties) {
model.base = 'any';
model.base = 'unknown';
model.export = 'interface';
model.type = 'any';
model.type = 'unknown';
model.default = getDefault(definition, model);

const modelProperties = getModelProperties(openApi, definition, getModel, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const getModelComposition = ({
} else {
composition.properties.push({
$refs: [],
base: 'any',
base: 'unknown',
description: '',
enum: [],
enums: [],
Expand All @@ -117,7 +117,7 @@ export const getModelComposition = ({
name: 'properties',
properties,
template: null,
type: 'any',
type: 'unknown',
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getModel } from './getModel';
export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParameter): OperationParameter => {
const operationParameter: OperationParameter = {
$refs: [],
base: 'any',
base: 'unknown',
deprecated: parameter.deprecated === true,
description: parameter.description || null,
enum: [],
Expand All @@ -30,7 +30,7 @@ export const getOperationParameter = (openApi: OpenApi, parameter: OpenApiParame
prop: parameter.name,
properties: [],
template: null,
type: 'any',
type: 'unknown',
};

if (parameter.$ref) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getModel } from './getModel';
export const getOperationRequestBody = (openApi: OpenApi, body: OpenApiRequestBody): OperationParameter => {
const requestBody: OperationParameter = {
$refs: [],
base: 'any',
base: 'unknown',
default: undefined,
description: body.description || null,
enum: [],
Expand All @@ -27,7 +27,7 @@ export const getOperationRequestBody = (openApi: OpenApi, body: OpenApiRequestBo
prop: body['x-body-name'] ?? 'requestBody',
properties: [],
template: null,
type: 'any',
type: 'unknown',
};

if (body.content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getOperationResponse = (
): OperationResponse => {
const operationResponse: OperationResponse = {
$refs: [],
base: responseCode !== 204 ? 'any' : 'void',
base: responseCode !== 204 ? 'unknown' : 'void',
code: responseCode,
description: response.description || null,
enum: [],
Expand All @@ -31,7 +31,7 @@ export const getOperationResponse = (
name: '',
properties: [],
template: null,
type: responseCode !== 204 ? 'any' : 'void',
type: responseCode !== 204 ? 'unknown' : 'void',
};

if (response.content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ export type $OpenApiTs = {
/**
* Response is a simple number
*/
200: any;
200: unknown;
/**
* Success
*/
Expand Down Expand Up @@ -778,7 +778,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class NoContentService {
}

/**
* @returns any Response is a simple number
* @returns unknown Response is a simple number
* @returns void Success
* @throws ApiError
*/
Expand All @@ -333,7 +333,7 @@ export class NoContentService {

export class ResponseService {
/**
* @returns any Response is a simple number
* @returns unknown Response is a simple number
* @returns void Success
* @throws ApiError
*/
Expand Down Expand Up @@ -377,7 +377,7 @@ export class ResponseService {
}

/**
* @returns any Message for 200 response
* @returns unknown Message for 200 response
* @returns ModelWithString Message for default response
* @returns ModelThatExtends Message for 201 response
* @returns ModelThatExtendsExtends Message for 202 response
Expand Down Expand Up @@ -490,7 +490,7 @@ export class TypesService {
* @returns number Response is a simple number
* @returns string Response is a simple string
* @returns boolean Response is a simple boolean
* @returns any Response is a simple object
* @returns unknown Response is a simple object
* @throws ApiError
*/
public static types(
Expand Down Expand Up @@ -576,7 +576,7 @@ export class HeaderService {

export class ErrorService {
/**
* @returns any Custom message: Successful response
* @returns unknown Custom message: Successful response
* @throws ApiError
*/
public static testErrorCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export class ResponseService {
}

/**
* @returns any Message for 200 response
* @returns unknown Message for 200 response
* @returns ModelWithString Message for default response
* @returns ModelThatExtends Message for 201 response
* @returns ModelThatExtendsExtends Message for 202 response
Expand Down Expand Up @@ -789,7 +789,7 @@ export class MultipartService {
}

/**
* @returns any OK
* @returns unknown OK
* @throws ApiError
*/
public static multipartResponse(): CancelablePromise<
Expand Down Expand Up @@ -824,7 +824,7 @@ export class HeaderService {

export class ErrorService {
/**
* @returns any Custom message: Successful response
* @returns unknown Custom message: Successful response
* @throws ApiError
*/
public static testErrorCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export class ResponseService {
}

/**
* @returns any Message for 200 response
* @returns unknown Message for 200 response
* @returns ModelWithString Message for default response
* @returns ModelThatExtends Message for 201 response
* @returns ModelThatExtendsExtends Message for 202 response
Expand Down Expand Up @@ -877,7 +877,7 @@ export class MultipartService {
}

/**
* @returns any OK
* @returns unknown OK
* @throws ApiError
*/
public multipartResponse(): Observable<$OpenApiTs['/api/v{api-version}/multipart']['get']['res'][200]> {
Expand Down Expand Up @@ -918,7 +918,7 @@ export class ErrorService {
constructor(public readonly http: HttpClient) {}

/**
* @returns any Custom message: Successful response
* @returns unknown Custom message: Successful response
* @throws ApiError
*/
public testErrorCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class ResponseService {
}

/**
* @returns any Message for 200 response
* @returns unknown Message for 200 response
* @returns ModelWithString Message for default response
* @returns ModelThatExtends Message for 201 response
* @returns ModelThatExtendsExtends Message for 202 response
Expand Down Expand Up @@ -828,7 +828,7 @@ export class MultipartService {
}

/**
* @returns any OK
* @returns unknown OK
* @throws ApiError
*/
public multipartResponse(): CancelablePromise<$OpenApiTs['/api/v{api-version}/multipart']['get']['res'][200]> {
Expand Down Expand Up @@ -863,7 +863,7 @@ export class ErrorService {
constructor(public readonly httpRequest: BaseHttpRequest) {}

/**
* @returns any Custom message: Successful response
* @returns unknown Custom message: Successful response
* @throws ApiError
*/
public testErrorCode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ export type $OpenApiTs = {
/**
* Custom message: Successful response
*/
200: any;
200: unknown;
};
};
};
Expand Down
Loading

0 comments on commit a2f68f6

Please sign in to comment.