Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): omit a null catalog field instead of answering it

Yudhi Armyndharisc3717f95fb86
diff --git a/src/modules/plugins/catalog.spec.ts b/src/modules/plugins/catalog.spec.ts
index f612359c9..5044c8f75 100644
--- a/src/modules/plugins/catalog.spec.ts
+++ b/src/modules/plugins/catalog.spec.ts
@@ -63,6 +63,31 @@ describe('annotateCatalog', () => {
});
});
+describe('annotateCatalog with null optional fields', () => {
+ it('omits a null optional field, as the published schema has it absent or a string', () => {
+ const [entry] = annotateCatalog(
+ [
+ {
+ id: 'a',
+ name: 'A',
+ version: '1.0.0',
+ description: null,
+ keywords: null,
+ download: null,
+ stars: null,
+ } as never,
+ ],
+ [],
+ );
+ expect(entry).not.toHaveProperty('description');
+ expect(entry).not.toHaveProperty('keywords');
+ expect(entry).not.toHaveProperty('download');
+ // Unknown extra fields are passed through as they came.
+ expect(entry).toHaveProperty('stars', null);
+ expect(entry).toMatchObject({ id: 'a', installed: false, updateAvailable: false });
+ });
+});
+
describe('isCatalogEntry', () => {
const ok = { id: 'a', name: 'A', version: '1.0.0' };
diff --git a/src/modules/plugins/catalog.ts b/src/modules/plugins/catalog.ts
index 81739fd4a..843494edd 100644
--- a/src/modules/plugins/catalog.ts
+++ b/src/modules/plugins/catalog.ts
@@ -55,6 +55,15 @@ export function isCatalogEntry(value: unknown): value is CatalogEntry {
);
}
+/** The entry without the optional fields a catalog left null, so none is answered as null where a string is declared. */
+function withoutNulls(entry: CatalogEntry): CatalogEntry {
+ const out: CatalogEntry = { ...entry };
+ for (const key of [...OPTIONAL_STRING_FIELDS, 'keywords'] as const) {
+ if (out[key] === null) delete out[key];
+ }
+ return out;
+}
+
/** A catalog entry annotated with this instance's install state. */
export interface CatalogPlugin extends CatalogEntry {
installed: boolean;
@@ -93,7 +102,7 @@ export function annotateCatalog(
const installedVersion = byId.get(entry.id) ?? null;
const cmp = installedVersion === null ? 0 : compareSemver(entry.version, installedVersion);
return {
- ...entry,
+ ...withoutNulls(entry),
installed: installedVersion !== null,
installedVersion,
updateAvailable:
diff --git a/src/modules/plugins/plugins.service.spec.ts b/src/modules/plugins/plugins.service.spec.ts
index 1b014c269..da96b5873 100644
--- a/src/modules/plugins/plugins.service.spec.ts
+++ b/src/modules/plugins/plugins.service.spec.ts
@@ -1214,7 +1214,10 @@ describe('PluginsService remote catalog', () => {
it('accepts null in optional fields', async () => {
serveOnce('[{"id":"a","name":"A","version":"1.0.0","homepage":null,"keywords":null}]');
- await expect(service().getCatalog()).resolves.toMatchObject([{ id: 'a', installed: false }]);
+ const [entry] = await service().getCatalog();
+ expect(entry).toMatchObject({ id: 'a', installed: false });
+ expect(entry).not.toHaveProperty('homepage');
+ expect(entry).not.toHaveProperty('keywords');
});
it('annotates a well-formed catalog', async () => {