question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support array responses

See original GitHub issue

A response example

[
    {
        "product_id": "efbbbf00-0000-0000-0000-000000000000",
        "description": "Incredibly fantastic product",
        "name": "Fantastic product"
    }
]

A content test

---
version: 1
tests:
  contentTests:
    - openApiOperationId: getProducts
        responseBodyTests:
          - key: "[]" # << Suggested notation, could be a dot or wathever else 
            minLength: 1
          - key: "[0].name"
            value: "Fantastic product"

Generated script

// Set response object as internal variable
let jsonData = {};
try {jsonData = pm.response.json();}catch(e){}

// Response body should have property "[]"
pm.test("[GET]::/products - Content check if property '[]' exists", function() {
   pm.expect((typeof jsonData.[] !== "undefined")).to.be.true;
// ---------------------------^ SYNTAX ERROR
});

// Response body should have a minimum length of "1" for "[]"
if (jsonData?.[]) {
// ----------^ SYNTAX ERROR
pm.test("[GET]::/products - Content check if value of '[]' has a minimum length of '1'", function() {
  pm.expect(jsonData.[].length).is.at.least(1);
// -------------------^ SYNTAX ERROR
})};

// Response body should have property "[0].name"
pm.test("[GET]::/products - Content check if property '[0].name' exists", function() {
   pm.expect((typeof jsonData.[0].name !== "undefined")).to.be.true;
// ---------------------------^ SYNTAX ERROR (see #333 )
});

// Response body should have value "Fantastic product" for "[0].name"
if (jsonData?.[0].name) {
pm.test("[GET]::/products - Content check if value for '[0].name' matches 'Fantastic product'", function() {
  pm.expect(jsonData.[0].name).to.eql("Fantastic product");
// -------------------^ SYNTAX ERROR (see #333 )
})};

Expected script

// Set response object as internal variable
let jsonData = {};
try {jsonData = pm.response.json();}catch(e){}

// Response body should be an array
pm.test("[GET]::/products - Content check if response is an array", function() {
   pm.expect(jsonData).to.be.an('array');
});

// Response body should have a minimum length of "1"
pm.test("[GET]::/products - Content check if value has a minimum length of '1'", function() {
  pm.expect(jsonData.length).is.at.least(1);
});

// Response body should have property "[0].name"
pm.test("[GET]::/products - Content check if property '[0].name' exists", function() {
   pm.expect((typeof jsonData[0].name !== "undefined")).to.be.true;
});

// Response body should have value "Fantastic product" for "[0].name"
if (jsonData?.[0].name) {
pm.test("[GET]::/products - Content check if value for '[0].name' matches 'Fantastic product'", function() {
  pm.expect(jsonData[0].name).to.eql("Fantastic product");
})};

Tested version: v1.16.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
Kallyscommented, Jun 21, 2022

@thim81 Thanks for this great job, I’ll test this as soon as possible!

1reaction
thim81commented, Jun 21, 2022

@Kallys We just release Portman v1.17, which includes the enhancement for targeting the root object or array, which should solve your reported issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support array-type response body by default #2809 - GitHub
I would like the openapiv2 and grpc-gateway plugins to support mapping an HTTP response to a repeated field, so that we can have...
Read more >
Array Response | Chainlink Documentation
Array Response. On this page. Overview; Example. This guide explains how to make an HTTP GET request to an external API, that returns...
Read more >
Different JSON array response - Stack Overflow
The JSON responses are different! The first one is an object, surrounded by { } , which contains a field "gear" that is...
Read more >
How to extract data from array within Response Body
I am trying to capture a value from the Response Body and feed it into a custom field on our incident table, something...
Read more >
JSON response starts with an array | Support Center
Hi, We have configure service rest and send out pagelist data in json format as response. We are getting the response, but the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found