-
Notifications
You must be signed in to change notification settings - Fork 2
[Feature Request] Support omitting fields rather than entire models #78
Description
Hi there! First of all, thank you for this generator. This is super helpful for my org, as hand-typing OpenAPI schema for the hundreds of models and thousands of fields we have would be impossible.
That being said, we have models that we would like to omit certain fields on, but not the entire model. For example, let's say we have an Account model may have hashed passwords in it. We want users of our API to be able to access this model (Say, for updating things like name, description, or other fields one may update on an Account), but we don't want them to access or even know about the password field (Or any sensitive fields). However, this field is automatically added to the OpenAPI schema when generated.
Since this library already generates descriptions for model properties based on the field.documentation property, would it be possible to have a field be omitted if a specific phrase or comment existed in the documentation for it? For example, maybe if the comment contains @openapi-omit the model would not be added to the generation chain? Here's an example snippet of an updated generatePropertiesFromModel function that would support this (Not tested, only theoretical):
export function generatePropertiesFromModel(
model: GeneratorOptions['dmmf']['datamodel']['models'][0],
allModels: GeneratorOptions['dmmf']['datamodel']['models'],
enums: GeneratorOptions['dmmf']['datamodel']['enums'],
): Record<string, SchemaObject | ReferenceObject> {
const properties: Record<string, SchemaObject | ReferenceObject> = {};
for (const field of model.fields) {
// Updated function, omits a field if the documentation string contains a specified value. Could also be rewritten to be "startsWith" or something
if(field.documentation?.includes("@openapi-omit")) continue;
// Existing code, truncated for brevity
let property: SchemaObject | ReferenceObject;
// ...
properties[field.name] = property;
}
return properties;
}This would be hugely beneficial for teams needing models to be generated but certain fields hidden within those models, without needing to manually edit the model afterwards. Thank you again for your work on this!!