wiremock: NullPointerException in com.github.tomakehurst.wiremock.matching.EqualToJsonPattern.getNode during record

The`ret.get(keyInt) in the row return getNode(ret.get(keyInt), path, ++pos); will under certain conditions be null because keyInt is larger than the size of ret.

How to reproduce: Add the two following classes directly on the same level as com.github.tomakehurst.wiremock.WireMockServer and add compile “commons-io:commons-io:2.4” compile ‘io.rest-assured:rest-assured:3.0.3’ to build.gradle.

package com.github.tomakehurst.wiremock;

import com.github.tomakehurst.wiremock.common.SingleRootFileSource; import org.apache.commons.io.FileUtils;

import java.io.File;

import static com.github.tomakehurst.wiremock.client.WireMock.*; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

public class Buggtest {

public static String URL = "/post";
public static String PROXYBASEURL = "http://httpbin.org";


public static void main(String[] args) {
    new Buggtest().tst();
}
void tst() {
    WireMockServer wireMockServer=null;
    try {
        wireMockServer = new WireMockServer(options().port(8099));
        wireMockServer.start();
        FileUtils.cleanDirectory(new File("/tmp/mappings"));
        FileUtils.cleanDirectory(new File("/tmp/__files"));
        wireMockServer.enableRecordMappings(new SingleRootFileSource("/tmp/mappings"), new SingleRootFileSource("/tmp/__files"));
        configureFor("localhost", 8099);
        stubFor(post(urlEqualTo(URL)).willReturn(aResponse().proxiedFrom(PROXYBASEURL)));
        Caller c = new Caller();
        c.call();
        wireMockServer.stop();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    finally {
        if (wireMockServer!=null && wireMockServer.isRunning()) {
            wireMockServer.stop();
        }
    }
}

}


package com.github.tomakehurst.wiremock;

import com.jayway.jsonpath.JsonPath; import io.restassured.RestAssured; import io.restassured.response.Response;

import static io.restassured.RestAssured.given;

public class Caller { void call() {

    String[] request=new String[2];
    request[0] = "{\"columns\": [{\"name\": \"x\",\"y\": 3},{\"name\": \"agreementnumber\",\"a\": 1},{\"name\": \"agreementstatus\",\"b\": 2}]}";
    request[1] = "{\"columns\": [{\"name\": \"agreementnumber\",\"a\": 1},{\"name\": \"utilizerstatus\",\"b\": 2}]}";


    //for(int i=request.length-1;i>=0;i--) {
    for(int i=0;i<request.length;i++) {
        System.out.println("Doing request: " + request[i]);
        Response r = given().contentType("application/json").body(request[i]).when().post("http://localhost:8099"+ Buggtest.URL);
        String body = r.getBody().asString();
        System.out.println("Request response:\n------8<----------------------------" );
        System.out.println(body);
        System.out.println("--------------------------------------->8-------");

    }
}

public static void main(String[] args) {
    Caller c = new Caller();
    c.call();
}

}


The second call will result in: Error 500 Problem accessing /post. Reason: java.lang.NullPointerException

The error do not happen if you change the order of the calls (reverse the for-loop in class Caller) or if you change columns to columnsZ the second request string.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

@tomakehurst Had any time yet? 😃