Skip to content

Commit

Permalink
fix(core): remove unnecessary empty item in case of null-only enum or…
Browse files Browse the repository at this point in the history
… empty enum (#1022) (#1023)
  • Loading branch information
bigen1925 authored Nov 8, 2023
1 parent e7dc399 commit f6f2d42
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 83 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/getters/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const getEnum = (value: string, enumName: string, names?: string[]) => {
};

export const getEnumImplementation = (value: string, names?: string[]) => {
// empty enum or null-only enum
if (value === '') return '';

return [...new Set(value.split(' | '))].reduce((acc, val, index) => {
const name = names?.[index];
if (name) {
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export const getScalar = ({
let isEnum = false;

if (enumItems) {
value = enumItems.map((enumItem: string) => `${enumItem}`).join(' | ');
value = enumItems
.map((enumItem: number | null) => `${enumItem}`)
.join(' | ');
isEnum = true;
}

Expand Down Expand Up @@ -84,12 +86,12 @@ export const getScalar = ({
let isEnum = false;

if (enumItems) {
value = `'${enumItems
.map((enumItem: string) =>
isString(enumItem) ? escape(enumItem) : `${enumItem}`,
value = `${enumItems
.map((enumItem: string | null) =>
isString(enumItem) ? `'${escape(enumItem)}'` : `${enumItem}`,
)
.filter(Boolean)
.join(`' | '`)}'`;
.join(` | `)}`;

isEnum = true;
}
Expand Down Expand Up @@ -131,12 +133,12 @@ export const getScalar = ({
case 'object':
default: {
if (enumItems) {
const value = `'${enumItems
.map((enumItem: string) =>
isString(enumItem) ? escape(enumItem) : `${enumItem}`,
const value = `${enumItems
.map((enumItem: unknown) =>
isString(enumItem) ? `'${escape(enumItem)}'` : `${enumItem}`,
)
.filter(Boolean)
.join(`' | '`)}'`;
.join(` | `)}`;

return {
value: value + nullable,
Expand Down
7 changes: 0 additions & 7 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ 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
32 changes: 31 additions & 1 deletion tests/specifications/null-type-v3-0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ paths:
$ref: '#/components/schemas/NullableAnyObject'
components:
schemas:
ObjectWithNullableObjectKey:
ObjectWithNullableItems:
type: object
properties:
nullableObject:
Expand All @@ -66,3 +66,33 @@ components:
NullableAnyObject:
type: object
nullable: true

# Nullable Enums
NullableStringEnum:
type: string
nullable: true
enum:
- ONE
- TWO
- THREE
- null
NullableNumberEnum:
type: number
nullable: true
enum:
- 1
- 2
- 3
- null
NullOnlyStringEnum:
type: string
nullable: true
enum:
- null
NullOnlyNumberEnum:
type: number
nullable: true
enum:
- null
EmptyEnum:
enum: []
66 changes: 0 additions & 66 deletions tests/specifications/nullable-enum.yaml

This file was deleted.

0 comments on commit f6f2d42

Please sign in to comment.