fix(ec2): omit empty stateReason element in DescribeInstances (#1410)

* fix(core): use self-closing tags for empty XmlBuilder values

XmlBuilder.elem(name, "") produced <name></name> for empty string values, which caused some XML consumers (e.g. Terraform AWS provider) to crash with nil pointer dereference.

Now empty string values render as self-closing <name/> instead, which is semantically identical but avoids the parser crash. Null values remain skipped entirely.

* fix(ec2): conditionally omit stateReason instead of self-closing empty tags

The previous approach (self-closing tags for empty XmlBuilder values)
was wrong for two reasons:
1. AWS emits <tag></tag> (paired tags), not <tag/> — verified against
   botocore rest-xml protocol fixtures (XmlEmptyStrings, XmlEmptyBlobs).
2. Go's encoding/xml tokenizes both forms identically (StartElement +
   EndElement, no CharData), so the Terraform crash persisted either way.

Real fix: stop hardcoding <stateReason><code></code><message></message>
in Ec2QueryHandler for every instance. AWS omits stateReason entirely
when there is none (e.g. a running instance). Now it is only emitted
when stateReasonCode or stateReasonMessage is present.

XmlBuilder.elem() reverted to its original paired-tag behavior.
Added Instance.stateReasonCode/stateReasonMessage model fields.
Updated Ec2InstanceResponseShapeTest to assert null for running
instances (matching real AWS behavior).
This commit is contained in:
José Miguel González Guzmán authored and GitHub committed 2026-06-19 19:03:50 -05:00
1 parent 4bd43020d5
commit b4ee4ca489
3 files changed
+21 -9

No files matched your search

@@ -1659,12 +1659,14 @@ public class Ec2QueryHandler {
.end("item");
}
xml.end("networkInterfaceSet");
xml.elem("clientToken", inst.getClientToken())
.start("stateReason")
.elem("code", "")
.elem("message", "")
.end("stateReason")
.start("cpuOptions")
xml.elem("clientToken", inst.getClientToken());
if (inst.getStateReasonCode() != null || inst.getStateReasonMessage() != null) {
xml.start("stateReason")
.elem("code", inst.getStateReasonCode())
.elem("message", inst.getStateReasonMessage())
.end("stateReason");
}
xml.start("cpuOptions")
.elem("coreCount", "1")
.elem("threadsPerCore", "1")
.end("cpuOptions")
@@ -39,6 +39,8 @@ public class Instance {
private boolean ebsOptimized = false;
private boolean enaSupport = true;
private String iamInstanceProfileArn;
private String stateReasonCode;
private String stateReasonMessage;
private String region;
private List<Tag> tags = new ArrayList<>();
@@ -143,6 +145,12 @@ public class Instance {
public String getIamInstanceProfileArn() { return iamInstanceProfileArn; }
public void setIamInstanceProfileArn(String iamInstanceProfileArn) { this.iamInstanceProfileArn = iamInstanceProfileArn; }
public String getStateReasonCode() { return stateReasonCode; }
public void setStateReasonCode(String stateReasonCode) { this.stateReasonCode = stateReasonCode; }
public String getStateReasonMessage() { return stateReasonMessage; }
public void setStateReasonMessage(String stateReasonMessage) { this.stateReasonMessage = stateReasonMessage; }
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }