Skip to content

Commit 5f5c1e4

Browse files
committed
Defend entity type lookups against nullish IDs
Make the generated getEntityTypeById() helper return undefined for unexpected nullish inputs instead of crashing on href access. Also add a regression test for nullish calls and refresh the generator snapshots. #681 Assisted-by: OpenCode:gpt-5.4
1 parent ce155c3 commit 5f5c1e4

6 files changed

Lines changed: 13 additions & 5 deletions

File tree

packages/vocab-tools/src/__snapshots__/class.test.ts.deno.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95871,7 +95871,7 @@ export function isEntityType(value: unknown): value is \$EntityType {
9587195871
* Gets the generated vocabulary entity class for the given type URI.
9587295872
*/
9587395873
export function getEntityTypeById(id: string | URL): \$EntityType | undefined {
95874-
return entityTypeIds.get(typeof id === \\"string\\" ? id : id.href);
95874+
return entityTypeIds.get(typeof id === \\"string\\" ? id : id?.href);
9587595875
}
9587695876

9587795877
"

packages/vocab-tools/src/__snapshots__/class.test.ts.node.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95869,7 +95869,7 @@ export function isEntityType(value: unknown): value is $EntityType {
9586995869
* Gets the generated vocabulary entity class for the given type URI.
9587095870
*/
9587195871
export function getEntityTypeById(id: string | URL): $EntityType | undefined {
95872-
return entityTypeIds.get(typeof id === \\"string\\" ? id : id.href);
95872+
return entityTypeIds.get(typeof id === \\"string\\" ? id : id?.href);
9587395873
}
9587495874

9587595875
"

packages/vocab-tools/src/__snapshots__/class.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95871,7 +95871,7 @@ export function isEntityType(value: unknown): value is $EntityType {
9587195871
* Gets the generated vocabulary entity class for the given type URI.
9587295872
*/
9587395873
export function getEntityTypeById(id: string | URL): $EntityType | undefined {
95874-
return entityTypeIds.get(typeof id === "string" ? id : id.href);
95874+
return entityTypeIds.get(typeof id === "string" ? id : id?.href);
9587595875
}
9587695876

9587795877
"

packages/vocab-tools/src/class.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ test("generateClasses() emits $EntityType helpers for fedify:vocabEntityType", a
109109
);
110110
match(
111111
entireCode,
112-
/return entityTypeIds\.get\(typeof id === "string" \? id : id\.href\);/,
112+
/return entityTypeIds\.get\(typeof id === "string" \? id : id\?\.href\);/,
113113
);
114114
});
115115

packages/vocab-tools/src/class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function isEntityType(value: unknown): value is $EntityType {
153153
* Gets the generated vocabulary entity class for the given type URI.
154154
*/
155155
export function getEntityTypeById(id: string | URL): $EntityType | undefined {
156-
return entityTypeIds.get(typeof id === "string" ? id : id.href);
156+
return entityTypeIds.get(typeof id === "string" ? id : id?.href);
157157
}
158158
159159
`;

packages/vocab/src/type.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ test("entity type helpers", () => {
2525
deepStrictEqual(getEntityTypeById(Person.typeId), Person);
2626
deepStrictEqual(getEntityTypeById(Person.typeId.href), Person);
2727
deepStrictEqual(getEntityTypeById(Link.typeId), undefined);
28+
deepStrictEqual(
29+
getEntityTypeById(null as unknown as string | URL),
30+
undefined,
31+
);
32+
deepStrictEqual(
33+
getEntityTypeById(undefined as unknown as string | URL),
34+
undefined,
35+
);
2836
});

0 commit comments

Comments
 (0)