jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
273 stars 81 forks source link

UnregisteredTypeException Issue #213

Closed feludens closed 5 years ago

feludens commented 5 years ago

Hi Jasmine, I am running into an issue and I am not sure why this is happening ONLY to this class. I am in a super time crunch after a backend change has been made where they changed the type attribute's name. I went ahead and updated the name from buildings to deliveryBuildings, since then, I have been getting UnregisteredTypeException. Do you know why that is happening?

@Type("deliveryBuildings")
@JsonIgnoreProperties(ignoreUnknown = true)
public class BuildingDTO extends JsonApiBaseDTO implements Building {

    private String instructions;
    private String streetNumber;
    private String streetName;
    private String storeId;
    private String city;
    private String postalCode;
    private String siteId;
    private double latitude;
    private double longitude;
    private boolean room;
    private boolean validCoordinates;
}

And here's the payload:

"data": [
    {
      "id": "117",
      "type": "deliveryBuildings",
      "attributes": {
        "instructions": null,
        "updatedBy": "SYSTEM",
        "streetNumber": "0",
        "city": "MONTGOMERY",
        "latitude": 0.00,
        "postalCode": "00000",
        "storeId": "0000",
        "version": 1,
        "room": false,
        "validCoordinates": true,
        "createdAt": "2018-12-21T20:59:42.807Z",
        "streetName": "Street Address",
        "createdBy": "SYSTEM",
        "name": "Full Address Name",
        "siteId": "1",
        "longitude": 0.00,
        "updatedAt": "2018-12-21T20:59:42.807Z"
      },
      "relationships": {},
      "links": {
        "self": {
          "meta": {},
          "href": "/link...."
        }
      }
    },
{...}

As I mentioned, when the type was buildings, it worked. Here's how I am trying to deserialize it:

private static final ResourceConverter JSON_API_CONVERTER =
            new ResourceConverter(
                    SiteDTO.class,
                    RegionDTO.class,
                    BuildingDTO.class,
                    TestDTO.class,
                    DeliveryBuildingDTO.class,
                    CampusBaseStoreDTO.class,
                    CampusBaseAddressDTO.class);

JSONAPIDocument<T> document =
                    JSON_API_CONVERTER.readDocument(response.getBody(), resourceClass);
            return document.get();

All other classed are deserialized as expected.

Any idea on why this is happening?

Thanks for the help!

EDIT: Are models being cached somehow?

EDIT 2: New findings If I call this directly: new ResourceConverter(BuildingDTO.class).readDocumentCollection(response.getBody(), resourceClass) Then it works. BuildingDTO, as you can see below, is included in my Resource Converter. ---- I THINK, I might know what's happening... Both BuildingDTO and DeliveryBuildingDTO have the same name type "deliveryBuilding" but they are actually two different object types.

Do you think having two different classes with the same type in a converter, could be causing this issue?

jasminb commented 5 years ago

Hello @feludens , registering two different classes with the same type is not supported. Registering one will invalidate the other which means that than when the other one is used you end up with the error described.

feludens commented 5 years ago

Thanks for confirming it!