From 8366ac256a498535ee1681b1c3c9d41c078d3d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=E9=9A=86=E5=BF=97?= Date: Fri, 3 Jul 2026 13:16:04 +0900 Subject: [PATCH] fix(cloudformation): register !Cidr and !GetAZs shorthand YAML tags (#1635) The Fn::Cidr and Fn::GetAZs intrinsics were added in #1365, but their YAML shorthand tags (!Cidr, !GetAZs) were never registered in CloudFormationYamlParser. Templates using the shorthand fail to parse with "could not determine a constructor for the tag !Cidr", while the long form (Fn::Cidr / Fn::GetAZs) works. Register the two missing tags alongside the other intrinsic shorthands so both forms behave identically. --- .../CloudFormationYamlParser.java | 2 + .../CloudFormationYamlParserTest.java | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParserTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParser.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParser.java index 9859a7aa6b..0bc1a7b881 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParser.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParser.java @@ -56,6 +56,8 @@ public class CloudFormationYamlParser { register("!Base64", n -> fnMap("Fn::Base64", n)); register("!FindInMap", n -> fnMap("Fn::FindInMap", n)); register("!Split", n -> fnMap("Fn::Split", n)); + register("!Cidr", n -> fnMap("Fn::Cidr", n)); + register("!GetAZs", n -> fnMap("Fn::GetAZs", n)); register("!ImportValue",n -> fnMap("Fn::ImportValue", n)); register("!Condition", n -> scalarMap("Condition", scalar(n))); register("!And", n -> fnMap("Fn::And", n)); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParserTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParserTest.java new file mode 100644 index 0000000000..905fe7b359 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationYamlParserTest.java @@ -0,0 +1,46 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CloudFormationYamlParserTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + private JsonNode parse(String yaml) throws Exception { + return new CloudFormationYamlParser(mapper).parse(yaml); + } + + @Test + void cidrShorthandExpandsToFnCidr() throws Exception { + JsonNode cidr = parse("Value: !Cidr [\"10.0.0.0/16\", 4, 8]").get("Value").get("Fn::Cidr"); + assertTrue(cidr.isArray()); + assertEquals("10.0.0.0/16", cidr.get(0).asText()); + assertEquals(4, cidr.get(1).asInt()); + assertEquals(8, cidr.get(2).asInt()); + } + + @Test + void getAzsShorthandExpandsToFnGetAZs() throws Exception { + assertEquals("", parse("Value: !GetAZs \"\"").get("Value").get("Fn::GetAZs").asText()); + } + + @Test + void cidrShorthandMatchesLongFormInNestedSelect() throws Exception { + JsonNode shorthand = parse("CidrBlock: !Select [1, !Cidr [!GetAtt Vpc.CidrBlock, 4, 8]]"); + JsonNode longForm = parse(""" + CidrBlock: + Fn::Select: + - 1 + - Fn::Cidr: + - Fn::GetAtt: [Vpc, CidrBlock] + - 4 + - 8 + """); + assertEquals(longForm, shorthand); + } +}