cloudevents/sdk-java

Error parsing datacontenttype with charset specified

andrejpk opened this issue · 4 comments

When I try to use the Jackson parser to parse an MQTT CloudEvent that looks like this:

[
  {
    "id": "0d989854-5940-4721-9c8e-a09902e4592c",
    "source": "apk2-evgns-mqtt-demo",
    "type": "MQTT.EventPublished",
    "data": {
        "hello": "world"
      }
    },
    "time": "2023-01-09T21:43:16.226+00:00",
    "specversion": "1.0",
    "datacontenttype": "application/json; charset=utf-8",
    "subject": "devices/client-4/messages/d2c",
    "mqtttopic": "devices/client-4/messages/d2c"
  }
]

I get an error: com.fasterxml.jackson.databind.exc.MismatchedInputException: Wrong type OBJECT for attribute data, expecting STRING. Because content type is not a json, only a string is accepted as data

The problem seems to stem from the extended contenttype: application/json; charset=utf-8which is explicitly recommended in the MQTT binding docs.

I traced this to this line where the datacontentype is checked against a regexp that doesn't accept the semicolon and trailing format:

https://github.com/cloudevents/sdk-java/blob/master/formats/json-jackson/src/main/java/io/cloudevents/jackson/JsonFormat.java#L221

It looks like #471 changed the way datacontenttype is checked with the prior implementation using a startswith check which would have worked.

Suggest adding a test case for the extended format and possibly whitespace support if the spec allows that too.

This should be fixed by 6362bfb, we currently trying to solve a problem with the release process that is a blocker to release a fix.

cc @duglin

Hi @andrejpk, I just published a new release 2.4.1 which includes the fix, please let me know if that fixes your issue

https://github.com/cloudevents/sdk-java/releases/tag/2.4.1

I'll test it out today, thank you, that was quick

Winner!! Works well now, thanks again @pierDipi .