joukevandermaas / saule

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

relative url for links object #144

Open luke-zhang opened 7 years ago

luke-zhang commented 7 years ago

is there a way to return relative url for links object. in our projects, we'd like to see this "links": { "self": "/api/sessions" }, instead of
"links": { "self": "http://localhost:49895/api/sessions" },

joukevandermaas commented 7 years ago

This is not possible at the moment, but I'm open to a PR to allow this.

The request URI comes from here:

https://github.com/joukevandermaas/saule/blob/41c9b84a0158bfa2b633e4023a15f2a24e8dcc0d/Saule/Http/PreprocessingDelegatingHandler.cs#L59

It's then passed into the serializer here:

https://github.com/joukevandermaas/saule/blob/41c9b84a0158bfa2b633e4023a15f2a24e8dcc0d/Saule/JsonApiSerializer.cs#L73

It's prepended to every generated path here:

https://github.com/joukevandermaas/saule/blob/41c9b84a0158bfa2b633e4023a15f2a24e8dcc0d/Saule/Serialization/ResourceSerializer.cs#L318

I think a property GenerateRelativeUrls in JsonApiSerializer (default value: false) is a nice backwards-compatible spot to put this.

luke-zhang commented 7 years ago

@joukevandermaas thanks a lot for your help. I am thinking if IUrlPathBuilder is better than JsonApiSerializer to place this property since it can be customized easily.

joukevandermaas commented 7 years ago

I agree with you, but it currently does not have this behavior. So that would be breaking backwards compatibility. If you can think of a way to not break existing stuff, I'm all for it.