Skip to content

Commit

Permalink
fix: strict mode nullable enum typing (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenwhite authored Nov 3, 2023
1 parent e868983 commit 13d3249
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/core/src/getters/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { keyword } from 'esutils';
import { isNumeric, sanitize } from '../utils';

export const getEnum = (value: string, enumName: string, names?: string[]) => {
let enumValue = `export type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}];\n`;
let enumValue = `export type ${enumName} = typeof ${enumName}[keyof typeof ${enumName}]`;

if (value.endsWith(' | null')) {
value = value.replace(' | null', '');
enumValue += ' | null';
}

enumValue += ';\n';

const implementation = getEnumImplementation(value, names);

Expand Down
7 changes: 7 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export default defineConfig({
target: '../generated/default/null-type-v3-0/endpoints.ts',
},
},
'nullable-enum': {
input: '../specifications/nullable-enum.yaml',
output: {
schemas: '../generated/default/nullable-enum/model',
target: '../generated/default/nullable-enum/endpoints.ts',
},
},
readonly: {
input: '../specifications/readonly.yaml',
output: {
Expand Down
66 changes: 66 additions & 0 deletions tests/specifications/nullable-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
openapi: 3.0.0
info:
title: Nullables
description: 'OpenAPI 3.0 Nullable types'
version: 1.0.0
tags:
- name: nullables
description: Nullable types
servers:
- url: http://localhost
paths:
/post-object-with-nullable-enum:
post:
tags:
- nullables
summary: Nullable object with nullable properties request
operationId: createNullableObject
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullableEnum'
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullableEnum'
/get-object-with-nullable-enum:
get:
tags:
- nullables
summary: Nullable object with nullable properties response
operationId: getNullableObject
parameters:
- name: nullableEnum
in: query
description: Nullable enum
required: false
schema:
type: string
nullable: true
enum:
- ONE
- TWO
- THREE
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullableEnum'
components:
schemas:
ObjectWithNullableEnum:
type: object
properties:
nullableEnum:
type: string
nullable: true
enum:
- ONE
- TWO
- THREE

0 comments on commit 13d3249

Please sign in to comment.