I have a class that is used for collection responses:
public class CollectionResponse<T>
{
[JsonExtensionData]
public Dictionary<string, object> Items { get; } = new(1);
public CollectionResponse(string name, IEnumerable<T> objects)
{
Items.Add(name, objects);
}
}
[JsonExtensionData]
perfectly helps with having a dynamic name for different response types. However, I need to set [Required]
annotation for that extended data to reflect it in OpenApi schema.
new CollectionResponse<Car>("cars", new [] {new Car()});
should result in OpenApi schema:
"CarCollectionResponse": {
"required": [
"cars"
],
"type": "object",
"properties": {
"cars": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Car"
},
"readOnly": true
}
}
}
So, question is, how to maintain a dynamic property name while also setting it as [Required]
?