From 25eeb9d3ff2b95f444b57bf127fccfdadeb26fce Mon Sep 17 00:00:00 2001 From: Ansh Chaturvedi Date: Wed, 5 Jun 2024 17:07:48 -0400 Subject: [PATCH] feat: support `@default` for strings Ticket: DX-447 --- packages/openapi-generator/src/openapi.ts | 2 ++ packages/openapi-generator/test/openapi.test.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/openapi-generator/src/openapi.ts b/packages/openapi-generator/src/openapi.ts index db433e7f..c386ec1b 100644 --- a/packages/openapi-generator/src/openapi.ts +++ b/packages/openapi-generator/src/openapi.ts @@ -134,6 +134,7 @@ function schemaToOpenAPI( }; function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject { + const defaultValue = getTagName(schema, 'default'); const description = schema.comment?.description; const example = getTagName(schema, 'example'); const maxLength = getTagName(schema, 'maxLength'); @@ -141,6 +142,7 @@ function schemaToOpenAPI( const pattern = getTagName(schema, 'pattern'); const defaultOpenAPIObject = { + ...(defaultValue ? { default: defaultValue } : {}), ...(description ? { description } : {}), ...(example ? { example } : {}), ...(maxLength ? { maxLength: Number(maxLength) } : {}), diff --git a/packages/openapi-generator/test/openapi.test.ts b/packages/openapi-generator/test/openapi.test.ts index 8d337f76..0da2be6e 100644 --- a/packages/openapi-generator/test/openapi.test.ts +++ b/packages/openapi-generator/test/openapi.test.ts @@ -2327,7 +2327,7 @@ testCase('route with descriptions for references', ROUTE_WITH_DESCRIPTIONS_FOR_R } }); -const ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS = ` +const ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS_AND_DEFAULT = ` import * as t from 'io-ts'; import * as h from '@api-ts/io-ts-http'; @@ -2349,7 +2349,8 @@ export const route = h.httpRoute({ * This is a foo description. * @minLength 5 * @maxLength 10 - * @example "BitgoInc" + * @example "SomeInc" + * @default "BitgoInc" */ foo: t.string() }, @@ -2362,7 +2363,7 @@ export const route = h.httpRoute({ }); `; -testCase('route with min and max values for strings', ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS, { +testCase('route with min and max values for strings and default value', ROUTE_WITH_MIN_AND_MAX_VALUES_FOR_STRINGS_AND_DEFAULT, { openapi: '3.0.3', info: { title: 'Test', @@ -2398,7 +2399,8 @@ testCase('route with min and max values for strings', ROUTE_WITH_MIN_AND_MAX_VAL foo: { type: 'string', description: 'This is a foo description.', - example: 'BitgoInc', + example: 'SomeInc', + default: 'BitgoInc', minLength: 5, maxLength: 10 }