From d28521566752f366ad8f7477ec877771fbdebd92 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 | 4 ++ .../integration/build-schema.integration.ts | 21 +++++++++++ .../src/build-schema.ts | 37 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/packages/openapi-v3/src/json-to-schema.ts b/packages/openapi-v3/src/json-to-schema.ts index 3fc49bfe2d17..d4eb6da9dd7f 100644 --- a/packages/openapi-v3/src/json-to-schema.ts +++ b/packages/openapi-v3/src/json-to-schema.ts @@ -143,6 +143,10 @@ 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..1fbac51d0107 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 @@ -485,6 +485,26 @@ describe('build-schema', () => { }, }); }); + it('adds index info in description', () => { + @model() + class TestModel { + @property({ + type: 'string', + required: true, + index: {unique: true}, + jsonSchema: { + format: 'email', + maxLength: 50, + minLength: 5, + }, + }) + email: string; + } + const jsonSchema = modelToJsonSchema(TestModel); + expect(jsonSchema.description).to.eql( + '{"indexInfo":{"email":{"unique":true}}}', + ); + }); context('with custom type properties', () => { it('properly converts undecorated custom type properties', () => { @@ -728,6 +748,7 @@ describe('build-schema', () => { @property({ type: 'string', required: true, + index: {unique: true}, jsonSchema: { format: 'email', maxLength: 50, diff --git a/packages/repository-json-schema/src/build-schema.ts b/packages/repository-json-schema/src/build-schema.ts index 484fc420cc7f..362c201437b4 100644 --- a/packages/repository-json-schema/src/build-schema.ts +++ b/packages/repository-json-schema/src/build-schema.ts @@ -486,6 +486,43 @@ export function modelToJsonSchema( continue; } + const index = meta.properties[p].index; + let indexInfo: {} = {}; + if (index && Object.keys(index).length) { + indexInfo = {[p]: index}; + } + if (indexInfo && Object.keys(indexInfo).length) { + if (result.description === undefined) 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, + '', + ); + if (result.description) { + result.description = + result.description + + `, ${JSON.stringify({indexInfo: existingIndexInfo})}`; + } else { + result.description = `${JSON.stringify({indexInfo: existingIndexInfo})}`; + } + } + } else { + if (result.description) { + result.description = + result.description + `, ${JSON.stringify({indexInfo})}`; + } else { + result.description = `${JSON.stringify({indexInfo})}`; + } + } + } if (meta.properties[p].type == null) { // Circular import of model classes can lead to this situation throw new Error(