reckart opened a new issue, #31:
URL: https://github.com/apache/uima-uimaj-io-jsoncas/issues/31
**Describe the bug**
`JsonCas2Deserializer.deserialize()` never returns when the top-level JSON
object contains a field other than `%HEADER`, `%TYPES`, `%VIEWS` or
`%FEATURE_STRUCTURES` at a point where the main loop of
`CasDeserializer.deserialize()` reaches it, e.g. as the first field. The
`switch` over the field name in that loop has no `default` branch, so for an
unknown field nothing advances the parser and `while (aParser.currentToken() !=
null)` repeats the same iteration forever. As a result, feeding any non-CAS
JSON object (for example a line from a JSON-lines file such as `{"text": "Great
food", "label": "+"}`) hangs instead of failing.
The same loop also never ends when `%VIEWS` or `%FEATURE_STRUCTURES` has the
value `null` and comes after `%TYPES`, e.g. `{"%TYPES": {}, "%VIEWS": null}`.
Here the field is known, but nothing consumes the `null` token, so the loop
again sees the same token over and over. When the same `null` field is the
first field, the deserializer fails with an exception instead, so whether it
hangs depends on what comes before it.
Separately, content after the top-level object is ignored: a stream with two
CAS JSON documents (one per line) deserializes the first one and returns
without an error.
**To Reproduce**
```java
CAS cas = CasCreationUtils.createCas((TypeSystemDescription) null, null,
null);
JsonCas2Deserializer d = new JsonCas2Deserializer();
d.setFsMode(FeatureStructuresMode.AS_ARRAY);
d.deserialize(new ByteArrayInputStream("{\"id\": 1}".getBytes(UTF_8)), cas);
// never returns
d.deserialize(new ByteArrayInputStream("{\"%TYPES\": {}, \"%VIEWS\":
null}".getBytes(UTF_8)), cas); // never returns
```
Observed results:
| Input | Result |
|---|---|
| a CAS serialized with `JsonCas2Serializer` | returns, CAS correct |
| `{"text": "Great food", "label": "+"}` | never returns |
| a serialized CAS with `"id": 1` inserted as the first field | never
returns |
| a serialized CAS with `"id": 1` appended as the last field | returns, CAS
correct |
| two serialized CASes, one per line | returns, only the first CAS is read,
no error |
| `{"%TYPES": {}, "%VIEWS": null}` | never returns |
| `{"%TYPES": {}, "%FEATURE_STRUCTURES": null}` | never returns |
| `{"%TYPES": null, "%VIEWS": null}` | never returns |
| `{"%VIEWS": null}` | `JsonParseException`: "Views declaration must be a
JSON object" |
| `{"%FEATURE_STRUCTURES": null}` | `JsonParseException`: "Expected feature
structure to start with START_OBJECT but found END_OBJECT instead" |
| `{"%HEADER": null}` | `NullPointerException` on
`header.getOffsetEncoding()` |
| `{"%TYPES": {}, "%VIEWS": {}}` | returns |
**Expected behavior**
An unknown top-level field either causes an exception (e.g. "unexpected
field [id] - not a UIMA CAS JSON document") or is skipped, but the deserializer
always terminates. A top-level field whose value has the wrong JSON type
(`null` where an object or array is expected) causes an exception that names
the field, wherever the field appears; a `NullPointerException` is not a useful
message for that case either. Content after the end of the top-level object
causes an exception instead of being silently ignored.
**Please complete the following information:**
- Version: 0.5.0
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]