jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

Class doesn't have a Type annotation error, even though the class does have one

Open dylwedma11748 opened this issue 2 years ago • 3 comments

My situation is a bit complicated. I'm working on a library to handle API calls to Patreon's APIv2 endpoints. I'm structuring my library similarly to the original. I've already wote most of the resource classes, but the error I'm getting is Class doesn't have a Type annotation. All resource classes must be annotated with a Type annotation! even though it does have one. I've uploaded all of the resource classes to a repo and I've also made a StackOverflow post explaining it in more detail.

The stacktrace I'm getting is: Exception in thread "main" java.lang.IllegalArgumentException: Class [Lpatreon.java.v2.resources.Campaign; doesn't have a Type annotation. All resource classes must be annotated with a Type annotation! at com.github.jasminb.jsonapi.ConverterConfiguration.processClass(ConverterConfiguration.java:168) at com.github.jasminb.jsonapi.ConverterConfiguration.registerType(ConverterConfiguration.java:310) at com.github.jasminb.jsonapi.ConverterConfiguration.processClass(ConverterConfiguration.java:83) at com.github.jasminb.jsonapi.ConverterConfiguration.registerType(ConverterConfiguration.java:310) at com.github.jasminb.jsonapi.ConverterConfiguration.<init>(ConverterConfiguration.java:50) at com.github.jasminb.jsonapi.ResourceConverter.<init>(ResourceConverter.java:93) at com.github.jasminb.jsonapi.ResourceConverter.<init>(ResourceConverter.java:83) at patreon.java.v2.PatreonAPI.<init>(PatreonAPI.java:70) at patreon.java.v2.PatreonAPI.<init>(PatreonAPI.java:60) at patreon.java.v2.PatreonAPITest.main(PatreonAPITest.java:8)

The Campaign class does have a Type annotation:

@Type("campaign")
public class Campaign extends BaseResource {
    ...
}

The ResourceConverter is being initialized like so:

public class PatreonAPI {
    ...    
    private ResourceConverter converter;

    PatreonAPI(String accessToken, RequestUtil requestUtil) {
        this.accessToken = accessToken;
        this.requestUtil = requestUtil;

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        this.converter = new ResourceConverter(objectMapper, Address.class, Benefit.class, Campaign.class, Deliverable.class, Goal.class, Media.class, Member.class, PledgeEvent.class, Post.class, Tier.class, User.class);
        this.converter.enableDeserializationOption(DeserializationFeature.ALLOW_UNKNOWN_INCLUSIONS);
    }
}

I've found that at stack "ConverterConfiguration.processClass(Class<?>) line: 83", targetType is set to Campaign, but clazz is set to Address:

variables

In the Address class, the only reference to the Campaign class is an Array of Campaigns with a Relationship annotation:

@Type("address")
public class Address extends BaseResource {
    ...
    @Relationship("campaigns")
    private Campaign[] campaigns;
}

dylwedma11748 avatar Mar 25 '23 17:03 dylwedma11748

Will take a look next week hopefully. Thank you for reporting!

jasminb avatar Mar 25 '23 17:03 jasminb

Not a problem. There's no rush, as this is a personal project.

dylwedma11748 avatar Mar 25 '23 18:03 dylwedma11748

Found out that it was the arrays causing it, I guess for some reason when you use a resource class that has a Type annotation in an array, it doesn't work or something... I don't know. I replaced the arrays with Lists instead.

dylwedma11748 avatar Mar 26 '23 05:03 dylwedma11748