joukevandermaas / saule

JSON API library for ASP.Net Web API 2.
https://joukevandermaas.github.io/saule
MIT License
76 stars 37 forks source link

Discuss new LinkType.Top #201

Closed sergey-litvinov-work closed 6 years ago

sergey-litvinov-work commented 6 years ago

Hi Jouke,

I wanted to ask one thing and i can make PR to add it, but wanted to get your opinion about it.

We have a case when we have endpoint that returns collection of items and we don't want to add additional endpoint to get specific item. The problem with the links as by default it will generate both self in each item and then links->self at top level. We want to be able to generate top level self but without self on each item level.

Right now it's impossible as if i set WithLinks(Saule.LinkType.Related); in the resource description, then it wont generate both self links and we need that top one.

Is it ok to add LinkType.Top like this

public enum Test
{
        /// <summary>
        /// No links
        /// </summary>
        None = 0,

        /// <summary>
        /// Only self links
        /// </summary>
        Self = 1,

        /// <summary>
        /// Only related links
        /// </summary>
        Related = 2,

        /// <summary>
        /// Only top links
        /// </summary>
        Top = 4,

        /// <summary>
        /// Generate all possible links
        /// </summary>
        All = ~None
}

So then i can specify LinkTypes.Top and it will display only top links.self section without self for each item.

And to prevent existing behavior i can check in the code (ResourceSerializer.CreateTopLevelLinks) to render top self link only if

if (_resource.LinkType.HasFlag(LinkType.Top) && !_resource.LinkType.HasFlag(LinkType.Self)
 || _resource.LinkType.HasFlag(LinkType.Self))
{
      result.Add("self", _baseUrl);
}

So existing code will work fine as Self would be treated as Top too. And if someone doesnt want it, then he can specify it in the resource as LinkType.Top & ~LinkType.Self

joukevandermaas commented 6 years ago

I think this makes sense, but I'm not sure I understand the reasoning for calling this Top. It sounds like it means "add the link for the collection, but not the items". Could we call it something like CollectionSelf? In any case I think it should have Self in the name. Does that make sense to you?

sergey-litvinov-work commented 6 years ago

Totally makes sense. And as i renamed it to TopSelf and you merged it, i'm closing this defect. Thanks!

sergey-litvinov-work commented 6 years ago

@joukevandermaas or if you think that CollectionSelf is better, i can rename it and make new PR