Reputation: 162
I'm using Grape-Swagger to generate Swagger documentation for my API. I have a endpoint that returns a JSON object with a nested object with dynamic keys.
I'm having trouble defining this nested object in my Swagger documentation so it will correctly genarete the TS client API.
For the UI I'm using typescript-swagger-api
to generate the API for the client, but when generating the API the interface for ResourcePermissions
the field groups is populated with object like so:
export interface ResourcePermissions {
...,
groups?: object;
}
But I'm looking more for something like this:
export interface ResourcePermissions {
...,
groups?: Record<string, ResourcePermission>;
}
Here's an example of the JSON response:
{
"resource_type": "user",
"resource_id": 1,
"groups": {
"group1": {
"rule": "read",
"id": 1,
"group_id": 1,
"group_name": "Group 1"
},
"group2": {
"rule": "write",
"id": 2,
"group_id": 2,
"group_name": "Group 2"
}
}
}
I tried to define the GrapeEntity like this, but value_type
is not being taken into consideration.
class ResourcePermissions < Grape::Entity
expose :key
expose :key2
expose :groups,
documentation: { type: Hash,
value_type: Entities::ResourcePermission } do |resource_permissions, options|
resource_permissions[:groups].each_value { |value| Entities::ResourcePermission.represent(value, options) }
end
end
I also tried a different approach with dynamic keys like so
class MapEntity < Grape::Entity
expose :**, using: Entities::ResourcePermission
end
But the result is far from what I expected.
export interface ResourcePermissions {
...,
"**"?: ResourcePermission;
}
Even tried something like this by Copilot's suggestion
documentation: { type: { Hash => Entities::ResourcePermission } }
but this broke the generation completely.
Any help would be greatly appreciated.
Upvotes: 0
Views: 32