Skip to content

Commit

Permalink
Support void as unknownType
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorcamachoo committed Feb 26, 2025
1 parent 7603554 commit e95563a
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/parser-ts/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ export function createOmitDeclaration({
export const keywordTypeNodes = {
any: factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
unknown: factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
void: factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
number: factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
integer: factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
object: factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword),
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-faker/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Options } from './types.ts'
const fakerKeywordMapper = {
any: () => 'undefined',
unknown: () => 'unknown',
void: () => 'void',
number: (min?: number, max?: number) => {
if (max !== undefined && min !== undefined) {
return `faker.number.float({ min: ${min}, max: ${max} })`
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-oas/src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Context<TOptions, TPluginOptions extends PluginFactoryOptions> = {

export type SchemaGeneratorOptions = {
dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
unknownType: 'any' | 'unknown'
unknownType: 'any' | 'unknown' | 'void'
enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal'
enumSuffix?: string
usedEnumNames?: Record<string, number>
Expand Down Expand Up @@ -260,6 +260,9 @@ export class SchemaGenerator<
if (options.unknownType === 'any') {
return schemaKeywords.any
}
if (options.unknownType === 'void') {
return schemaKeywords.void
}

return schemaKeywords.unknown
}
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-oas/src/SchemaMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export type SchemaKeywordMapper = {
null: { keyword: 'null' }
any: { keyword: 'any' }
unknown: { keyword: 'unknown' }
void: { keyword: 'void' }
blob: { keyword: 'blob' }
schema: { keyword: 'schema'; args: { type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object'; format?: string } }
name: { keyword: 'name'; args: string }
Expand Down Expand Up @@ -110,6 +111,7 @@ export const schemaKeywords = {
email: 'email',
uuid: 'uuid',
url: 'url',
void: 'void',
/* intersection */
default: 'default',
const: 'const',
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-ts/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ paths:
properties:
name:
$ref: "#/components/schemas/ErrorCode"
'202':
description: Accepted response
default:
description: unexpected error
content:
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-ts/src/generators/__snapshots__/createPet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type CreatePets201 = error & {
name?: errorCode
}

/**
* @description Accepted response
*/
export type CreatePets202 = any

/**
* @description unexpected error
*/
Expand All @@ -24,10 +29,10 @@ export type CreatePetsMutationRequest = {
tag: string
}

export type CreatePetsMutationResponse = createPets201
export type CreatePetsMutationResponse = createPets201 | createPets202

export type createPetsMutation = {
Response: createPets201
Response: createPets201 | createPets202
Request: createPetsMutationRequest
Errors: any
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type CreatePets201 = error & {
name?: errorCode
}

/**
* @description Accepted response
*/
export type CreatePets202 = unknown

/**
* @description unexpected error
*/
Expand All @@ -24,10 +29,10 @@ export type CreatePetsMutationRequest = {
tag: string
}

export type CreatePetsMutationResponse = createPets201
export type CreatePetsMutationResponse = createPets201 | createPets202

export type createPetsMutation = {
Response: createPets201
Response: createPets201 | createPets202
Request: createPetsMutationRequest
Errors: any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @description Null response
*/
export type CreatePets201 = error & {
/**
* @type object | undefined
*/
name?: errorCode
}

/**
* @description Accepted response
*/
export type CreatePets202 = void

/**
* @description unexpected error
*/
export type CreatePetsError = error

export type CreatePetsMutationRequest = {
/**
* @type string
*/
name: string
/**
* @type string
*/
tag: string
}

export type CreatePetsMutationResponse = createPets201 | createPets202

export type createPetsMutation = {
Response: createPets201 | createPets202
Request: createPetsMutationRequest
Errors: any
}
9 changes: 9 additions & 0 deletions packages/plugin-ts/src/generators/typeGenerator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,15 @@ describe('typeGenerator operation', async () => {
unknownType: 'unknown',
},
},
{
name: 'createPet with unknownType void',
input: '../../mocks/petStore.yaml',
path: '/pets',
method: 'post',
options: {
unknownType: 'void',
},
},
{
name: 'deletePet',
input: '../../mocks/petStore.yaml',
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ts/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type ts from 'typescript'
export const typeKeywordMapper = {
any: () => factory.keywordTypeNodes.any,
unknown: () => factory.keywordTypeNodes.unknown,
void: () => factory.keywordTypeNodes.void,
number: () => factory.keywordTypeNodes.number,
integer: () => factory.keywordTypeNodes.number,
object: (nodes?: ts.TypeElement[]) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type Options = {
* Which type to use when the Swagger/OpenAPI file is not providing more information.
* @default 'any'
*/
unknownType?: 'any' | 'unknown'
unknownType?: 'any' | 'unknown' | 'void'
/**
* Choose what to use as mode for an optional value.
* @examples 'questionToken': type?: string
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-zod/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Schema, SchemaKeywordBase, SchemaMapper } from '@kubb/plugin-oas'
const zodKeywordMapper = {
any: () => 'z.any()',
unknown: () => 'z.unknown()',
void: () => 'z.void()',
number: (coercion?: boolean, min?: number, max?: number) => {
return [coercion ? 'z.coerce.number()' : 'z.number()', min !== undefined ? `.min(${min})` : undefined, max !== undefined ? `.max(${max})` : undefined]
.filter(Boolean)
Expand Down

0 comments on commit e95563a

Please sign in to comment.