deblockt / hal-rest-client

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

error parsing json #10

Closed baxtheman closed 7 years ago

baxtheman commented 7 years ago

Please, see this hal json response from Spring Data application:

https://gist.github.com/baxtheman/5873c9de7821970f2d075b733ee6a3c4

It causes a parsing error calling the service with fetchResource

Uncaught (in promise) TypeError: Cannot read property 'href' of undefined at JSONParser.jsonToResource (19:17) at JSONParser.parseJson (19:67) at eval (19:63) at Array.map (<anonymous>) at JSONParser.parseJson (19:63) at JSONParser.jsonToResource (19:45) at JSONParser.parseJson (19:67) at eval (19:63) at Array.map (<anonymous>) at JSONParser.parseJson (19:63)

This the position of the exception:

Any suggestions? thx

deblockt commented 7 years ago

I think, this is caused by lavorazioniarray ho contains json like this :

{
     "_links": {
       "commessa": {
         "href": "http://localhost:8180/registrazioni/data/commessa/23"     
      },
      "lavorazione": {
         "href": "http://localhost:8180/registrazioni/data/lavorazione/1"
      }
   }
}

this look like hal object, who contains two property commessa and lavorazione but there are no _self link.

the jsonToResource method, look if _linksattribute is present. It it is, it try to create an HalResource, but to create HalResource self link is mandatory.

Can you explain what is this object? It's a HAL Resource or a simple plain object?

baxtheman commented 7 years ago

It's a pure HAL resource generated by Sprind Data @RepositoryRestResource. Maybe Spring Data has some issues with this entity:

@Entity(name = "Commesse")
public class Commessa {

    @Id
    @Column(name = "ID_Commessa")
    private Long id;
........

    @OneToMany(mappedBy="commessa", fetch=FetchType.LAZY)
    private Collection<CommessaLavorazione> lavorazioni;
deblockt commented 7 years ago

I have reread the doc, and effectively, resource may not define self link. I must add a case to this issue

baxtheman commented 7 years ago

Pls, share the doc link where you find the issue. Do you think is it a fast fix?

deblockt commented 7 years ago

It's present on this link http://stateless.co/hal_specification.html Resource chapter. In most cases, resources should have a self URI

I think this can be quickly fix, I will looking this night.

baxtheman commented 7 years ago

Ok, my temp workaround to go on with my job is

    JSONParser.prototype.jsonToResource = function (json, c, resource) {
        if (!("_links" in json)) {
            throw new Error("object is not hal resource " + JSON.stringify(json));
        }
        if (!json._links.self) {
            return resource;
        }
        if (!resource) {
            var uri = "string" === typeof json._links.self ? json._links.self : json._links.self.href;
            resource = hal_factory_1.createResource(this.halRestClient, c, uri);
        }

Good job and thank you!

deblockt commented 7 years ago

this issue is fixed on 0.2.6 version.

But warning, templated link are not actualy supported. Have a templated link on self can create issue. A cache is used to refresh automaticly entity using entity uri (self link). With templated link you can two different entity with same uri, so cache can create refresh issue. If you have this type of issue, you can use clearCache methode while templated link are not fully supported.

see #9