From 41963430853e8dc6eed144f8632f096a9123507c Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Tue, 4 Oct 2022 16:58:26 +0200 Subject: [PATCH] fix: use object instead of array for enum --- src/Field.ts | 2 +- src/openapi3/handleJson.test.ts | 25 ++++++++++++++++++++----- src/openapi3/handleJson.ts | 13 +++++++++++-- src/swagger/handleJson.test.ts | 9 +++++++-- src/swagger/handleJson.ts | 11 ++++++++++- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Field.ts b/src/Field.ts index 6f0eadb..74419ed 100644 --- a/src/Field.ts +++ b/src/Field.ts @@ -36,7 +36,7 @@ export interface FieldOptions range?: string; type?: FieldType; arrayType?: FieldType; - enum?: string[] | number[]; + enum?: { [key: string | number]: string | number }; reference?: string | Resource; embedded?: Resource; required?: boolean; diff --git a/src/openapi3/handleJson.test.ts b/src/openapi3/handleJson.test.ts index c82a3de..772268e 100644 --- a/src/openapi3/handleJson.test.ts +++ b/src/openapi3/handleJson.test.ts @@ -516,7 +516,7 @@ const openApi3Definition: OpenAPIV3.Document = { bookFormat: { type: "string", description: "The publication format of the book.", - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: ["AUDIOBOOK_FORMAT", "E_BOOK", "PAPERBACK", "HARDCOVER"], }, publicationDate: { type: "string", @@ -573,7 +573,7 @@ const openApi3Definition: OpenAPIV3.Document = { bookFormat: { type: "string", description: "The publication format of the book.", - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: ["AUDIOBOOK_FORMAT", "E_BOOK", "PAPERBACK", "HARDCOVER"], }, publicationDate: { type: "string", @@ -705,7 +705,12 @@ const parsed = [ range: null, type: "string", arrayType: null, - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: { + "Audiobook format": "AUDIOBOOK_FORMAT", + "E book": "E_BOOK", + Paperback: "PAPERBACK", + Hardcover: "HARDCOVER", + }, reference: null, embedded: null, nullable: false, @@ -829,7 +834,12 @@ const parsed = [ range: null, type: "string", arrayType: null, - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: { + "Audiobook format": "AUDIOBOOK_FORMAT", + "E book": "E_BOOK", + Paperback: "PAPERBACK", + Hardcover: "HARDCOVER", + }, reference: null, embedded: null, nullable: false, @@ -927,7 +937,12 @@ const parsed = [ range: null, type: "string", arrayType: null, - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: { + "Audiobook format": "AUDIOBOOK_FORMAT", + "E book": "E_BOOK", + Paperback: "PAPERBACK", + Hardcover: "HARDCOVER", + }, reference: null, embedded: null, nullable: false, diff --git a/src/openapi3/handleJson.ts b/src/openapi3/handleJson.ts index 60222e3..27d5a75 100644 --- a/src/openapi3/handleJson.ts +++ b/src/openapi3/handleJson.ts @@ -74,8 +74,17 @@ const buildResourceFromSchema = ( (property.items as OpenAPIV3.SchemaObject).format ) : null, - // Object.values is used because the array is annotated: it contains the __meta symbol used by jsonref. - enum: property.enum ? Object.values(property.enum) : null, + enum: property.enum + ? Object.fromEntries( + // Object.values is used because the array is annotated: it contains the __meta symbol used by jsonref. + Object.values(property.enum).map((enumValue) => [ + typeof enumValue === "string" + ? inflection.humanize(enumValue) + : enumValue, + enumValue, + ]) + ) + : null, reference: null, embedded: null, nullable: property.nullable || false, diff --git a/src/swagger/handleJson.test.ts b/src/swagger/handleJson.test.ts index 9fbb019..e9475f6 100644 --- a/src/swagger/handleJson.test.ts +++ b/src/swagger/handleJson.test.ts @@ -329,7 +329,7 @@ const swaggerApiDefinition: OpenAPIV2.Document = { bookFormat: { type: "string", description: "The publication format of the book.", - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: ["AUDIOBOOK_FORMAT", "E_BOOK", "PAPERBACK", "HARDCOVER"], }, publicationDate: { description: @@ -445,7 +445,12 @@ const parsed = [ type: "string", reference: null, embedded: null, - enum: ["AudiobookFormat", "EBook", "Paperback", "Hardcover"], + enum: { + "Audiobook format": "AUDIOBOOK_FORMAT", + "E book": "E_BOOK", + Paperback: "PAPERBACK", + Hardcover: "HARDCOVER", + }, required: true, description: "The publication format of the book.", }, diff --git a/src/swagger/handleJson.ts b/src/swagger/handleJson.ts index e59a220..e83e451 100644 --- a/src/swagger/handleJson.ts +++ b/src/swagger/handleJson.ts @@ -60,7 +60,16 @@ export default function ( get(property, "type", "") as string, get(property, "format", "") as string ), - enum: property.enum ?? null, + enum: property.enum + ? Object.fromEntries( + property.enum.map((enumValue: string | number) => [ + typeof enumValue === "string" + ? inflection.humanize(enumValue) + : enumValue, + enumValue, + ]) + ) + : null, reference: null, embedded: null, required: !!requiredFields.find((value) => value === fieldName),