Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get request.query.size as empty value instead of length of the structure when the query parameter isn't sent

Tags:

wiremock

I am trying to dynamically generate a list with N entities, the quantity can be set using the query parameter size as the following GET /search?size=15. I want to set a default value for it when the query parameter size isn't sent as the following GET /search.

  • I am using Response Templating (therefore using Handlebars)
  • I am using WireMock Java version 3.13.1 (org.wiremock.wiremock-standalone with Maven)
  • The query parameter name can't be changed, it MUST BE size

mappings/search.json

{
  "request": {
    "method": "GET",
    "urlPath": "/search"
  },
  "response": {
    "status": 200,
    "bodyFileName": "search-response-a.json",
    "transformers": [ "response-template" ]
  }
}

I am trying to do the following

__files/search-response-a.json

{{#assign 'page-size'}}
  {{val request.query.size default='20'}}
{{/assign~}}

It works fine when the query parameter is sent. When the request GET /search?size=15 is sent the request.query.size evaluates to 15.

But it won't work as expected when the query parameter isn't sent. When the request GET /search is sent the request.query.size was expected to evaluate to the default which is 20 but it actually evaluates to the quantity of query parameters sent, 0 in this scenario.

When the request GET /search?a=1&b=2&c=3 is sent the request.query.size evaluates to 3

When I rename the query parameter to something else such as _size it works as expected but changing the name of the query parameter is not an option for my scenario.

How can I set a default value to request.query.size when the query parameter isn't sent in the request?

I tried alternative syntaxes to access the parameter but they have the same behavior

__files/search-response-b.json

{{#assign 'page-size'}}
  {{val request.query.[size] default='20'}}
{{/assign~}}

When the request GET /search is sent the request.query.size evaluates to 0 but 20 was expected.


__files/search-response-c.json

{{#assign 'page-size'}}
  {{val (lookup request.query "size") default='20'}}
{{/assign~}}

When the request GET /search?page=7&filter=lorem is sent the request.query.size and page-size evaluates to 2 but 20 was expected.


__files/search-response-d.json

{{#assign 'page-size'}}
  {{#if request.query.size}}
    {{request.query.size}}
  {{else}}
    20
  {{/if}}
{{/assign}}

When the request GET /search?filter=lorem&order=desc is sent the request.query.size and page-size evaluates to 2 but 20 was expected.

like image 347
Kodex Avatar asked Dec 21 '25 22:12

Kodex


1 Answers

Instead of using request.query.size directly, you need to access it using the lookup helper on the query parameters map:

{{#assign "page-size"}}
  {{#if (lookup request.query "size")}}
    {{lookup request.query "size"}}
  {{else}}
    20
  {{/if}}
{{/assign}}
like image 156
Lavi Kumar Avatar answered Dec 31 '25 18:12

Lavi Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!