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

json resource array index access not working #1560

Closed
schegi opened this issue Mar 14, 2017 · 2 comments · Fixed by #1561
Closed

json resource array index access not working #1560

schegi opened this issue Mar 14, 2017 · 2 comments · Fixed by #1561

Comments

@schegi
Copy link

schegi commented Mar 14, 2017

Description

Currently trying to use the json resource. Array access via index is not working as described in the documentation.

I got the following json in test.json

{
  "hash":{
    "subhash":{
      "key": "value"
    }
  }
  "array": [
    "zero",
    "one",
    "three"
  ]
}

and the following control:

control 'test' do
  impact 1.0
  title 'test'
  desc '
   Test
  '

  describe file('/root/test.json') do
    it { should be_file }
  end

  describe json('/root/test.json') do
          its(['hash','subhash','key']) {should eq "value"}
          its(['array']) { should include "zero" }
          its(['array',0]) { should eq "zero" }
  end
end

And i get the following output:

×  test: Test (1 failed)
     ✔  File /root/test.json should be file
     ✔  Json /root/test.json ["hash", "subhash", "key"] should eq "value"
     ✔  Json /root/test.json ["array"] should include "zero"
     ×  Json /root/test.json ["array", 0] should eq "zero"

     expected: "zero"
          got: [nil, nil, nil]

     (compared using ==)

     Diff:
     @@ -1,2 +1,2 @@
     -"zero"
     +[nil, nil, nil]

Hash access and access to the array node itself is working as expected but indexed access to the array is not working, only returning nil objects.

InSpec and Platform Version

inspec version 1.16.1

@adamleff adamleff self-assigned this Mar 14, 2017
@adamleff
Copy link
Contributor

@schegi agreed, it does appear that something is incorrect with the implementation or possibly the docs.

If you only care about the first or last element, you can use the first and last methods:

describe json('/tmp/test.json') do
   its('array.first') { should eq 'zero' }
   its('array.last') { should eq 'two' }
end

However, accessing the array items by index number does appear to be taking the full array contents and then replacing the values with nil instead. I'm not sure why and need to go dig in the code.

I hope this workaround helps for now, and I'll update this issue when I have time to dig in and take a look.

Thanks,
~Adam

@adamleff
Copy link
Contributor

I believe I have found the bug, and I'm putting up a PR shortly to fix it and to add some tests to the ObjectTraverser class that is responsible for this functionality.

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

Successfully merging a pull request may close this issue.

3 participants