Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show index information in openapi specs #10674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/openapi-v3/src/json-to-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -728,6 +748,7 @@ describe('build-schema', () => {
@property({
type: 'string',
required: true,
index: {unique: true},
jsonSchema: {
format: 'email',
maxLength: 50,
Expand Down
37 changes: 37 additions & 0 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,43 @@ export function modelToJsonSchema<T extends object>(
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(
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/validation/ajv-factory.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class AjvFactoryProvider implements Provider<AjvFactory> {
const ajvInst = new AjvCtor(ajvOptions);
ajvInst.addKeyword('components');
ajvInst.addKeyword('x-typescript-type');
ajvInst.addKeyword('x-index-info');

ajvKeywords(ajvInst, validationOptions.ajvKeywords);

Expand Down
Loading