Skip to content

Commit

Permalink
feat: show index information 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 Oct 19, 2024
1 parent 5ef9137 commit 75d7ace
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 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,11 @@ 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 @@ -379,6 +379,7 @@ describe('build-schema', () => {
format: 'email',
minLength: 5,
maxLength: 50,
index: {unique: true},
transform: ['toLowerCase'],
},
})
Expand Down
24 changes: 22 additions & 2 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ export function metaToJsonProperty(meta: PropertyDefinition): JsonSchema {
if (meta.jsonSchema) {
Object.assign(propDef, meta.jsonSchema);
}

return result;
}

Expand Down Expand Up @@ -500,7 +499,6 @@ export function modelToJsonSchema<T extends object>(

// populating "properties" key
result.properties[p] = metaToJsonProperty(metaProperty);

// handling 'required' metadata
const optional = options.optional.includes(p as keyof T);

Expand Down Expand Up @@ -611,5 +609,27 @@ export function modelToJsonSchema<T extends object>(
if (meta.jsonSchema) {
Object.assign(result, meta.jsonSchema);
}
const indexInfo = meta.index;
if (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,
'',
);
result.description =
result.description +
`, ${JSON.stringify({indexInfo: existingIndexInfo})}`;
}
} else {
result.description =
result.description + `, ${JSON.stringify({indexInfo})}`;
}
}
return result;
}

0 comments on commit 75d7ace

Please sign in to comment.