Skip to content

Commit 1882d95

Browse files
committed
Fix NPE in startswith condition if no default is provided
1 parent facfea6 commit 1882d95

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/main/java/com/bertramlabs/plugins/hcl4j/HCLBaseFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void registerBaseFunctions(HCLParser parser) {
162162
});
163163

164164
parser.registerFunction("startswith", (arguments) -> {
165-
if(arguments.size() == 2) {
165+
if(arguments.size() == 2 && arguments.get(0) != null && arguments.get(1) != null) {
166166
String prefix = (String)(arguments.get(1));
167167
String value = (String)(arguments.get(0));
168168
return value.startsWith(prefix);

src/test/groovy/com/bertramlabs/plugins/hcl4j/HCLParserSpec.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,46 @@ resource "aws_instance" "bw-instance-1" {
17371737
results.resource["aws_instance"]["bw-instance-1"] != null
17381738

17391739
}
1740+
1741+
void "should parse startswith correctly if no default is provided from hcl"() {
1742+
given:
1743+
1744+
def hcl = '''
1745+
variable "server_id" {
1746+
type = string
1747+
description = "Server ID"
1748+
validation {
1749+
condition = length(var.server_id) == 19
1750+
error_message = "Server ID must be 19 characters long"
1751+
}
1752+
validation {
1753+
condition = startswith(var.server_id, "s-")
1754+
error_message = "Server ID must start with 's-'"
1755+
}
1756+
validation {
1757+
condition = lower(var.server_id) == var.server_id
1758+
error_message = "Server ID must be lowercase"
1759+
}
1760+
}
1761+
'''
1762+
HCLParser parser = new HCLParser();
1763+
when:
1764+
def results = parser.parse(hcl)
1765+
then:
1766+
results.containsKey('variable')
1767+
results.variable.containsKey("server_id")
1768+
results.variable.server_id.type.name == "string"
1769+
results.variable.server_id.description == "Server ID"
1770+
results.variable.server_id.validation.size() == 3
1771+
results.variable.server_id.validation[0].condition == false
1772+
results.variable.server_id.validation[0].error_message == "Server ID must be 19 characters long"
1773+
results.variable.server_id.validation[1].condition == null
1774+
results.variable.server_id.validation[1].error_message == "Server ID must start with 's-'"
1775+
results.variable.server_id.validation[2].condition == true
1776+
results.variable.server_id.validation[2].error_message == "Server ID must be lowercase"
1777+
1778+
}
1779+
17401780
}
17411781

17421782

0 commit comments

Comments
 (0)