From aa79d6286a1552e5107877fd66b3f414be5e4640 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 24 Nov 2026 22:32:17 +0100 Subject: [PATCH] fix(paddle): read the Geofabrik region list again The admin's paddle region picker failed with "The list of regions cannot be fetched right now". Geofabrik's index now writes a continent's parent as null instead of leaving the key out, and the schema took only a string or nothing, so the whole list was refused over nine entries. The optional fields now take null as well, and the entries are read one by one: an entry in a shape TREK does not know costs only that region and is counted in a warning. A list of which nothing can be read is still refused. Closes #3195 --- .../nest/paddle-obstacles/geofabrik.client.ts | 44 ++++++++++++------- .../paddle-obstacles/geofabrik.client.test.ts | 39 ++++++++++++++++ 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/server/src/nest/paddle-obstacles/geofabrik.client.ts b/server/src/nest/paddle-obstacles/geofabrik.client.ts index 5a10aaa96..2792b4e6f 100644 --- a/server/src/nest/paddle-obstacles/geofabrik.client.ts +++ b/server/src/nest/paddle-obstacles/geofabrik.client.ts @@ -30,20 +30,23 @@ export const MAX_EXTRACT_BYTES = 4 * 1024 * 1024 * 1024; const DOWNLOAD_TIMEOUT_MS = 60 * 60 * 1000; const MAX_REDIRECTS = 3; -const indexSchema = z.object({ - features: z.array( - z.object({ - properties: z.object({ - id: z.string(), - parent: z.string().optional(), - name: z.string(), - urls: z.object({ pbf: z.string().optional() }).passthrough(), - }), - geometry: z.unknown(), - }), - ), +/** + * One entry of the list. Geofabrik writes an absent value as null (a + * continent's `parent`), so every optional field takes both spellings. + */ +const featureSchema = z.object({ + properties: z.object({ + id: z.string(), + parent: z.string().nullish(), + name: z.string(), + urls: z.object({ pbf: z.string().nullish() }).passthrough(), + }), + geometry: z.unknown(), }); +/** The list itself; its entries are read one by one, so one odd entry costs only that region. */ +const indexSchema = z.object({ features: z.array(z.unknown()) }); + export interface GeofabrikRegion { id: string; name: string; @@ -98,10 +101,16 @@ export class GeofabrikClient { const parsed = indexSchema.safeParse(await readCappedJson(response, MAX_INDEX_BYTES)); if (!parsed.success) throw new Error('Geofabrik index is not the list of extracts'); const regions = new Map(); - for (const feature of parsed.data.features) { - const { id, name, parent, urls } = feature.properties; + let unreadable = 0; + for (const entry of parsed.data.features) { + const feature = featureSchema.safeParse(entry); + if (!feature.success) { + unreadable += 1; + continue; + } + const { id, name, parent, urls } = feature.data.properties; if (!urls.pbf || !isExtractUrl(urls.pbf)) continue; - const boundary = paddleRegionBoundarySchema.safeParse(feature.geometry); + const boundary = paddleRegionBoundarySchema.safeParse(feature.data.geometry); regions.set(id, { id, name, @@ -110,6 +119,11 @@ export class GeofabrikClient { boundary: boundary.success ? boundary.data : null, }); } + // Entries none of which can be read are a list in another shape, not an empty catalogue. + if (unreadable > 0 && unreadable === parsed.data.features.length) { + throw new Error('Geofabrik index is not the list of extracts'); + } + if (unreadable > 0) logWarn(`[Paddle] Geofabrik index: skipped ${unreadable} entries it could not read`); this.index = { regions, fetchedAt: now }; return regions; } diff --git a/server/tests/unit/nest/paddle-obstacles/geofabrik.client.test.ts b/server/tests/unit/nest/paddle-obstacles/geofabrik.client.test.ts index e68707442..ef2805ed8 100644 --- a/server/tests/unit/nest/paddle-obstacles/geofabrik.client.test.ts +++ b/server/tests/unit/nest/paddle-obstacles/geofabrik.client.test.ts @@ -123,6 +123,45 @@ describe('GeofabrikClient.regions', () => { expect(fetchMock).toHaveBeenCalledTimes(2); }); + it('GEOF-016: reads the list Geofabrik serves with null for a top level parent or a missing extract (#3195)', async () => { + // The live index names a continent's parent as null rather than leaving the key out. + const live = { + type: 'FeatureCollection', + features: [ + { + properties: { + id: 'europe', + parent: null, + name: 'Europe', + urls: { pbf: `${GEOFABRIK_HOST}europe-latest.osm.pbf` }, + }, + geometry: null, + }, + { + properties: { id: 'oberpfalz', parent: 'bayern', name: 'Oberpfalz', urls: { pbf: EXTRACT } }, + geometry: square, + }, + { properties: { id: 'nopbf', parent: 'europe', name: 'No pbf', urls: { pbf: null } }, geometry: null }, + ], + }; + fetchMock.mockResolvedValueOnce(json(live)); + + const regions = await client.regions(1000); + + expect([...regions.keys()]).toEqual(['europe', 'oberpfalz']); + expect(regions.get('europe')?.parent).toBeNull(); + expect(regions.get('oberpfalz')?.parent).toBe('bayern'); + }); + + it('GEOF-017: skips an entry it cannot read and keeps the rest, but refuses a list of nothing readable', async () => { + const odd = { properties: { id: 42, name: 'Odd', urls: { pbf: EXTRACT } }, geometry: null }; + fetchMock.mockResolvedValueOnce(json({ features: [...index.features, odd] })); + expect([...(await client.regions(1000)).keys()]).toEqual(['bayern', 'oberpfalz']); + + fetchMock.mockResolvedValueOnce(json({ features: [odd, odd] })); + await expect(new GeofabrikClient().regions()).rejects.toThrow('Geofabrik index is not the list of extracts'); + }); + it('GEOF-004: refuses an answer that is not the list, or not a success', async () => { fetchMock.mockResolvedValueOnce(json({ type: 'FeatureCollection' })); await expect(client.regions()).rejects.toThrow('Geofabrik index is not the list of extracts');