简体   繁体   中英

Rest-Assured IllegalArgumentException when JSON key starts with number

I am making a request to my server and the JSON response looks as follows:

{
   "6269f15a0bb9b1b7d86ae718e84cddcd" : {
            "attr1":"val1",
            "attr2":"val2",
                   .
                   .
            "attrx":"valx",
   }
}

The key is an MD5 hash of a "user" (irrelevant for this discussion). I cannot change this key at this point.

When I run a rest-assured test, where my request object looks like

{ 
  "fields": [ "attr1","attr2",...,"attr3" ]
  "users": [
              {
                 "userId": "2f605845757870234d94ae14ca83c660"
              }
           ]
}

When I run the following I get a run-time error

Response response = 
                given().
                       contentType("application/json").
                       request().body(requestObj).
                when().
                       post(ipAddress).   
                then().
                       extract().response(); 


response.
         then().
                body("6269f15a0bb9b1b7d86ae718e84cddcd.attr1",equalTo("blah"));


java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: 6269f @ line 1, column 27.
                        6269f15a0bb9b1b7d86ae718e84cddcd
                         ^

 1 error

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at com.jayway.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:51)
at com.jayway.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at com.jayway.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:31)
at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:183)
at com.nxn.AutomationTests.MDXTest.testSample(MDXTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Now, I assume that this is because the JSON gets deserialized into a Java class and when I ask for value of attr1 it does a class look up which fails since the class names can't start with numbers. Does anyone know how I get around this issue?

This is actually a bug! I've added it to the issue tracker ( https://code.google.com/p/rest-assured/issues/detail?id=318 ).

A work-around is to escape the attribute name:

.. .then().body("\"6269f15a0bb9b1b7d86ae718e84cddcd\".attr1"), equalTo("val1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM