joukevandermaas / saule

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

Relationship attributes not being returned along with included members. #113

Closed dhirajdhakal09 closed 8 years ago

dhirajdhakal09 commented 8 years ago
    I could not generate the output with attributes in the relationship node, only 'link' is returned and also included members are not displayed.  Please help me asap.

    This is how i defined my controller 

    [HttpGet]
    [ReturnsResource(typeof(NumberResource))]
    [Route("api/numbers")]
    public IHttpActionResult GetSomeNumbers()
    {
        return Ok(MockupGetSomeNumbers());
    }

    public IQueryable MockupGetSomeNumbers()
    {
        var myNumber = new Number
        {
            Id = new Guid(),
            Number = "12345",
            Plan = new Plan
            {
                Id = new Guid(),
                Description = "Some Description"
            }
        };
        var someQueryableNumbers = new List<Number>();
        someQueryableNumbers.Add(myNumber);
        return someQueryableNumbers.AsQueryable();
    }

    And the following is my resources

    public class NumberResource : ApiResource
    {
        public NumberResource()
        {
            OfType("numbers");
            Attribute("Number");

            BelongsTo<PlanResource>("plans");
        }
    }

    public class PlanResource : ApiResource
    {
        public PlanResource()
        {
            OfType("plans");
            Attribute("Description");
        }

    }

    And the following is the output I got

    {
        "data": [
        {
        "type": "numbers",
        "id": "00000000-0000-0000-0000-000000000000",
        "attributes": {
        "number": "12345",

          },
          "relationships": {

            "plans": {
              "links": {
                "self": "http://localhost:58739/api/numbers/00000000-0000-0000-0000-000000000000/relationships/plans/",
                "related": "http://localhost:58739/api/numbers/00000000-0000-0000-0000-000000000000/plans/"
              }
            }
          },
          "links": {
            "self": "http://localhost:58739/api/numbers/00000000-0000-0000-0000-000000000000/"
          }
        }
        ],
        "links": {
        "self": "http://localhost:58739/api/numbers"
        }
    }
joukevandermaas commented 8 years ago

When including relationships, there are three options:

  1. The property does not exist
    Saule will generate a link according to the spec.
  2. The property exists, but it is null
    Saule will generate a link.
  3. The property exists and is not null
    Saule will add the resource to the included hash according to the spec.

In your case, you need to set the value of a Plan property on Number.

dhirajdhakal09 commented 8 years ago

Hi,

I have already set the value of plan property on number, I showed in the example too, doing like as follows:

var myNumber = new Number { Id = new Guid(), Number = "12345", Plan = new Plan { Id = new Guid(), Description = "Some Description" } };

But still attribute of Plan resource not seen in the Number resource.

Moreover, I tried to use the same resource provided in the wiki, even on doing that I cannot see attribute in relationship.

I am using Visual Studio 13 Community Edition.

Thanks!!

On Sat, Jun 4, 2016 at 1:30 AM, Jouke van der Maas notifications@github.com wrote:

When including relationships, there are three options:

The property does not exist Saule will generate a link according to the spec. The property exists, but it is null Saule will generate a link. The property exists and is not null Saule will add the resource to the included hash according to the spec.

In your case, you need to set the value of a Plan property on Number.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

dhirajdhakal09 commented 8 years ago

Can you please share me the simple working example?