rom-http
rom-http copied to clipboard
Allow optional attributes in relation schemas
REST APIs come in many flavours, some not always purely RESTful. It would be helpful if schemas for rom-http relations could support optional attributes. This is for a scenario where an API provides a limited set of attributes from a search or collection API endpoint. Then the full details of the resource must be retrieved from a REST endpoint that represents that resource.
The specific example that triggered this is using rom-http with the Spoonacular API. This has a "Search Recipes" endpoint that returns recipes with about 8 attributes, then the "Get Recipe Information" endpoint returns a full recipe object with much more data.
Examples
This might look like:
module Spoonacular
class Recipes < ROM::Relation[:http]
schema(:recipes) do
attribute :id, Types::Integer.meta(primary_key: true)
attribute :title, Types::String
attribute :image, Types::String
attribute :readyInMinutes, Types::Integer.optional
attribute :servings, Types::Integer.optional
attribute :sourceUrl, Types::String.optional
attribute :extendedIngredients, Types::Array.optional
end
struct_namespace Spoonacular
auto_struct true
....
end
end
NOTE: Some attributes are camelCase because currently I'm not sure of the best approach to map them to ruby snake case. If there is a known pattern for this, tips welcome :)
Resources
First queried here.
@flash-gordon 👋🏻 just wanted to double-check with you - this makes perfect sense, right? We could follow the same convention as you introduced in dry-struct, so just adding attribute? and handle it as expected would do the trick. WDYT?
@solnic / @flash-gordon, I wanted to check in on this. I'm running into a similar issue (where the APII am integrating with does not always return the full set of possible params on all requests to the same resource). dry-initializer and dry-schema support something similar to this already, I can work on a PR if this is still a possibility.
As a somewhat philosophical point, since external API schemas are out of the control of the API wrapper library, it might make more sense if all attributes were assumed optional, or at least if there could be a ROM config to set this.