Skip to content

Commit f8f19dc

Browse files
authored
Correctly handle optional fields in EventArc emulator (#5819)
* Optional attributes shoud actually be optional * add changelog * Fix tests * Actually fix tests
1 parent 2e84e94 commit f8f19dc

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixes an issue in the EventArc emualtor where events missing optional fields would cause crashes. (#5803)

Diff for: src/emulator/eventarcEmulatorUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export function cloudEventFromProtoToJson(ce: any): CloudEvent<any> {
3636
}
3737

3838
function getOptionalAttribute(ce: any, attr: string, type: string): string | undefined {
39-
return ce["attributes"][attr][type];
39+
return ce?.["attributes"]?.[attr]?.[type];
4040
}
4141

4242
function getRequiredAttribute(ce: any, attr: string, type: string): string {
43-
const val = ce["attributes"][attr][type];
43+
const val = ce?.["attributes"]?.[attr]?.[type];
4444
if (val === undefined) {
4545
throw new FirebaseError("CloudEvent must contain " + attr + " attribute");
4646
}

Diff for: src/test/emulators/eventarcEmulatorUtils.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,41 @@ describe("eventarcEmulatorUtils", () => {
122122
expect(got.datacontenttype).to.deep.eq("text/plain");
123123
expect(got.data).to.eq("hello world");
124124
});
125+
126+
it("allows optional attribute to not be set", () => {
127+
expect(
128+
cloudEventFromProtoToJson({
129+
"@type": "type.googleapis.com/io.cloudevents.v1.CloudEvent",
130+
attributes: {
131+
customattr: {
132+
ceString: "custom value",
133+
},
134+
datacontenttype: {
135+
ceString: "application/json",
136+
},
137+
time: {
138+
ceTimestamp: "2022-03-16T20:20:42.212Z",
139+
},
140+
},
141+
id: "user-provided-id",
142+
source: "/my/functions",
143+
specVersion: "1.0",
144+
textData: '{"hello":"world"}',
145+
type: "some.custom.event",
146+
})
147+
).to.deep.eq({
148+
type: "some.custom.event",
149+
specversion: "1.0",
150+
datacontenttype: "application/json",
151+
id: "user-provided-id",
152+
subject: undefined,
153+
data: {
154+
hello: "world",
155+
},
156+
source: "/my/functions",
157+
time: "2022-03-16T20:20:42.212Z",
158+
customattr: "custom value",
159+
});
160+
});
125161
});
126162
});

0 commit comments

Comments
 (0)