OpenAPI shema definition for models should exclude hidden properties by default
Hidden properties are automatically removed from the response data (model's toJSON() excludes the hidden properties), so it seems that the schema definition should also exclude these fields by default. Currently, one has to manually add these fields to the "exclude" option in getModelSchemaRef()
Current behavior: hidden properties are removed from response data but openapi schema definition lists hidden properties in the schema.
Expected behavior: hidden properties are removed from response data and openapi schema definition does not list hidden properties.
@ih84ds Thank you for the suggestion, it sounds reasonable to me. Take the Todo app as an example:
Now the response schema of GET still uses the original model schema Todo
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Todo"
}
}
}
the Todo schema in reference:
{
"Todo": {
"title": "Todo",
"properties": {
"id": {
"type": "number"
},
"title": {
"type": "string"
},
"desc": {
"type": "string"
},
// The new added secret property is not hidden
"secretProp": {
"type": "string"
}
},
"required": [
"title"
],
"additionalProperties": false
}
}
While the expected schema should be
{
"Todo": {
"title": "Todo",
"properties": {
"id": {
"type": "number"
},
"title": {
"type": "string"
},
"desc": {
"type": "string"
}
},
"required": [
"title"
],
"additionalProperties": false
}
}
I can think of two approaches:
-
Generate new schema with hidden properties excluded:
- In the controller template, introduce a config to hide the properties
content: { 'application/json': { schema: getModelSchemaRef(Todo, {hiddenProps: true}) } }, - In
@loopback/repository-json-schema, read the flag then generate a new reference for them calledTodoExcludeHiddenProps
- In the controller template, introduce a config to hide the properties
-
Use the existing
excludeconfigcontent: { 'application/json': { schema: getModelSchemaRef(Todo, {title: 'NewTodo', exclude: ['secretProp']}), }, },
I'm wondering why nobody Is working of this. Excluding hidden properties at controller level is redundant and lead to weirds generated schemas like this:
export interface TodoExcludingSecretPropWithRelations {
id?: number;
title: string;
desc?: string;
}
@jannyHou do you have any plan to sort it out?
+1 for this, currently there is no way to remove property in a nested model