Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 20 additions & 5 deletions src/openapi3/handleJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions src/openapi3/handleJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | number>(property.enum).map((enumValue) => [
typeof enumValue === "string"
? inflection.humanize(enumValue)
: enumValue,
enumValue,
])
)
: null,
reference: null,
embedded: null,
nullable: property.nullable || false,
Expand Down
9 changes: 7 additions & 2 deletions src/swagger/handleJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.",
},
Expand Down
11 changes: 10 additions & 1 deletion src/swagger/handleJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down