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

IsEmpty doesn't seem to work on Object Collections #1788

Closed
taigrr opened this issue Jul 21, 2023 · 3 comments
Closed

IsEmpty doesn't seem to work on Object Collections #1788

taigrr opened this issue Jul 21, 2023 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@taigrr
Copy link

taigrr commented Jul 21, 2023

What is the current bug behavior?

I have a server returning the following JSON:
{ "a":{ "b":{ "c":{}, "d":{} } } }

And I have a hurl file with the following assertions:

# request endpoint:
POST http://server/request
{
   "x":"y",
   "z":420
}
HTTP 200
[Asserts]
jsonpath "$.a.b.c" != null
jsonpath "$.a.b.c" isCollection
jsonpath "$.a.b.c" isEmpty
  1. Set up a server to return that response
  2. Hit it with hurl hurl --test *.hurl
  3. See an assertion failure:
test.hurl: Running [1/1]
error: Assert failure
  --> test.hurl:11:0
   |
13 | jsonpath "$.a.b.c" isEmpty
   |   actual:   object
   |   expected: count equals to 0
   |   >>> types between actual and expected are not consistent
   |


test.hurl: Failure (1 request(s) in 2 ms)
--------------------------------------------------------------------------------
Executed files:  1
Succeeded files: 0 (0.0%)
Failed files:    1 (100.0%)
Duration:        2 ms

According to the documentation:

isEmpty Query returns an empty collection jsonpath "$.movies" isEmpty

Since isCollection returns true, my c object is a collection. It's clearly empty, why am I getting a type error?

What is the expected correct behavior?

I should not get a type error

Execution context

hurl 4.0.0 libcurl/8.2.0 OpenSSL/3.1.1 zlib/1.2.13 brotli/1.0.3840 zstd/1.5.5 libidn2/2.3.4 libssh2/1.11.0 nghttp2/1.55.1
Features (libcurl): alt-svc AsynchDNS brotli HSTS HTTP2 IDN IPv6 Largefile libz NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets zstd
Features (built-in): brotli

archlinux, 6.4.4-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 19 Jul 2023 19:19:38 +0000 x86_64 GNU/Linux

Possible fixes

It's possible isEmpty only works for arrays/slices, not all collection types, it looks to me like it's just implemented as an alias to count == 0. Probably need to do a type check and have multiple codepaths instead.

@taigrr taigrr added the bug Something isn't working label Jul 21, 2023
@taigrr taigrr changed the title IsEmpty doesn't seem to work on Collections IsEmpty doesn't seem to work on Object Collections Jul 21, 2023
@jcamiel jcamiel added this to the 4.1.0 milestone Jul 21, 2023
@jcamiel
Copy link
Collaborator

jcamiel commented Jul 21, 2023

Hi @taigrr thanks for you detailed report. It's effectively a bug, isEmpty should have been OK in this case.

@jcamiel jcamiel linked a pull request Jul 25, 2023 that will close this issue
@jcamiel
Copy link
Collaborator

jcamiel commented Jul 25, 2023

Thanks @taigrr for the issue, it should be fixed on master, you can test it if you know how to build a Rust application (see => Build & Test) or wait for the next Hurl release landing in September. One thing that we can improve (I just need to check with @fabricereix) is do we allow count on an object ?

POST http://server/request
{
   "x":"y",
   "z":420
}
jsonpath "$.a.b.c" != null       # OK
jsonpath "$.a.b.c" isCollection  # OK
jsonpath "$.a.b.c" isEmpty       # OK
jsonpath "$.a.b.c" count == 0    # ??

@jcamiel jcamiel closed this as completed Jul 25, 2023
@taigrr
Copy link
Author

taigrr commented Jul 25, 2023

Thanks! For my personal use case I am ok with this as-is, and I don't know of a use case for count on an object. I could check individual properties on the object, or I would want to know if it's null or empty or non-empty, but I don't know a real-world use case for count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants