Skip to content

Commit d285215

Browse files
committed
feat: show index information in openapi specs
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 5ef9137 commit d285215

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

+4
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

+21
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

+37
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,43 @@ export function modelToJsonSchema<T extends object>(
486486
continue;
487487
}
488488

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

0 commit comments

Comments
 (0)