Skip to content

Commit

Permalink
Correctly handle optional fields in EventArc emulator (#5819)
Browse files Browse the repository at this point in the history
* Optional attributes shoud actually be optional

* add changelog

* Fix tests

* Actually fix tests
  • Loading branch information
joehan committed May 11, 2023
1 parent 2e84e94 commit f8f19dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes an issue in the EventArc emualtor where events missing optional fields would cause crashes. (#5803)
4 changes: 2 additions & 2 deletions src/emulator/eventarcEmulatorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export function cloudEventFromProtoToJson(ce: any): CloudEvent<any> {
}

function getOptionalAttribute(ce: any, attr: string, type: string): string | undefined {
return ce["attributes"][attr][type];
return ce?.["attributes"]?.[attr]?.[type];
}

function getRequiredAttribute(ce: any, attr: string, type: string): string {
const val = ce["attributes"][attr][type];
const val = ce?.["attributes"]?.[attr]?.[type];
if (val === undefined) {
throw new FirebaseError("CloudEvent must contain " + attr + " attribute");
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/emulators/eventarcEmulatorUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,41 @@ describe("eventarcEmulatorUtils", () => {
expect(got.datacontenttype).to.deep.eq("text/plain");
expect(got.data).to.eq("hello world");
});

it("allows optional attribute to not be set", () => {
expect(
cloudEventFromProtoToJson({
"@type": "type.googleapis.com/io.cloudevents.v1.CloudEvent",
attributes: {
customattr: {
ceString: "custom value",
},
datacontenttype: {
ceString: "application/json",
},
time: {
ceTimestamp: "2022-03-16T20:20:42.212Z",
},
},
id: "user-provided-id",
source: "/my/functions",
specVersion: "1.0",
textData: '{"hello":"world"}',
type: "some.custom.event",
})
).to.deep.eq({
type: "some.custom.event",
specversion: "1.0",
datacontenttype: "application/json",
id: "user-provided-id",
subject: undefined,
data: {
hello: "world",
},
source: "/my/functions",
time: "2022-03-16T20:20:42.212Z",
customattr: "custom value",
});
});
});
});

0 comments on commit f8f19dc

Please sign in to comment.