janus icon indicating copy to clipboard operation
janus copied to clipboard

Can't get :of-type :array to work

Open ulsa opened this issue 12 years ago • 3 comments

According to the json_path tests, this is how to get an array:

  (at-path "$.foo[*]" {:foo ["a", "b", "c"]}) => ["a", "b", "c"]

Whenever I select an element that is an array, it looks like the verify-document function calls verify-clause on each of the elements, checking if the element is of type array, which it isn't.

Output:

$ java -jar janus-0.0.2-standalone.jar --verify myservice.jns
verify-document: doc {"links":[{"href":"http://myservice.example.com/v3/channels","rel":"channels"},{"href":"http://config.example.com/v1/","rel":"config"},{"href":"http://myservice.example.com/v3/","rel":"self"}]} clauses ([:path $.links[*] :of-type :array])
  verify-seq: actual-seq [{:href http://myservice.example.com/v3/channels, :rel channels} {:href http://config.example.com/v1/, :rel config} {:href http://myservice.example.com/v3/, :rel self}] clause [:path $.links[*] :of-type :array]
    verify-clause: value {:href http://myservice.example.com/v3/channels, :rel channels} clause [:path $.links[*] :of-type :array]
of-type: actual {:href http://myservice.example.com/v3/channels, :rel channels}
    verify-clause: value {:href http://config.example.com/v1/, :rel config} clause [:path $.links[*] :of-type :array]
of-type: actual {:href http://config.example.com/v1/, :rel config}
    verify-clause: value {:href http://v3/, :rel self} clause [:path $.links[*] :of-type :array]
of-type: actual {:href http://myservice.example.com/v3/, :rel self}
1 service (1 failed)
My Service:
    root resource:
        Expected "{:href "http://myservice.example.com/v3/channels", :rel "channels"}" to be array, at path $.links[*]
        Expected "{:href "http://config.example.com/v1/", :rel "config"}" to be array, at path $.links[*]
        Expected "{:href "http://myservice.example.com/v3/", :rel "self"}" to be array, at path $.links[*]

myservice.jns:

(service
 "My Service"

 (contract "root resource"
           (method :get)
           (url "http://myservice.example.com/v3/?param1=a&param2=24")
           (header "Accept-Language" "en_US")
           (header "Accept" "application/json")

           (should-have :path "$.links[*]" :of-type :array)))

Response:

{
  "links": [
    {
      "href": "http://myservice.example.com/v3/channels",
      "rel": "channels"
    },
    {
      "href": "http://config.example.com/v1/",
      "rel": "config"
    },
    {
      "href": "http://myservice.example.com/v3/",
      "rel": "self"
    }
  ]
}

ulsa avatar Nov 27 '13 13:11 ulsa

I'll have a closer look later, but what does the following do?

(should-have :path "$.links" :of-type :array)

I vaguely recall encountering this myself. The intention is that the path $.links[*] selects everything under the links key. And then applies the checking clause to that set. You're interested in selecting the links key itself, which you should be able to do with just $.links.

gga avatar Nov 27 '13 14:11 gga

I tried that also. The result is the same, I believe:

1 service (1 failed)
My Service:
    root resource:
        Expected "{:href "http://myservice.example.com/v3/channels", :rel "channels"}" to be array, at path $.links
        Expected "{:href "http://config.example.com/v1/", :rel "config"}" to be array, at path $.links
        Expected "{:href "http://myservice.example.com/v3/", :rel "self"}" to be array, at path $.links

ulsa avatar Nov 27 '13 16:11 ulsa

Ahhh well. It was a bit much to hope it would be that simple. From looking at your example above, I think it's json-path that's getting it wrong. That example should instead return [["a" "b" "c"]]. That is, a list of lists.

From memory, the problem is that janus can't distinguish between when a path matches a set of items (returned as a list) and when a path matches a single key that is an array.

Hmmm… maybe this would all disappear if json-path were to always return multiple matches as an actual set?

gga avatar Nov 28 '13 10:11 gga