Skip to content

Commit

Permalink
Fixing some review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 28, 2023
1 parent ee8c1f1 commit 53d3201
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class CustomMustacheFactory extends DefaultMustacheFactory {
static final String X_WWW_FORM_URLENCODED_MEDIA_TYPE = "application/x-www-form-urlencoded";

private static final String DEFAULT_MEDIA_TYPE = JSON_MEDIA_TYPE;
private static final boolean DEFAULT_DETECT_MISSING_PARAMS = false;

private static final Map<String, Supplier<Encoder>> ENCODERS = Map.of(
V7_JSON_MEDIA_TYPE_WITH_CHARSET,
Expand Down Expand Up @@ -366,7 +367,7 @@ public void encode(String s, Writer writer) throws IOException {
*/
static class Builder {
private String mediaType = DEFAULT_MEDIA_TYPE;
private boolean detectMissingParams = false;
private boolean detectMissingParams = DEFAULT_DETECT_MISSING_PARAMS;

private Builder() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Object coerce(Object object) {
}
}

@Override
public Wrapper find(String name, List<Object> scopes) {
Wrapper wrapper = super.find(name, scopes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ public String execute() {
try {
template.execute(writer, params);
} catch (Exception e) {
if (logException(e)) {
if (shouldLogException(e)) {
logger.error(() -> format("Error running %s", template), e);
}
throw new GeneralScriptException("Error running " + template, e);
}
return writer.toString();
}

public boolean logException(Throwable e) {
public boolean shouldLogException(Throwable e) {
if (e instanceof InvalidParameterException) {
return false;
}
return e.getCause() == null || logException(e.getCause());
return e.getCause() == null || shouldLogException(e.getCause());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ public void testDetectMissingParam() throws IOException {
}
}

public void testMissingParam() throws IOException {
Map<String, String> scriptOptions = Collections.emptyMap();
String source = "{\"match\": { \"field\": \"{{query_string}}\" }";
TemplateScript.Factory compiled = qe.compile(null, source, TemplateScript.CONTEXT, scriptOptions);

// When the DETECT_MISSING_PARAMS_OPTION is not specified, missing variable is replaced with an empty string.
assertThat(compiled.newInstance(Collections.emptyMap()).execute(), equalTo("{\"match\": { \"field\": \"\" }"));
assertThat(compiled.newInstance(null).execute(), equalTo("{\"match\": { \"field\": \"\" }"));
}

public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException {
String templateString = """
{
Expand Down

0 comments on commit 53d3201

Please sign in to comment.