Skip to content

Commit

Permalink
feat: add relation type in openapi specs
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Aug 16, 2024
1 parent 3c4c5b8 commit 4d13c0a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -70,6 +72,7 @@ describe('jsonToSchemaObject', () => {
'x-foreign-keys': {
categoryId: 'Category',
},
'x-relation-type': 'hasMany',
},
},
'x-typescript-type': 'ProductWithRelations',
Expand Down
7 changes: 3 additions & 4 deletions packages/openapi-v3/src/json-to-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand All @@ -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);
Expand Down Expand Up @@ -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'},
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ export function modelToJsonSchema<T extends object>(
$ref?: string;
items?: {$ref: string};
foreignKeys?: {[x: string]: string};
relationType?: string;
};

const foreignKey: {[x: string]: string} = {};
Expand Down Expand Up @@ -681,6 +682,7 @@ export function modelToJsonSchema<T extends object>(
};
}
relationships[relMeta.name].foreignKeys = foreignKey;
relationships[relMeta.name].relationType = relMeta.type;
if (result.description) {
if (result.description.includes('relationships')) {
const relationMatched =
Expand Down

0 comments on commit 4d13c0a

Please sign in to comment.