Skip to content

Fix GeoJsonReader.geoJsonFrom(contentsOf:) always returning nil - #264

Merged
trasch merged 1 commit into
mainfrom
fix/geojsonreader-contentsof
Oct 19, 2026
Merged

trasch merged 1 commit into
mainfrom
fix/geojsonreader-contentsof

Conversation

@trasch

@trasch trasch commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Summary

GeoJsonReader.geoJsonFrom(contentsOf:) always returned nil, so loading any GeoJSON file through the generic reader failed even though the concrete initializers (e.g. Point(contentsOf:)) worked.

Root cause

The file-based overload passed the raw Data to geoJsonFrom(json:), which expects an already-parsed JSON object (Any?). Since Data is never a GeoJSON object, parsing always failed.

// before
geoJsonFrom(json: try? Data(contentsOf: url))

Changes

  • Read the file first and delegate to the data overload (geoJsonFrom(jsonData:)), which performs the actual JSONSerialization step.
  • Return nil when the file cannot be read (unchanged behaviour).

Tests

Added to ReaderTests:

  • loadContentsOfUrl — reads a Point from a file URL (the reproducer from GeoJsonReader.geoJsonFrom(contentsOf:) always returns nil #262).
  • loadFeatureCollectionFromContentsOfUrl — reads a FeatureCollection and checks equality.
  • loadContentsOfUrlWithMissingFile — missing file still returns nil.
  • loadContentsOfUrlWithInvalidContent — non-GeoJSON file still returns nil.

swift test --filter ReaderTests: 16/16 pass. With the fix reverted, loadContentsOfUrl fails as expected, so the regression test is effective.

Fixes #262

The file-based overload passed raw Data to geoJsonFrom(json:), which expects an already-parsed JSON object, so every call returned nil. Delegate to geoJsonFrom(jsonData:) instead.

Add tests for reading a Point and a FeatureCollection from a file URL, plus missing-file and invalid-content handling.

Fixes #262
@trasch trasch added the bug Something isn't working label Oct 19, 2026
@trasch trasch self-assigned this Oct 19, 2026
@trasch
trasch merged commit fdcb9de into main Oct 19, 2026
1 check passed
@trasch
trasch deleted the fix/geojsonreader-contentsof branch October 19, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GeoJsonReader.geoJsonFrom(contentsOf:) always returns nil

1 participant