From c647cb6960bb5724a44be6fb5e83913e5d6200e8 Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 7 Jun 2024 14:20:30 -0400 Subject: [PATCH 1/2] feat(openapi-generator): parse `examples` and `default` in apiSpec as the schema type DX-458 --- packages/openapi-generator/src/openapi.ts | 28 +++++++++++++++++-- .../openapi-generator/test/openapi.test.ts | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/openapi-generator/src/openapi.ts b/packages/openapi-generator/src/openapi.ts index ff8a8958..d4064a15 100644 --- a/packages/openapi-generator/src/openapi.ts +++ b/packages/openapi-generator/src/openapi.ts @@ -133,6 +133,30 @@ function schemaToOpenAPI( } }; + /** + * This function will return the field value parsed as the type of the schema. i.e. if the schema is a number, it will return the field as a JS number. + * + * @param schema A schema object + * @param field The target field to parse in that schema + * @returns the parsed value + */ + const parseField = (schema: Schema, field: string): any => { + const fieldValue = getTagName(schema, field); + if (!fieldValue) { + return; + } + + if (schema.type === 'number' || schema.type === 'integer') { + return Number(fieldValue); + } else if (schema.type === 'boolean') { + return fieldValue === 'true'; + } else if (schema.type === 'null') { + return null; + } else { + return fieldValue; + } + }; + function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject { const defaultValue = getTagName(schema, 'default'); const example = getTagName(schema, 'example'); @@ -157,10 +181,10 @@ function schemaToOpenAPI( const description = schema.comment?.description; const defaultOpenAPIObject = { - ...(defaultValue ? { default: defaultValue } : {}), + ...(defaultValue ? { default: parseField(schema, 'default') } : {}), ...(deprecated ? { deprecated: true } : {}), ...(description ? { description } : {}), - ...(example ? { example } : {}), + ...(example ? { example: parseField(schema, 'example') } : {}), ...(maxLength ? { maxLength: Number(maxLength) } : {}), ...(minLength ? { minLength: Number(minLength) } : {}), ...(pattern ? { pattern } : {}), diff --git a/packages/openapi-generator/test/openapi.test.ts b/packages/openapi-generator/test/openapi.test.ts index 1aba9e74..2dbb5a26 100644 --- a/packages/openapi-generator/test/openapi.test.ts +++ b/packages/openapi-generator/test/openapi.test.ts @@ -2119,7 +2119,7 @@ testCase('route with descriptions, patterns, and examples', ROUTE_WITH_DESCRIPTI foo: { type: 'number', description: 'foo description', - example: '12345', + example: 12345, pattern: '^[1-9][0-9]{4}$' }, child: { From ad98b739fdf9cb45c1d00fc13483be86f1e4aa3a Mon Sep 17 00:00:00 2001 From: Aryaman Dhingra Date: Fri, 7 Jun 2024 15:02:20 -0400 Subject: [PATCH 2/2] refactor: make parsing code slightly cleaner DX-458 --- packages/openapi-generator/src/openapi.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/openapi-generator/src/openapi.ts b/packages/openapi-generator/src/openapi.ts index d4064a15..8901d63f 100644 --- a/packages/openapi-generator/src/openapi.ts +++ b/packages/openapi-generator/src/openapi.ts @@ -137,15 +137,10 @@ function schemaToOpenAPI( * This function will return the field value parsed as the type of the schema. i.e. if the schema is a number, it will return the field as a JS number. * * @param schema A schema object - * @param field The target field to parse in that schema + * @param fieldValue The value to parse * @returns the parsed value */ - const parseField = (schema: Schema, field: string): any => { - const fieldValue = getTagName(schema, field); - if (!fieldValue) { - return; - } - + const parseField = (schema: Schema, fieldValue: string): any => { if (schema.type === 'number' || schema.type === 'integer') { return Number(fieldValue); } else if (schema.type === 'boolean') { @@ -181,10 +176,10 @@ function schemaToOpenAPI( const description = schema.comment?.description; const defaultOpenAPIObject = { - ...(defaultValue ? { default: parseField(schema, 'default') } : {}), + ...(defaultValue ? { default: parseField(schema, defaultValue) } : {}), ...(deprecated ? { deprecated: true } : {}), ...(description ? { description } : {}), - ...(example ? { example: parseField(schema, 'example') } : {}), + ...(example ? { example: parseField(schema, example) } : {}), ...(maxLength ? { maxLength: Number(maxLength) } : {}), ...(minLength ? { minLength: Number(minLength) } : {}), ...(pattern ? { pattern } : {}),