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

How to set diffferent value for same parameter name with different scope? #214

Closed
anujaware opened this issue Jun 16, 2015 · 5 comments
Closed

Comments

@anujaware
Copy link

Suppose I have a nested form and have 'email' field for parent and child model also if I want to pass parameters for two child objects with different email address. Is there a way to do this?

@thomassnielsen
Copy link

We're seeing the same issue. We're following the jsonapi spec, which requires related resources to be nested inside a response tag. Example:

{
    "data":{
        "type":"cart-items",
        "attributes":{
            "count":"1",
            "product-iteration-id":2
        },
        "relationships":{
            "product-variant":{
                "data": {
                    "type":"product-variants",
                    "id":3
                }
            },
            "user":{
                "data": {
                    "type":"users",
                    "id":4
                }
            }
        }
    }
}

This leaves us with a problem when creating params, as we would need multiple ids and types with different scopes. #221 lets us scope them correctly, but we're still having the issue with them being called the same thing.

To illustrate the problem:

parameter :id, "Id of product variant", scope: [:relationships, :variant, :data]
parameter :id, "The id of the user (checked against auth token)", scope: [:relationships, :user, :data]
parameter :type, "Id of product variant", scope: [:relationships, :variant, :data]
parameter :type, "Id of product variant", scope: [:relationships, : user, :data]

I'm not familiar enough with the internals of rspec_api_documentation to suggest a technical solution, but from a usage perspective it would be nice if we could define the data and parameter together. Something like:

parameter [:relationships, :user, :data, :id], "Id of the user", { user.id }

@cover
Copy link
Contributor

cover commented Sep 2, 2015

#221 also takes care of setting parameters with the same name (and different scopes). You just need to prefix the scope:

let(:relationships_variant_data_id) { 111 }
let(:relationships_user_data_id) { 222 }
let(:relationships_variant_data_type) { 'variant' }
let(:relationships_user_data_type) { 'user' }

To set the parameter value it looks for a method called scope_parameter and if it doesn't exists it tries with parameter, mostly in order to avoid breaking existing apps that use scopes

@thomassnielsen
Copy link

Will it work with underscored_names? (I don't see how it will know the difference between scopes and variable_names?) What about "dasherized-names"? We're using strings in our lets because we need dasherized names, and that seems to work.

Example:

parameter "display-addr", "The address of the shop", required: false, scope: :attributes
let("display-addr") { "Test shop street 1" }

@cover
Copy link
Contributor

cover commented Sep 2, 2015

Yes, it does.

parameter :resource_id, "Id of product variant", scope: [:data, :relationships, :variant, :data]

will look for data_relationships_variant_data_resource_id and fallback to resource_id.
When it builds the hash it passes an array with all the keys (scope + param name). The last one is always the param name and the previous one the scope making it working with underscored names

I haven't tried with dasherized names, but if let("display-addr") works then the scoped let("attributes_display-addr") should work as well. The scope and the parameter name are joined using the underscore

@thomassnielsen
Copy link

Thanks a lot, especially for linking to the relevant code 👍
I'll give it a try once our specs are past their current pending state.

Btw, this should be documented. If nobody else does it I'll throw in a PR once I've verified how it works for us.

ghost pushed a commit to einSelbst/rspec_api_documentation that referenced this issue Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants