Skip to content

Commit

Permalink
correct handling of empty request body with Handlebars (wiremock#2546) (
Browse files Browse the repository at this point in the history
wiremock#2552)

Instead of having `jsonpath` in handlebars throwing an `IllegalArgumentException`,
empty bodies are handled the same way as `null` bodies: as empty string
  • Loading branch information
dirkbolte authored Jan 15, 2024
1 parent a14e7f5 commit 3b601c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2023 Thomas Akehurst
* Copyright (C) 2017-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,7 +37,7 @@ public class HandlebarsJsonPathHelper extends HandlebarsHelper<Object> {

@Override
public Object apply(final Object input, final Options options) throws IOException {
if (input == null) {
if (input == null || (input instanceof String && ((String) input).isEmpty())) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2023 Thomas Akehurst
* Copyright (C) 2017-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,7 +62,7 @@ public void mergesASimpleValueFromRequestIntoResponseBody() {
}

@Test
public void incluesAnErrorInTheResponseBodyWhenTheJsonPathIsInvalid() {
public void includesAnErrorInTheResponseBodyWhenTheJsonPathIsInvalid() {
final ResponseDefinition responseDefinition =
transform(
transformer,
Expand Down Expand Up @@ -264,6 +264,11 @@ public void rendersAnEmptyStringWhenJsonIsNull() {
testHelperError(helper, null, "$.test", is(""));
}

@Test
public void rendersAnEmptyStringWhenJsonIsEmptyString() {
testHelperError(helper, "", "$.test", is(""));
}

@Test
public void rendersAMeaningfulErrorWhenJsonPathIsNull() {
testHelperError(
Expand Down

0 comments on commit 3b601c9

Please sign in to comment.