Skip to content

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

@lorrin

Description

@lorrin

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.");
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions