Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MappingIterator should move past errors or not return hasNext() == true #733

Closed
lorrin opened this issue Mar 25, 2015 · 1 comment
Closed
Milestone

Comments

@lorrin
Copy link

lorrin commented Mar 25, 2015

When MappingIterator encounters an Exception, it continues to return hasNext() == true, but next() keeps throwing the same Exception and the parser never modes forward.

I would expect the following code to print:

Parsed foo 1: 'bar1'
UnrecognizedPropertyException parsing line 2.
Parsed foo 3: 'bar2'

Or maybe just get to the Exception and then stop. Instead it does:

Parsed foo 1: 'bar1'
UnrecognizedPropertyException parsing line 2.
JsonMappingException parsing line 2.
JsonMappingException parsing line 2.

java.lang.AssertionError: Infinite loop detected.
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import org.junit.Assert;
import org.junit.Test;


public class MappingIteratorTest
{
  public static class Foo {
    public String foo;
  }

  @Test
  public void testInfiniteLoop() throws IOException {
    ObjectMapper om = new ObjectMapper();
    String input = "{\"foo\":\"bar1\"}\n{\"bad\":\"bar2\"}\n{\"foo\":\"bar3\"}";
    JsonParser jp = new JsonFactory().createParser(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)));
    MappingIterator<Foo> i = om.readValues(jp, Foo.class);
    int count = 0;
    while (i.hasNext()) {
      int lineNr = jp.getCurrentLocation().getLineNr();
      try {
        Foo f = i.next();
        System.out.println(String.format("Parsed foo %d: '%s'", lineNr, f.foo));
      }
      catch (RuntimeJsonMappingException e) {
        System.out.println(String.format("%s parsing line %d.", e.getCause().getClass().getSimpleName(), lineNr));
      }
      count++;

      if (count > 3) {
        Assert.fail("Infinite loop detected.");
      }
    }
  }
}
@cowtowncoder cowtowncoder added this to the 2.5.2 milestone Mar 25, 2015
@cowtowncoder
Copy link
Member

Minimal fix for 2.5.2, which will enforce clearing of the last token even if exception is thrown.
This will guarantee that at least one token is read, no matter what. For specific error this will likely produce "partial" objects (since part of the object remains in read stream), which may be problematic.
Caller may try recover using underlying JsonParser, since it may have more knowledge on how to re-sync content (for shallow Objects simply scan past END_OBJECT, for example)

I will also file a follow-up issue for adding better heuristics for re-sync in 2.6 (or later), which should make error recovery more convenient. But for a patch version I don't want any chance of regression, hence keeping this fix very simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants