Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(paddle): read the Geofabrik region list again

Mauriceaa79d6286a15
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<unknown>(response, MAX_INDEX_BYTES));
if (!parsed.success) throw new Error('Geofabrik index is not the list of extracts');
const regions = new Map<string, GeofabrikRegion>();
- 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');