deblockt / hal-rest-client

Typescript HAL Rest client
Apache License 2.0
25 stars 11 forks source link

Cannot parse link as resource if mapping link name from hal through @HalProperty() decorator #24

Open vdytynyuk opened 6 years ago

vdytynyuk commented 6 years ago

Lets say I have a model which has a field expected as link.

Model:

export class Foo extends HalResource {
   @HalProperty('response.bar')
   bar: Bar; // it's link
}

When fetching Foo resource hal-parser spits out bar field as HalResorce not as Bar which is incorrect behavior. After some debugging I found that library passes to method Reflect.getMetadata link key as it is from hal json (e.g. 'response.bar') but MetadataMap contains field names and appropriate constructors in format that is specified in @HalProperty() decorator (e.g. 'bar'). Apparently proper constructor cannot be found by incorrect key and that leads to casting Bar field to HalResource.

deblockt commented 6 years ago

I don't understand the issue.

Can you provide you json ? I will try to reproduce your error.

vdytynyuk commented 6 years ago

@deblockt here's my json.

{
    "_links": {
        "self": {
            "href": "projects/2",
            "type": "application/hal+json"
        },
        "service.runs": {
            "href": "project/2/runs",
            "type": "application/hal+json"
        }
    },
    "key": 2,
    "name": "proj54"
}

Here's my Project model.

export class Project extends HalResource {
    @HalProperty()
    key: number;

    @HalProperty()
    name: string;

    @HalProperty('service.runs')
    runs: RunCollection;
}

export class RunCollection extends HalResource {
    @HalProperty()
    count: number;

    @HalProperty(Run)
    list: Run[];
}

After execution of await this.project.runs.fetch(true); field this.project.runs is HalResource not RunCollection.

vdytynyuk commented 6 years ago

I believe this PR should fix the problem. When will it be merged?