From 179bf7b445692e7ead1e617760f5300c5027646a Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Thu, 18 Jul 2024 19:52:59 -0400 Subject: [PATCH] feat: support `min/max -Item`'s for array types See example in ticket which inspired this PR. Ticket: DX-602 --- packages/openapi-generator/src/openapi.ts | 4 +++- packages/openapi-generator/test/openapi.test.ts | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/openapi-generator/src/openapi.ts b/packages/openapi-generator/src/openapi.ts index 04efcebe..16285ce2 100644 --- a/packages/openapi-generator/src/openapi.ts +++ b/packages/openapi-generator/src/openapi.ts @@ -55,10 +55,12 @@ function schemaToOpenAPI( return undefined; } - const { arrayExample, ...rest } = defaultOpenAPIObject; + const { arrayExample, minItems, maxItems, ...rest } = defaultOpenAPIObject; return { type: 'array', + ...(minItems ? { minItems } : {}), + ...(maxItems ? { maxItems } : {}), ...(arrayExample ? { example: JSON.parse(arrayExample) } : {}), // Add example to array if it exists items: { ...innerSchema, ...rest }, }; diff --git a/packages/openapi-generator/test/openapi.test.ts b/packages/openapi-generator/test/openapi.test.ts index 5ef9d28d..f9026652 100644 --- a/packages/openapi-generator/test/openapi.test.ts +++ b/packages/openapi-generator/test/openapi.test.ts @@ -3588,6 +3588,11 @@ export const route = h.httpRoute({ * @arrayExample ["btc", "eth"] */ array2: t.array(t.string), + /** + * @minItems 1 + * @maxItems 5 + */ + array3: t.array(t.number), objectWithArray: t.type({ /** * @arrayExample ["btc", "eth"] @@ -3632,6 +3637,14 @@ testCase("route with array examples", ROUTE_WITH_ARRAY_EXAMPLE, { example: '"btc"' }, }, + array3: { + items: { + type: 'number' + }, + maxItems: 5, + minItems: 1, + type: 'array' + }, objectWithArray: { properties: { nestedArray: { @@ -3651,7 +3664,7 @@ testCase("route with array examples", ROUTE_WITH_ARRAY_EXAMPLE, { type: 'object' }, }, - required: [ 'array1', 'array2', 'objectWithArray' ], + required: [ 'array1', 'array2', 'array3', 'objectWithArray' ], }, } }