#3283·wiremock

Feature request: JSON-aware iteration helper for conditional elements

Author: deblocktCreated Dec 28, 2025Updated Sep 15, 2026
Labelsenhancement

Proposal

Description

When generating JSON arrays with conditional elements, there's no native way to handle comma separators properly when some elements are filtered out.

Problem

Using #each with @first/@last doesn't work when elements are conditionally rendered:

handlebars
{
  "items": [
    {{#each items}}
      {{#if this.enabled}}
        {{#unless @first}},{{/unless}}
        {"id": "{{this.id}}"}
      {{/if}}
    {{/each}}
  ]
}

If the first item is filtered out (enabled=false), the second item will still have @first=false and produce a leading comma:

Input data:

json
[
  {"id": "1", "enabled": false},
  {"id": "2", "enabled": true},
  {"id": "3", "enabled": true}
]

Output (invalid JSON):

json
{
  "items": [
    ,{"id": "2"},{"id": "3"}
  ]
}

The @first and @last variables are based on iteration index, not on whether content was actually rendered.

Expected Behavior

A way to iterate over collections and automatically handle JSON comma separators based on actually rendered elements.

Suggested Solution

A JSON-aware iteration helper that tracks whether content was emitted:

handlebars
{{#jsonEach items}}
  {{#if this.enabled}}
    {"id": "{{this.id}}"}
  {{/if}}
{{/jsonEach}}

Expected output (valid JSON):

json
{"id": "2"},{"id": "3"}

Use Case

We use WireMock for integration testing and need to generate dynamic JSON responses where array elements are conditionally included based on request parameters or other criteria. For example, filtering products based on what's in the request body:

handlebars
{{#jsonEach products}}
  {{#if (contains request.body this.id)}}
    {"id": "{{this.id}}", "name": "{{this.name}}"}
  {{/if}}
{{/jsonEach}}

Current Workaround

We maintain a custom TemplateHelperProviderExtension that extends EachHelper and adds commas only between non-blank rendered items. This works but requires deploying custom extensions, which complicates standalone WireMock usage and distributed testing scenarios.

Environment

  • WireMock version: 3.13.2
  • Java version: 21

References

No response