Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional attributes in relation schemas #46

Open
pmackay opened this issue Feb 8, 2021 · 3 comments
Open

Allow optional attributes in relation schemas #46

pmackay opened this issue Feb 8, 2021 · 3 comments
Labels

Comments

@pmackay
Copy link

pmackay commented Feb 8, 2021

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.

@pmackay pmackay added the feature label Feb 8, 2021
@solnic
Copy link
Member

solnic commented Feb 9, 2021

@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?

@htcarr3
Copy link

htcarr3 commented Jul 23, 2023

@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.

@htcarr3
Copy link

htcarr3 commented Jul 23, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants