crob1140 / confluence-java-client

A simple Java client for the Confluence Cloud REST API
MIT License
1 stars 7 forks source link

Wrong mapping of Metadata Labels #5

Open thlaegler opened 4 years ago

thlaegler commented 4 years ago

Using Confluence Server 7.5.2 the field content.metdata.labels a result list which contains the actual labels; it's not just a List<Label>. Not sure if this changed recently or since which version this is the case? It is not well documented but looking at the response schema it gives a hint, that metadata.labels is an object and not an array: https://docs.atlassian.com/ConfluenceServer/rest/latest/#api

Example REST response:

{
  [...]
  "metadata": {
    "currentuser": {
      "_expandable": {
        "favourited": "",
        "lastmodified": "",
        "viewed": "",
        "lastcontributed": ""
      }
    },
    "labels": {
      "results": [
        {
          "prefix": "global",
          "name": "foo",
          "id": "12345678901"
        },
        {
          "prefix": "global",
          "name": "bar",
          "id": "12345678902"
        }
      ],
      "start": 0,
      "limit": 200,
      "size": 2,
      "_links": {
        "self": "https://confluence.example.org/rest/api/content/123456789/label"
      }
    }
  }
  [...]
}
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 7961] (through reference chain: com.github.crob1140.confluence.requests.GetContentResponse["results"]->java.util.ArrayList[0]->com.github.crob1140.confluence.content.Content["metadata"]->com.github.crob1140.confluence.content.Metadata["labels"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1364)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1140)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1094)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1682)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:977)
    at org.glassfish.jersey.jackson.internal.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:838)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    ... 12 more

I could create a pull request to fix this. But someone should probably first check if this is a version-specific change or just a bug in this client.

Reproducable with:

ConfluenceClient confluenceClient = new ConfluenceClient(foo..., bar....);
List<Content> searchResults = confluenceClient.getContent(new GetContentRequest.Builder()
    .setTitle("My Title")
    .setExpandedProperties(new ExpandedContentProperties.Builder()
        .addMetadata(new ExpandedMetadataProperties.Builder()
            .addLabels()
            .build())
        .build())
    .build());

Currently Metadata.java looks like this:

public class Metadata {
  private List<Label> labels;
  private Version version;
}

Proposed fix: It should probably look more like this

public class Metadata {
  private LabelResult labels;
  private Version version;
}

public class LabelResult {
  private List<Label> results;
}

public class Label {
  private LabelPrefix prefix;
  private String name;
  private String id; <--- should be added
}

Also, in class Label the field id is not mapped.

cpesch commented 3 years ago

@thlaegler did you fix this problem?

I just saw a "solution" here to entirely remove the Label https://github.com/onesty-dev/confluence-java-client/commit/7d2560bbca44d4f26180058f5144e19de1e08b9e