@intentius/chant-lexicon-aws 0.18.16 → 0.18.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import { awsSerializer } from "./serializer";
3
3
  import { AttrRef } from "@intentius/chant/attrref";
4
4
  import { DECLARABLE_MARKER, type Declarable } from "@intentius/chant/declarable";
5
5
  import { LexiconOutput } from "@intentius/chant/lexicon-output";
6
- import { Sub } from "./intrinsics";
6
+ import { Sub, Join } from "./intrinsics";
7
7
  import { AWS } from "./pseudo";
8
8
  import { nestedStack, NestedStackOutputRef } from "./nested-stack";
9
9
  import { stackOutput } from "@intentius/chant/stack-output";
@@ -262,6 +262,23 @@ describe("intrinsic serialization", () => {
262
262
  });
263
263
 
264
264
  describe("LexiconOutput serialization", () => {
265
+ test("resolves AttrRef nested in a Join output to Fn::GetAtt, not the __attrRef envelope (chant#935)", () => {
266
+ const s1 = new MockBucket({ BucketName: "subnet-1" });
267
+ const s2 = new MockBucket({ BucketName: "subnet-2" });
268
+ (s1.arn as Record<string, unknown>)._setLogicalName("Subnet1");
269
+ (s2.arn as Record<string, unknown>)._setLogicalName("Subnet2");
270
+ const joined = new LexiconOutput(Join(":", [s1.arn, s2.arn]), "SubnetIds");
271
+ joined._setSourceEntity("Subnet1");
272
+ const entities = new Map<string, Declarable>();
273
+ entities.set("Subnet1", s1);
274
+ entities.set("Subnet2", s2);
275
+ const template = JSON.parse(awsSerializer.serialize(entities, [joined]));
276
+ const value = JSON.stringify(template.Outputs.SubnetIds.Value);
277
+ expect(value).not.toContain("__attrRef");
278
+ expect(value).toContain("Fn::GetAtt");
279
+ expect(value).toContain("Fn::Join");
280
+ });
281
+
265
282
  test("generates CF Outputs section for LexiconOutputs", () => {
266
283
  const bucket = new MockBucket({ BucketName: "data-bucket" });
267
284
  const lexiconOutput = new LexiconOutput(bucket.arn, "DataBucketArn");
@@ -305,6 +322,23 @@ describe("LexiconOutput serialization", () => {
305
322
  });
306
323
  });
307
324
 
325
+ test("resolves an AttrRef nested in an intrinsic output → Fn::GetAtt, not __attrRef (#935)", () => {
326
+ const b1 = new MockBucket({ BucketName: "b1" });
327
+ const b2 = new MockBucket({ BucketName: "b2" });
328
+ const r1 = new AttrRef(b1, "Arn"); r1._setLogicalName("B1");
329
+ const r2 = new AttrRef(b2, "Arn"); r2._setLogicalName("B2");
330
+ const joined = new LexiconOutput(Join(",", [r1, r2]), "oArns");
331
+
332
+ const entities = new Map<string, Declarable>();
333
+ entities.set("B1", b1);
334
+ entities.set("B2", b2);
335
+
336
+ const template = JSON.parse(awsSerializer.serialize(entities, [joined]) as string);
337
+ expect(template.Outputs.oArns.Value).toEqual({
338
+ "Fn::Join": [",", [{ "Fn::GetAtt": ["B1", "Arn"] }, { "Fn::GetAtt": ["B2", "Arn"] }]],
339
+ });
340
+ });
341
+
308
342
  test("omits Outputs section when no LexiconOutputs provided", () => {
309
343
  const entities = new Map<string, Declarable>();
310
344
  entities.set("MyBucket", new MockBucket({ BucketName: "bucket" }));
@@ -364,6 +398,23 @@ describe("stackOutput serialization", () => {
364
398
  "Fn::GetAtt": ["MyBucket", "Arn"],
365
399
  });
366
400
  });
401
+
402
+ test("intrinsic-wrapped output (Join over a ref) → Fn::Join + Fn::GetAtt (#517)", () => {
403
+ const bucket = new MockBucket({ BucketName: "my-bucket" });
404
+ // Deliberately NOT pre-resolved — the serializer must set the nested ref's
405
+ // logical name from the entity map (resolveAttrRefs doesn't reach it here).
406
+ const arnRef = new AttrRef(bucket, "Arn");
407
+ const output = stackOutput(Join(",", arnRef));
408
+
409
+ const entities = new Map<string, Declarable>();
410
+ entities.set("MyBucket", bucket);
411
+ entities.set("MyBucketJoined", output as unknown as Declarable);
412
+
413
+ const template = JSON.parse(awsSerializer.serialize(entities) as string);
414
+ expect(template.Outputs.MyBucketJoined.Value).toEqual({
415
+ "Fn::Join": [",", { "Fn::GetAtt": ["MyBucket", "Arn"] }],
416
+ });
417
+ });
367
418
  });
368
419
 
369
420
  // ── Nested Stack Serialization ──────────────────────────
package/src/serializer.ts CHANGED
@@ -7,6 +7,7 @@ import type { LexiconOutput } from "@intentius/chant/lexicon-output";
7
7
  import { walkValue, type SerializerVisitor } from "@intentius/chant/serializer-walker";
8
8
  import { isChildProject, type ChildProjectInstance } from "@intentius/chant/child-project";
9
9
  import { isStackOutput, type StackOutput } from "@intentius/chant/stack-output";
10
+ import { isAttrRefLike } from "@intentius/chant/utils";
10
11
  import { resolveDependsOn } from "@intentius/chant/resource-attributes";
11
12
  import { isDefaultTags, type TagEntry } from "./default-tags";
12
13
  import { loadTaggableResources } from "./taggable";
@@ -101,6 +102,31 @@ function toCFValue(value: unknown, entityNames: Map<Declarable, string>): unknow
101
102
  return walkValue(value, entityNames, cfnVisitor(entityNames));
102
103
  }
103
104
 
105
+ /**
106
+ * Set logical names on any AttrRefs nested inside a value (e.g. inside a `Join`
107
+ * that a `stackOutput` exports). resolveAttrRefs only reaches entity attributes,
108
+ * not refs buried in an output's intrinsic — without this the walker would throw
109
+ * "logical name not set" for them (#517).
110
+ */
111
+ function resolveNestedAttrRefs(
112
+ value: unknown,
113
+ entityNames: Map<Declarable, string>,
114
+ seen = new Set<unknown>(),
115
+ ): void {
116
+ if (value === null || typeof value !== "object" || seen.has(value)) return;
117
+ seen.add(value);
118
+ if (isAttrRefLike(value)) {
119
+ if (!value.getLogicalName()) {
120
+ const parent = value.parent.deref();
121
+ const parentName = parent ? entityNames.get(parent as Declarable) : undefined;
122
+ if (parentName) value._setLogicalName(parentName);
123
+ }
124
+ return;
125
+ }
126
+ const children = Array.isArray(value) ? value : Object.values(value as Record<string, unknown>);
127
+ for (const child of children) resolveNestedAttrRefs(child, entityNames, seen);
128
+ }
129
+
104
130
  /**
105
131
  * Convert entity props to CF properties
106
132
  */
@@ -289,31 +315,42 @@ function serializeToTemplate(
289
315
  template.Outputs = {};
290
316
  }
291
317
  const stackOutput = entity as StackOutput;
292
- const ref = stackOutput.sourceRef;
293
- const logicalName = ref.getLogicalName();
294
- if (logicalName) {
318
+ // Typed `unknown` so `isAttrRefLike` narrows cleanly regardless of how the
319
+ // AttrRef type resolves across the workspace/published boundary.
320
+ const ref: unknown = stackOutput.sourceRef;
321
+ let value: unknown;
322
+ if (isAttrRefLike(ref)) {
323
+ const logicalName = ref.getLogicalName();
324
+ if (!logicalName) continue;
295
325
  // Use Ref for primary identifier ("Id") since not all resources
296
326
  // support Fn::GetAtt for their primary identifier (e.g. ACM Certificate).
297
327
  // Ref always returns the primary identifier for any CF resource.
298
- const output: CFOutput = {
299
- Value: ref.attribute === "Id"
300
- ? { Ref: logicalName }
301
- : { "Fn::GetAtt": [logicalName, ref.attribute] },
302
- };
303
- if (stackOutput.description) {
304
- output.Description = stackOutput.description;
305
- }
306
- template.Outputs[name] = output;
328
+ value = ref.attribute === "Id" ? { Ref: logicalName } : { "Fn::GetAtt": [logicalName, ref.attribute] };
329
+ } else {
330
+ // An intrinsic wrapping refs (e.g. Join(",", zone.NameServers), #517):
331
+ // resolve the nested AttrRefs' logical names from the entity map, then
332
+ // serialize through the CF walker → {Fn::Join:[",",{Fn::GetAtt:[…]}]}.
333
+ resolveNestedAttrRefs(ref, entityNames);
334
+ value = toCFValue(ref, entityNames);
335
+ }
336
+ const output: CFOutput = { Value: value };
337
+ if (stackOutput.description) {
338
+ output.Description = stackOutput.description;
307
339
  }
340
+ template.Outputs[name] = output;
308
341
  }
309
342
  }
310
343
 
311
- // Add CF Outputs for LexiconOutputs produced by this lexicon
344
+ // Add CF Outputs for LexiconOutputs produced by this lexicon. Run the value
345
+ // through the CF walker so an AttrRef nested in an intrinsic (e.g.
346
+ // Join(",", [subnet1.SubnetId, …])) becomes Fn::GetAtt instead of leaking the
347
+ // generic `{__attrRef}` envelope — the same conversion resource Properties get
348
+ // (#935). A bare Fn::GetAtt value walks through unchanged.
312
349
  if (outputs && outputs.length > 0) {
313
350
  template.Outputs = template.Outputs ?? {};
314
351
  for (const output of outputs) {
315
352
  template.Outputs[output.outputName] = {
316
- Value: output.getOutputValue(),
353
+ Value: toCFValue(output.getOutputValue(), entityNames),
317
354
  };
318
355
  }
319
356
  }