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

Semantic JSON assertion #2835

Open
4 tasks
marcodpt opened this issue May 24, 2024 · 5 comments
Open
4 tasks

Semantic JSON assertion #2835

marcodpt opened this issue May 24, 2024 · 5 comments
Labels
enhancement New feature or request topic: json

Comments

@marcodpt
Copy link

Problem to solve

Firstly, I would like to thank this wonderful project and the efforts of its developers in the open source community.

I consider HURL excellent for testing Rest API.

However, the tests themselves could function as documentation if written well.

One of the things that prevents me from doing this is being forced to put unformatted JSON in the tests.

Another problem that it generates is that any ordering change in the properties and we have to correct all the tests.

These seem like simple problems, but they become serious and impeding as the number of tests increases and the complexity of the JSON API data increases.

This problem has already been mentioned and discussed: #272 #270

Proposal

My proposal is to ignore the problem of semantic validation of XML for now and start by solving the problem of semantic validation of JSON.

Libraries like serde json create the Value structure that allows you to compare JSON semantically.

Multiline body starting with ```json should be parsed with the library and the server response as well.

assert_eq!(
    test_multi_line_json.parse::<serde_json::Value>().unwrap(),
    server_response.parse::<serde_json::Value>().unwrap()
);

Additional context and resources

serde_json

JSON semantic compare in rust

Tasks to complete

  • parse with serde json the expected multiline JSON defined in tests.hurl
  • parse the response body
  • in case of any error of parsing the test fail
  • compare both Value with the simple eq operator in rust
@marcodpt marcodpt added the enhancement New feature or request label May 24, 2024
@jcamiel
Copy link
Collaborator

jcamiel commented May 24, 2024

Hi @marcodpt

We're currently preparing the work for better error reporting and this work is the route to semantic difference.
A first batch of refacto is to be able to able to show better diff with multiline ```

The multiline check will not be semantic, whereas the JSON (and XML) body will be semantic. This way, user could choose to have semantic or "raw" diff.

To be clear (unless I'm mistaken of what we want @fabricereix )

Not semantic, raw diff, whitespaces sensitive:

GET https://foo.com/json
HTTP 200
```json
{ 
  "bar": true
}
```
GET https://foo.com/csv
HTTP 200
```
aaa,bbb,ccc
aaa,bbb,ccc
```

=> current work is to improve the error diag

Semantic, whitespaces unsensitive:

GET https://foo.com/json
HTTP 200
{ 
  "bar": true
}

@fabricereix
Copy link
Collaborator

This one is clearly not semantic

```
{ 
  "bar": true
}
```

but I'm not sure when the string type json is specified.

```json
{ 
  "bar": true
}
```

Setting such a request body adds automatically the `Content-Type: application/json' header.
So maybe this one should also be semantic.

@jcamiel
Copy link
Collaborator

jcamiel commented May 24, 2024

Yes it makes sens

@marcodpt
Copy link
Author

marcodpt commented May 24, 2024

I would like to thank you very much for your feedback.

I wasn't trying to argue about whether ```json should be semantic or not.

Actually, to be honest, I had forgotten that sending a direct json without a backtick works.

What I really miss is JSON semantic validation, which will allow me to migrate tests and documentation to the .hurl file.

I don't work much with XML in rust, but JSON seems like a not very difficult problem, but I don't really understand anything about the internal workings of hurl.

Semantic validation, for example, allows you to define the specification of an API in a .hurl file and at the same time serve as tests.

That in itself is beautiful, in the file format you define.

@marcodpt
Copy link
Author

I work a lot writing servers using Rest API and servers for communicating with IOT devices.

I consider that .hurl files would be perfect for defining an API specification.

But without semantic validation the work becomes much more difficult, and the idea of ​​freezing a specification and building servers is a little weakened if I constantly have to change the .hurl files due to a change in order in the properties or if the files are unreadable by human beings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request topic: json
Projects
None yet
Development

No branches or pull requests

3 participants