Skip to content

Commit

Permalink
feat: support x-enum-varnames
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaim Lev-Ari committed Dec 18, 2023
1 parent c8d4ead commit e8e6e8c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
1 change: 1 addition & 0 deletions e2e/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const schemas = [
['requestBody', './schemas/requestBody.yaml'],
['box', './schemas/box.json'],
['digitalocean', './schemas/digitalocean.yaml'],
['enums', './schemas/enums.yaml']
]

/** @type {import('@kubb/core').KubbUserConfig} */
Expand Down
39 changes: 39 additions & 0 deletions e2e/schemas/enums.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
definitions:
enumVarNames.Type:
enum:
- 0
- 1
type: integer
x-enum-varnames:
- Pending
- Received
enumNames.Type:
enum:
- 0
- 1
x-enumNames:
- Pending
- Received
swagger: "2.0"
info:
title: test
version: "1.0.0"
paths:
/var-names:
get:
consumes:
- application/json
responses:
200:
description: Success
schema:
$ref: '#/definitions/enumVarNames.Type'
/names:
get:
consumes:
- application/json
responses:
200:
description: Success
schema:
$ref: '#/definitions/enumNames.Type'
21 changes: 14 additions & 7 deletions packages/swagger-faker/src/FakerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,22 @@ export class FakerGenerator extends Generator<PluginOptions['resolvedOptions'],
}

if (schema.enum) {
if ('x-enumNames' in schema) {
return [
{
keyword: fakerKeywords.enum,
args: [`[${[...new Set(schema['x-enumNames'] as string[])].map((value) => `\`${value}\``).join(', ')}]`],
},
]
const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
return [
{
keyword: fakerKeywords.enum,
args: [`[${[...new Set(schema[extensionKey] as string[])].map((value) => `\`${value}\``).join(', ')}]`],
},
]
}
})

if (extensionEnums.length > 0) {
return extensionEnums[0]

Check warning on line 258 in packages/swagger-faker/src/FakerGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-faker/src/FakerGenerator.ts#L246-L258

Added lines #L246 - L258 were not covered by tests
}


Check warning on line 261 in packages/swagger-faker/src/FakerGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-faker/src/FakerGenerator.ts#L261

Added line #L261 was not covered by tests
if (schema.type === 'number' || schema.type === 'integer') {
return [
{
Expand Down
16 changes: 12 additions & 4 deletions packages/swagger-ts/src/TypeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,20 @@ export class TypeGenerator extends Generator<PluginOptions['resolvedOptions'], C

let enums: [key: string, value: string | number][] = [...new Set(schema.enum)].map((key) => [key, key])

if ('x-enumNames' in schema) {
enums = [...new Set(schema['x-enumNames'] as string[])].map((key: string, index) => {
return [key, schema.enum?.[index] as string]
})
const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
return [...new Set(schema[extensionKey] as string[])].map((key, index) => {
return [key, schema.enum?.[index] as string]
})
}
})

if (extensionEnums.length > 0) {
return extensionEnums[0]

Check warning on line 292 in packages/swagger-ts/src/TypeGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-ts/src/TypeGenerator.ts#L283-L292

Added lines #L283 - L292 were not covered by tests
}



Check warning on line 296 in packages/swagger-ts/src/TypeGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-ts/src/TypeGenerator.ts#L295-L296

Added lines #L295 - L296 were not covered by tests
this.extraNodes.push(
...factory.createEnumDeclaration({
name: transformers.camelCase(enumName),
Expand Down
23 changes: 15 additions & 8 deletions packages/swagger-zod/src/ZodGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,23 @@ export class ZodGenerator extends Generator<PluginOptions['resolvedOptions'], Co
}

if (schema.enum) {
if ('x-enumNames' in schema) {
return [
{
keyword: zodKeywords.enum,
args: [...new Set(schema['x-enumNames'] as string[])].map((value: string) => `\`${value}\``),
},
...baseItems,
]
const extensionEnums = ['x-enumNames', 'x-enum-varnames'].map(extensionKey => {
if (extensionKey in schema) {
return [
{
keyword: zodKeywords.enum,
args: [...new Set(schema[extensionKey] as string[])].map((value: string) => `\`${value}\``),
},
...baseItems,
]
}
})

if (extensionEnums.length > 0) {
return extensionEnums[0]

Check warning on line 265 in packages/swagger-zod/src/ZodGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-zod/src/ZodGenerator.ts#L252-L265

Added lines #L252 - L265 were not covered by tests
}


Check warning on line 268 in packages/swagger-zod/src/ZodGenerator.ts

View check run for this annotation

Codecov / codecov/patch

packages/swagger-zod/src/ZodGenerator.ts#L268

Added line #L268 was not covered by tests
if (schema.type === 'number' || schema.type === 'integer') {
// we cannot use z.enum when enum type is number/integer
return [
Expand Down

0 comments on commit e8e6e8c

Please sign in to comment.