From 4d13c0a8715484c326d9ec56c11046c909f88bfc Mon Sep 17 00:00:00 2001 From: Muhammad Aaqil Date: Sat, 17 Aug 2024 00:11:58 +0500 Subject: [PATCH] feat: add relation type in openapi specs Signed-off-by: Muhammad Aaqil --- .../openapi-v3/src/__tests__/unit/json-to-schema.unit.ts | 5 ++++- packages/openapi-v3/src/json-to-schema.ts | 7 +++---- .../src/__tests__/integration/build-schema.integration.ts | 8 ++++---- packages/repository-json-schema/src/build-schema.ts | 2 ++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/openapi-v3/src/__tests__/unit/json-to-schema.unit.ts b/packages/openapi-v3/src/__tests__/unit/json-to-schema.unit.ts index c16cf2192de5..e5cad1a74c34 100644 --- a/packages/openapi-v3/src/__tests__/unit/json-to-schema.unit.ts +++ b/packages/openapi-v3/src/__tests__/unit/json-to-schema.unit.ts @@ -44,7 +44,7 @@ describe('jsonToSchemaObject', () => { description: '(tsType: ProductWithRelations, ' + 'schemaOptions: { includeRelations: true }), ' + - '{"relationships":{"products":{"description":"Category have many Product.","type":"array","items":{"$ref":"#/definitions/ProductWithRelations"},"foreignKeys":{"categoryId":"Category"}}}}', + '{"relationships":{"products":{"description":"Category have many Product.","type":"array","items":{"$ref":"#/definitions/ProductWithRelations"},"relationType":"hasMany","foreignKeys":{"categoryId":"Category"}}}}', }; type Relationship = { description?: string; @@ -53,6 +53,8 @@ describe('jsonToSchemaObject', () => { items?: {$ref: string}; // eslint-disable-next-line @typescript-eslint/naming-convention 'x-foreign-keys'?: {[x: string]: string}; + // eslint-disable-next-line @typescript-eslint/naming-convention + 'x-relation-type'?: string; }; const expectedDescriptionWithRelation: SchemaObject & { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -70,6 +72,7 @@ describe('jsonToSchemaObject', () => { 'x-foreign-keys': { categoryId: 'Category', }, + 'x-relation-type': 'hasMany', }, }, 'x-typescript-type': 'ProductWithRelations', diff --git a/packages/openapi-v3/src/json-to-schema.ts b/packages/openapi-v3/src/json-to-schema.ts index efad3e001175..396f2b7d0d7a 100644 --- a/packages/openapi-v3/src/json-to-schema.ts +++ b/packages/openapi-v3/src/json-to-schema.ts @@ -143,10 +143,9 @@ export function jsonToSchemaObject( if (result.description) { const relationMatched = result.description.match(/\{"relationships".*$/s); if (relationMatched) { - const stringifiedRelation = relationMatched[0].replace( - /foreignKey/g, - 'x-foreign-key', - ); + const stringifiedRelation = relationMatched[0] + .replace(/foreignKey/g, 'x-foreign-key') + .replace(/relationType/g, 'x-relation-type'); if (stringifiedRelation) { result['x-relationships'] = JSON.parse(stringifiedRelation)['relationships']; 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 6948da4a0fbd..b85420a8bcd8 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 @@ -1127,7 +1127,7 @@ describe('build-schema', () => { description: `(tsType: ProductWithRelations, ` + `schemaOptions: { includeRelations: true }), ` + - `{\"relationships\":{\"category\":{\"description\":\"Product belongs to Category.\",\"type\":\"object\",\"$ref\":\"#/definitions/CategoryWithRelations\",\"foreignKeys\":{\"categoryId\":\"Category\"}}}}`, + `{\"relationships\":{\"category\":{\"description\":\"Product belongs to Category.\",\"type\":\"object\",\"$ref\":\"#/definitions/CategoryWithRelations\",\"foreignKeys\":{\"categoryId\":\"Category\"},\"relationType\":\"belongsTo\"}}}`, properties: { id: {type: 'number'}, categoryId: {type: 'number'}, @@ -1153,7 +1153,7 @@ describe('build-schema', () => { description: `(tsType: CategoryWithRelations, ` + `schemaOptions: { includeRelations: true }), ` + - `{\"relationships\":{\"products\":{\"description\":\"Category have many Product.\",\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/ProductWithRelations\"},\"foreignKeys\":{\"categoryId\":\"Category\"}}}}`, + `{\"relationships\":{\"products\":{\"description\":\"Category have many Product.\",\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/ProductWithRelations\"},\"foreignKeys\":{\"categoryId\":\"Category\"},\"relationType\":\"hasMany\"}}}`, }; const jsonSchema = getJsonSchema(Category, {includeRelations: true}); expect(jsonSchema).to.deepEqual(expectedSchema); @@ -1182,7 +1182,7 @@ describe('build-schema', () => { description: `(tsType: ProductWithRelations, ` + `schemaOptions: { includeRelations: true }), ` + - `{\"relationships\":{\"category\":{\"description\":\"Product belongs to CategoryWithoutProp.\",\"type\":\"object\",\"$ref\":\"#/definitions/CategoryWithoutPropWithRelations\",\"foreignKeys\":{\"categoryId\":\"CategoryWithoutProp\"}}}}`, + `{\"relationships\":{\"category\":{\"description\":\"Product belongs to CategoryWithoutProp.\",\"type\":\"object\",\"$ref\":\"#/definitions/CategoryWithoutPropWithRelations\",\"foreignKeys\":{\"categoryId\":\"CategoryWithoutProp\"},\"relationType\":\"belongsTo\"}}}`, properties: { id: {type: 'number'}, categoryId: {type: 'number'}, @@ -1205,7 +1205,7 @@ describe('build-schema', () => { description: `(tsType: CategoryWithoutPropWithRelations, ` + `schemaOptions: { includeRelations: true }), ` + - `{\"relationships\":{\"products\":{\"description\":\"CategoryWithoutProp have many Product.\",\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/ProductWithRelations\"},\"foreignKeys\":{\"categorywithoutpropId\":\"CategoryWithoutProp\"}}}}`, + `{\"relationships\":{\"products\":{\"description\":\"CategoryWithoutProp have many Product.\",\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/ProductWithRelations\"},\"foreignKeys\":{\"categorywithoutpropId\":\"CategoryWithoutProp\"},\"relationType\":\"\hasMany"}}}`, }; // To check for case when there are no other properties than relational diff --git a/packages/repository-json-schema/src/build-schema.ts b/packages/repository-json-schema/src/build-schema.ts index 38b4321232cd..7a7f22e86cc3 100644 --- a/packages/repository-json-schema/src/build-schema.ts +++ b/packages/repository-json-schema/src/build-schema.ts @@ -583,6 +583,7 @@ export function modelToJsonSchema( $ref?: string; items?: {$ref: string}; foreignKeys?: {[x: string]: string}; + relationType?: string; }; const foreignKey: {[x: string]: string} = {}; @@ -681,6 +682,7 @@ export function modelToJsonSchema( }; } relationships[relMeta.name].foreignKeys = foreignKey; + relationships[relMeta.name].relationType = relMeta.type; if (result.description) { if (result.description.includes('relationships')) { const relationMatched =