Skip to content

Commit 0cf2e13

Browse files
leonmk-awsLeon Michalski
andauthored
fix: do not print event diff when the ids have changed (#2252)
Fixes #2248 ### Description of the bug When checking for the database diff, the `eventRootProperty` and `eventResourcesField` of events were shown as changing like: ```│ └ events │ ├[~] event aws.autoscaling@EC2InstanceLaunchLifecycleAction │ │ └ - rootProperty: EC2InstanceLaunchLifecycleAction │ │ + rootProperty: EC2InstanceLaunchLifecycleAction │ │ - resourcesField: [EC2InstanceLaunchLifecycleAction.AutoScalingGroupName] │ │ + resourcesField: [EC2InstanceLaunchLifecycleAction.AutoScalingGroupName] ``` This was caused because they used `JsonEq` for comparison, which also checked if they has the same `$id` in the db, which is not always true (e.g. when adding a new resource to the db, ids will change). ### Solution Do not compare the ids, but use the existing functions to compare the `EvenTypeDefinition` in these properties. ### Testing - Ran the diff script on databases where the ids had changed, diff is now correct. - Added a unit test Co-authored-by: Leon Michalski <[email protected]>
1 parent 41ca41e commit 0cf2e13

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/@aws-cdk/service-spec-importers/src/db-diff.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
UpdatedEvent,
2323
UpdatedEventTypeDefinition,
2424
UpdatedEventProperty,
25+
ResourceField,
2526
} from '@aws-cdk/service-spec-types';
27+
import { Reference } from '@cdklabs/tskb';
2628
import {
2729
diffByKey,
2830
collapseUndefined,
@@ -204,8 +206,8 @@ export class DbDiff {
204206
description: diffScalar(a, b, 'description'),
205207
source: diffScalar(a, b, 'source'),
206208
detailType: diffScalar(a, b, 'detailType'),
207-
resourcesField: diffField(a, b, 'resourcesField', jsonEq),
208-
rootProperty: diffField(a, b, 'rootProperty', jsonEq),
209+
resourcesField: diffField(a, b, 'resourcesField', this.eqEventResourcesField.bind(this)),
210+
rootProperty: diffField(a, b, 'rootProperty', this.eqEventRootProperty.bind(this)),
209211
typeDefinitionDiff: this.diffEventTypeDefinitions(a, b),
210212
} satisfies AllFieldsGiven<UpdatedEvent>);
211213
}
@@ -254,6 +256,23 @@ export class DbDiff {
254256
return this.stringifyGenericType(a, this.db1) === this.stringifyGenericType(b, this.db2);
255257
}
256258

259+
private eqEventRootProperty(a: Reference<EventTypeDefinition>, b: Reference<EventTypeDefinition>): boolean {
260+
return this.eqEventPropertyType({ type: 'ref', reference: a }, { type: 'ref', reference: b });
261+
}
262+
263+
private eqEventResourcesField(a: ResourceField[], b: ResourceField[]): boolean {
264+
if (a.length !== b.length) return false;
265+
for (let i = 0; i < a.length; i++) {
266+
if (
267+
!this.eqEventPropertyType({ type: 'ref', reference: a[i].type }, { type: 'ref', reference: b[i].type }) ||
268+
a[i].fieldName !== b[i].fieldName
269+
) {
270+
return false;
271+
}
272+
}
273+
return true;
274+
}
275+
257276
/**
258277
* Stringify a generic property type for comparison.
259278
*/

packages/@aws-cdk/service-spec-importers/test/db-diff.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,29 @@ test('metrics diff only on statistic and ignore dedup key for diff', () => {
4141
statistic: { new: 'Maximum', old: 'Average' },
4242
});
4343
});
44+
45+
test('event diff ignores different $ref IDs when event type definitions have same name', () => {
46+
const eventType1 = db1.allocate('eventTypeDefinition', { name: 'WorkSpacesAccess', properties: {} });
47+
const eventType2 = db2.allocate('eventTypeDefinition', { name: 'WorkSpacesAccess', properties: {} });
48+
49+
const event1 = db1.allocate('event', {
50+
name: 'aws.workspaces@WorkSpacesAccess',
51+
description: 'Test event',
52+
source: 'aws.workspaces',
53+
detailType: 'WorkSpaces Access',
54+
rootProperty: ref(eventType1),
55+
resourcesField: [{ type: ref(eventType1), fieldName: 'workspaceId' }],
56+
});
57+
58+
const event2 = db2.allocate('event', {
59+
name: 'aws.workspaces@WorkSpacesAccess',
60+
description: 'Test event',
61+
source: 'aws.workspaces',
62+
detailType: 'WorkSpaces Access',
63+
rootProperty: ref(eventType2),
64+
resourcesField: [{ type: ref(eventType2), fieldName: 'workspaceId' }],
65+
});
66+
67+
const ed = diff.diffEvent(event1, event2);
68+
expect(ed).toBeUndefined();
69+
});

0 commit comments

Comments
 (0)