From 75d7aceff8b9a138cf85d363baf4eb90bba41b88 Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sat, 19 Oct 2024 23:55:42 +0500 Subject: [PATCH] feat: show index information in openapi specs Signed-off-by: Muhammad Aaqil --- packages/openapi-v3/src/json-to-schema.ts | 5 ++++ .../integration/build-schema.integration.ts | 1 + .../src/build-schema.ts | 24 +++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/openapi-v3/src/json-to-schema.ts b/packages/openapi-v3/src/json-to-schema.ts index 3fc49bfe2d17..2aac00b7589c 100644 --- a/packages/openapi-v3/src/json-to-schema.ts +++ b/packages/openapi-v3/src/json-to-schema.ts @@ -143,6 +143,11 @@ export function jsonToSchemaObject( if (matched) { result['x-typescript-type'] = matched[1]; } + + const indexInfoMatched = result.description?.match(/\{"indexInfo".*$/s); + if (indexInfoMatched) { + result['x-index-info'] = indexInfoMatched[1]; + } return result; } diff --git a/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts b/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts index af512eb78d83..1434d919aba3 100644 --- a/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts +++ b/packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts @@ -379,6 +379,7 @@ describe('build-schema', () => { format: 'email', minLength: 5, maxLength: 50, + index: {unique: true}, transform: ['toLowerCase'], }, }) diff --git a/packages/repository-json-schema/src/build-schema.ts b/packages/repository-json-schema/src/build-schema.ts index 484fc420cc7f..3e8eac3f40e8 100644 --- a/packages/repository-json-schema/src/build-schema.ts +++ b/packages/repository-json-schema/src/build-schema.ts @@ -312,7 +312,6 @@ export function metaToJsonProperty(meta: PropertyDefinition): JsonSchema { if (meta.jsonSchema) { Object.assign(propDef, meta.jsonSchema); } - return result; } @@ -500,7 +499,6 @@ export function modelToJsonSchema( // populating "properties" key result.properties[p] = metaToJsonProperty(metaProperty); - // handling 'required' metadata const optional = options.optional.includes(p as keyof T); @@ -611,5 +609,27 @@ export function modelToJsonSchema( if (meta.jsonSchema) { Object.assign(result, meta.jsonSchema); } + const indexInfo = meta.index; + if (result.description) { + if (result.description.includes('indexInfo')) { + const indexInfoMatched = result.description.match(/\{"indexInfo".*$/s); + if (indexInfoMatched) { + const {indexInfo: existingIndexInfo} = JSON.parse(indexInfoMatched[0]); + existingIndexInfo[Object.keys(indexInfo)[0]] = { + ...indexInfo, + }; + result.description = result.description.replace( + /\{"indexInfo".*$/s, + '', + ); + result.description = + result.description + + `, ${JSON.stringify({indexInfo: existingIndexInfo})}`; + } + } else { + result.description = + result.description + `, ${JSON.stringify({indexInfo})}`; + } + } return result; }