joukevandermaas / saule

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

used _baseUrl.AbsoluteUri instead of _baseUrl to avoid included port number in the self link #206

Closed sergey-litvinov-work closed 6 years ago

sergey-litvinov-work commented 6 years ago

Hi Jouke,

This is small PR to fix one small issue. It happens for us only when we host it under IIS (Win10\WinServer 2016 and probably all others too).

The issue is that top self link always includes port even if port is standard like this:

    "links": {
        "self": "http://localhost:80/SauleExample/api/values"
    }

or like this

    "links": {
        "self": "https://localhost:443/SauleExample/api/values"
    }

It's still valid url as it can contain port, but it looks a bit weird. Somehow IIS always pushes port in the Request.Url and _baseUrl.OriginalUrl always has it, and when we pass Uri as object for serialization, then Newtonsoft.Json uses .OriginalUrl and it gives us 80\443 ports for regular http\https hosting.

I pushed change to use _baseUri.AbsoluteUri in the ResourceSerializer and also updated a couple of unit tests as they were using Uri from JObject but now it has just regular string. I also added unit test for ResourceSerializer to test this specific case. Also i verified that non standard ports still are displayed under IIS\IIS Express

Here is also screenshot of VisualStudio watch that shows current values during debug

sauleexample_debug

Full response example before fix looks like it:

{
    "data": [
        {
            "type": "person",
            "id": "1",
            "links": {
                "self": "http://localhost/SauleExample/api/people/1/"
            },
            "attributes": {
                "name": "some person",
                "age": 42
            },
            "relationships": {
                "job": {
                    "links": {
                        "self": "http://localhost/SauleExample/api/people/1/relationships/job/",
                        "related": "http://localhost/SauleExample/api/people/1/job/"
                    },
                    "data": {
                        "type": "company",
                        "id": "11"
                    }
                }
            }
        }
    ],
    "links": {
        "self": "http://localhost:80/SauleExample/api/values"
    },
    "included": [
        {
            "type": "company",
            "id": "11",
            "links": {
                "self": "http://localhost/SauleExample/api/companies/11/"
            },
            "attributes": {
                "name": "Some company",
                "location": "some location"
            }
        }
    ]
}
sergey-litvinov-work commented 6 years ago

@joukevandermaas just wanted to double check, you want me to do something more there or you just waiting for something? as you haven't merged these changes :)

joukevandermaas commented 6 years ago

@sergey-litvinov-work my apologies, I got distracted by other things and completely forgot.

sergey-litvinov-work commented 6 years ago

Thanks!