Skip to content

Fix Polygon cast returning bare geometry instead of SpatialExpression - #333

Closed
halkibsi wants to merge 1 commit into
fleetbase:mainfrom
halkibsi:fix/polygon-cast-spatial-expression
Closed

halkibsi wants to merge 1 commit into
fleetbase:mainfrom
halkibsi:fix/polygon-cast-spatial-expression

Conversation

@halkibsi

Copy link
Copy Markdown

Summary

Casts\Polygon::set() returned the bare Polygon object for typed geometries, while Point and MultiPolygon wrap theirs in SpatialExpression.

Because Laravel re-runs class casts during getAttributes() (mergeAttributesFromClassCasts), the second set() call overwrote the SpatialExpression that SpatialTrait::performInsert() had prepared — so the raw Polygon object was bound to the query, PDO stringified it to (lng lat, ...), and MySQL rejected the write:

SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry
object from data you send to the GEOMETRY field

Hit this running the FleetOps testing seeders (NetworkSeeder → zones.border) against the bundled MySQL 8 container. The same path breaks Zone creation through the API.

Changes

  • Casts\Polygon::set() now returns new SpatialExpression($value) for SpatialPolygon and GeometryInterface inputs, mirroring MultiPolygon exactly (the GeoJSON and SpatialExpression branches are unchanged — non-object inputs never enter the cast cache, so the geometry-object return there is still correct).
  • Updated SpatialCastBranchesTest which previously asserted the old hand-back-the-object behavior; it now expects a SpatialExpression like the other casts.

Test plan

  • db:seed --class="Fleetbase\FleetOps\Seeders\Testing\TestingSeeder" completes on a fresh Docker install (was failing on zone insert)
  • CI: unit tests

Polygon::set() returned the bare Polygon object for typed geometries
while Point and MultiPolygon wrap them in SpatialExpression. Laravel
re-runs class casts during getAttributes(), which overwrote the
SpatialExpression prepared by SpatialTrait::performInsert() — so the
raw geometry was bound to the query and MySQL rejected the write with
error 1416 (Cannot get geometry object from data you send to the
GEOMETRY field). This broke zone creation, e.g. in the FleetOps
testing seeders.
@halkibsi halkibsi closed this Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant