Skip to content

Commit a5caa61

Browse files
aaqilnizdhmlau
authored andcommitted
feat: show index information in openapi specs
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent 41ed17b commit a5caa61

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

packages/openapi-v3/src/json-to-schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ export function jsonToSchemaObject(
143143
if (matched) {
144144
result['x-typescript-type'] = matched[1];
145145
}
146+
const indexInfoMatched = result.description?.match(/\{"indexInfo".*$/s);
147+
if (indexInfoMatched) {
148+
result['x-index-info'] = indexInfoMatched[1];
149+
}
146150
return result;
147151
}
148152

packages/repository-json-schema/src/__tests__/integration/build-schema.integration.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,26 @@ describe('build-schema', () => {
485485
},
486486
});
487487
});
488+
it('adds index info in description', () => {
489+
@model()
490+
class TestModel {
491+
@property({
492+
type: 'string',
493+
required: true,
494+
index: {unique: true},
495+
jsonSchema: {
496+
format: 'email',
497+
maxLength: 50,
498+
minLength: 5,
499+
},
500+
})
501+
email: string;
502+
}
503+
const jsonSchema = modelToJsonSchema(TestModel);
504+
expect(jsonSchema.description).to.eql(
505+
'{"indexInfo":{"email":{"unique":true}}}',
506+
);
507+
});
488508

489509
context('with custom type properties', () => {
490510
it('properly converts undecorated custom type properties', () => {
@@ -728,6 +748,7 @@ describe('build-schema', () => {
728748
@property({
729749
type: 'string',
730750
required: true,
751+
index: {unique: true},
731752
jsonSchema: {
732753
format: 'email',
733754
maxLength: 50,

packages/repository-json-schema/src/build-schema.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,43 @@ export function modelToJsonSchema<T extends object>(
483483
continue;
484484
}
485485

486+
const index = meta.properties[p].index;
487+
let indexInfo: {} = {};
488+
if (index && Object.keys(index).length) {
489+
indexInfo = {[p]: index};
490+
}
491+
if (indexInfo && Object.keys(indexInfo).length) {
492+
if (result.description === undefined) result.description = '';
493+
if (result.description.includes('indexInfo')) {
494+
const indexInfoMatched = result.description.match(/\{"indexInfo".*$/s);
495+
if (indexInfoMatched) {
496+
const {indexInfo: existingIndexInfo} = JSON.parse(
497+
indexInfoMatched[0],
498+
);
499+
existingIndexInfo[Object.keys(indexInfo)[0]] = {
500+
...indexInfo,
501+
};
502+
result.description = result.description.replace(
503+
/\{"indexInfo".*$/s,
504+
'',
505+
);
506+
if (result.description) {
507+
result.description =
508+
result.description +
509+
`, ${JSON.stringify({indexInfo: existingIndexInfo})}`;
510+
} else {
511+
result.description = `${JSON.stringify({indexInfo: existingIndexInfo})}`;
512+
}
513+
}
514+
} else {
515+
if (result.description) {
516+
result.description =
517+
result.description + `, ${JSON.stringify({indexInfo})}`;
518+
} else {
519+
result.description = `${JSON.stringify({indexInfo})}`;
520+
}
521+
}
522+
}
486523
if (meta.properties[p].type == null) {
487524
// Circular import of model classes can lead to this situation
488525
throw new Error(

packages/rest/src/validation/ajv-factory.provider.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class AjvFactoryProvider implements Provider<AjvFactory> {
6363
const ajvInst = new AjvCtor(ajvOptions);
6464
ajvInst.addKeyword('components');
6565
ajvInst.addKeyword('x-typescript-type');
66+
ajvInst.addKeyword('x-index-info');
6667

6768
ajvKeywords(ajvInst, validationOptions.ajvKeywords);
6869

0 commit comments

Comments
 (0)