Dynamic exposures and documentations

12 views
Skip to first unread message

Paolo “Sberla” Fabbri

unread,
Apr 3, 2025, 1:05:56 PMApr 3
to Grape Framework Discussion

I'm working on an endpoint that should return an array of images. This array will contain URLs for the preview images in sizes specified by a parameter in the request. So far, I have been using something like this:



expose :urls, documentation: {
desc: "An object with the URL of the original file and URLs for image previews in the specified sizes.",
example: { original: "https://example.com/original.jpg", "100x200": "https://example.com/100x200.jpg" },
type: Hash
}

def URLs
# function that will take the sizes passed as WxH in the options and
# return a hash with the URLs plus the URL for the original
# image so something like:
{
"100x200" => "https://example.com/100x200.jpg",
"300x400" => "https://example.com/200x300.jpg",
original: "https://example.com/original.jpg"
}
end


Our code has worked well so far, but we have now decided to start using Swagger documentation to automatically build the TypeScript types used in our frontend. However, that code is compiled to:


urls: Record<string, never>;


My first attempt was to fix this by moving the URLs into a separate entity with:


options[:sizes].each do |size|
expose size, documentation: {
type: String,
desc: "preview in size #{size}",
required: true
}
end
expose :original, documentation: {
type: String,
desc: "Original file",
required: true
}

I didn't have much hope that this would work, as I realized that even if we had the sizes when the entity was evaluated, it would have been empty when generating the types. Unfortunately, it failed to parse the object because we don't have access to the options at that point. After many attempts, I can't seem to find a solution, and I'm starting to wonder if this is even possible. Does anyone know how I could fix this?


Daniel D.

unread,
Apr 4, 2025, 12:39:14 PMApr 4
to ruby-...@googlegroups.com
The Google group has been very quiet, consider opening an issue in https://github.com/ruby-grape/grape-swagger?

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ruby-grape/2e65f018-400a-4261-a6eb-658e7298712cn%40googlegroups.com.


--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - t:@dblockdotorg - ig:@artdblockdotorggithub/dblock

Reply all
Reply to author
Forward
0 new messages