From f77dcf83d4964d6ba1c1c5291448fe839a35d436 Mon Sep 17 00:00:00 2001 From: Drew Kimberly Date: Thu, 5 Dec 2024 15:26:48 -0600 Subject: [PATCH 1/2] test(oas): add nullable enums to OAS test fixture [ISSUE-271] --- .../__snapshots__/operation.test.ts.snap | 92 +++++++++++++++++++ .../__tests__/fixtures/oas3-kitchen-sink.json | 10 ++ 2 files changed, 102 insertions(+) diff --git a/src/oas/__tests__/__snapshots__/operation.test.ts.snap b/src/oas/__tests__/__snapshots__/operation.test.ts.snap index 24458fd..f322302 100644 --- a/src/oas/__tests__/__snapshots__/operation.test.ts.snap +++ b/src/oas/__tests__/__snapshots__/operation.test.ts.snap @@ -787,6 +787,29 @@ Array [ ], "type": "string", }, + "nullableEnumExplicit": Object { + "enum": Array [ + null, + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, + "nullableEnumImplicit": Object { + "enum": Array [ + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, "photoUrls": Object { "items": Object { "type": "string", @@ -849,6 +872,29 @@ Array [ ], "type": "string", }, + "nullableEnumExplicit": Object { + "enum": Array [ + null, + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, + "nullableEnumImplicit": Object { + "enum": Array [ + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, "photoUrls": Object { "items": Object { "type": "string", @@ -1143,6 +1189,29 @@ Array [ ], "type": "string", }, + "nullableEnumExplicit": Object { + "enum": Array [ + null, + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, + "nullableEnumImplicit": Object { + "enum": Array [ + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, "photoUrls": Object { "items": Object { "type": "string", @@ -1205,6 +1274,29 @@ Array [ ], "type": "string", }, + "nullableEnumExplicit": Object { + "enum": Array [ + null, + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, + "nullableEnumImplicit": Object { + "enum": Array [ + "foo", + "bar", + null, + ], + "type": Array [ + "string", + "null", + ], + }, "photoUrls": Object { "items": Object { "type": "string", diff --git a/src/oas/__tests__/fixtures/oas3-kitchen-sink.json b/src/oas/__tests__/fixtures/oas3-kitchen-sink.json index 26a09ca..c9d9636 100644 --- a/src/oas/__tests__/fixtures/oas3-kitchen-sink.json +++ b/src/oas/__tests__/fixtures/oas3-kitchen-sink.json @@ -312,6 +312,16 @@ "type": "string", "description": "pet status in the store", "enum": ["available", "pending", "sold"] + }, + "nullableEnumImplicit": { + "type": "string", + "nullable": true, + "enum": ["foo", "bar"] + }, + "nullableEnumExplicit": { + "type": "string", + "nullable": true, + "enum": [null, "foo", "bar"] } }, "required": ["name", "photoUrls"] From 7452a4b394ebe0403dadeed8edee3b60a5763df7 Mon Sep 17 00:00:00 2001 From: Drew Kimberly Date: Thu, 5 Dec 2024 15:30:04 -0600 Subject: [PATCH 2/2] fix(oas): prevent duplicate null values in enum array [ISSUE-271] --- src/oas/__tests__/__snapshots__/operation.test.ts.snap | 4 ---- src/oas/transformers/schema/keywords/nullable.ts | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/oas/__tests__/__snapshots__/operation.test.ts.snap b/src/oas/__tests__/__snapshots__/operation.test.ts.snap index f322302..97dfad5 100644 --- a/src/oas/__tests__/__snapshots__/operation.test.ts.snap +++ b/src/oas/__tests__/__snapshots__/operation.test.ts.snap @@ -792,7 +792,6 @@ Array [ null, "foo", "bar", - null, ], "type": Array [ "string", @@ -877,7 +876,6 @@ Array [ null, "foo", "bar", - null, ], "type": Array [ "string", @@ -1194,7 +1192,6 @@ Array [ null, "foo", "bar", - null, ], "type": Array [ "string", @@ -1279,7 +1276,6 @@ Array [ null, "foo", "bar", - null, ], "type": Array [ "string", diff --git a/src/oas/transformers/schema/keywords/nullable.ts b/src/oas/transformers/schema/keywords/nullable.ts index 72de6ee..74c4766 100644 --- a/src/oas/transformers/schema/keywords/nullable.ts +++ b/src/oas/transformers/schema/keywords/nullable.ts @@ -26,7 +26,7 @@ const createNullableConverter = (keyword: 'x-nullable' | 'nullable'): Converter if (schema[keyword] === true) { schema.type = [schema.type, 'null']; - if (Array.isArray(schema.enum)) { + if (Array.isArray(schema.enum) && !schema.enum.includes(null)) { schema.enum = [...schema.enum, null]; } } else {