Skip to content

Respect the stride parameter in deflateCoordinate - #17614

Merged
ahocevar merged 1 commit into
openlayers:mainfrom
ethanstoner:fix-deflate-coordinate-stride
Sep 4, 2026
Merged

ahocevar merged 1 commit into
openlayers:mainfrom
ethanstoner:fix-deflate-coordinate-stride

Conversation

@ethanstoner

@ethanstoner ethanstoner commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15754.

deflateCoordinate takes a stride but loops over coordinate.length, so a coordinate with more ordinates than the layout allows writes the extras into the flat coordinates. Its callers are Point#setCoordinates and Circle#setCenterAndRadius, which are then left with a stride and layout that no longer describe their own data. Circle also reports a wrong radius and extent, because setCenterAndRadius writes the radius at the offset the deflate returned.

const point = new Point([1, 2, 3], 'XYZ');
point.setCoordinates(point.getCoordinates(), 'XY');
point.getCoordinates(); // [1, 2, 3], expected [1, 2]

Tests cover deflateCoordinate directly and the Point#setCoordinates case.

The two GeoJSON tests for empty coordinate arrays fail on this branch, as expected, and #17615 fixes them.

@fallenmi fallenmi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stride clamp fixes the layout inconsistency without regressing supported empty coordinates. With the identical public-API oracle on exact base d2d35d7d, reducing a Point from XYZ to XY left [1, 2, 3], and the same over-length center made a radius-5 Circle report 4.47213595499958. Exact head 0d367d050 returns [1, 2], preserves radius 5, and leaves both empty and shorter-than-stride coordinates at their existing lengths.

I also checked both callers' truncation behavior and a nonzero-offset deflateCoordinate case. The exact head passes the focused Point/Circle/deflate tests (74/74), the full node suite (2,229/2,229), the browser suite (3,063/3,063), TypeScript typecheck, and diff check. The accepted issue contract and sibling deflateCoordinates behavior match this change. There are currently no published GitHub check runs on the PR head; I did not run the rendering image-comparison suite locally.

Reviewed with OpenAI Codex assistance; I reproduced the exact base/head behavior and ran the suites above locally.

@github-actions

Copy link
Copy Markdown

@codeCraft-Ritik codeCraft-Ritik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very thorough and well-documented fix!

Catching the edge case where deflateCoordinates would pad undefined on empty coordinates was a great catch. The solution with Math.min is clean and elegant.

@ahocevar ahocevar left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethanstoner Thanks for the fix. Please take the time to look at other commits and pull requests in this repository to align yours with the culture of the project (one-line commit messages, keep source code comments short if they are necessary; avoid them if the code speaks for itself).

Short but precise pull request descriptions are what maintainers rely on for reviewing your code, and what's important when someone needs to understand why a change was made, even years after it was made.

Most obvious example: Text like this is not helpful — that's the least you should remove when you let an AI write pull request descriptions.

PR: Respect the stride parameter in deflateCoordinate

Title: Respect the stride parameter in deflateCoordinate
Base: openlayers/openlayers:main
Head: ethanstoner:fix-deflate-coordinate-stride

The repo's .github/PULL_REQUEST_TEMPLATE.md is comment-only (a checklist for the author, no
fields to fill in), so the body below is free-form and follows the template's guidance: reference
the issue, keep commits small and clear, cover the fix with regression tests.

Body

Ideally, you'd go over the pull request description yourself. Ask yourself what a maintainer needs to know, and write it accordingly.

That said, please improve the pull request (both code and description) according to my comment, then ask for another review.

Note to @codeCraft-Ritik and @fallenmi: before posting a comment on a pull request, please ask yourself if the text of you have in mind is useful for the topic. Useful comments include reports about the pull request having fixed the underlying issue in your application, or additional observations that might help improve the pull request.

@ethanstoner
ethanstoner force-pushed the fix-deflate-coordinate-stride branch from 0d367d0 to abc8f57 Compare August 31, 2026 13:47
@ethanstoner

Copy link
Copy Markdown
Contributor Author

Done. The commit is one line, the source comment is gone, and the description is rewritten.

You're right about the description — the header block you quoted was scaffolding that should never have been in there, and the rest was longer than a reviewer needs. The new one states the defect, the two affected callers, and why the clamp is Math.min(coordinate.length, stride) rather than stride.

Comment thread src/ol/geom/flat/deflate.js Outdated
*/
export function deflateCoordinate(flatCoordinates, offset, coordinate, stride) {
for (let i = 0, ii = coordinate.length; i < ii; ++i) {
const ii = Math.min(coordinate.length, stride);

@ahocevar ahocevar Aug 31, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why math.min? According to how the other deflate functions work, the correct for-loop would be

for (let i = 0; i < stride; ++i) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first. It fails two tests that are already on main, in a file this branch doesn't touch:

FAIL test/node/ol/format/GeoJSON.test.js > #readGeometry > works with empty coordinate array
  AssertionError: expected [ undefined, undefined ] to deeply equal []

FAIL test/node/ol/format/GeoJSON.test.js > #readGeometry > works with empty coordinate array and reprojection
  AssertionError: expected [ NaN, NaN ] to deeply equal []

test/node/ol/format/GeoJSON.test.js:596 and :615.

You're right that {"type": "Point", "coordinates": []} isn't valid GeoJSON. The clamp isn't there to defend the spec, it's there because the reader accepts that input today and those two tests assert what it produces. With i < stride it writes undefined into the flat coordinates instead.

So it's your call which behaviour you want:

  1. keep Math.min, and no existing test changes
  2. use i < stride, and update those two tests to match, treating an empty position as unsupported

Happy to push 2 if that's the direction. I didn't want to change how the GeoJSON reader handles that input without asking first.

@ahocevar ahocevar Aug 31, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither 1 nor 2, but closer to 2 — see my comment below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to for (let i = 0; i < stride; ++i).

The two GeoJSON tests for empty coordinate arrays fail on this branch, as you said to expect. Rest of the node suite is green: 2226 passed, 2 failed.

Comment thread test/node/ol/geom/flat/deflate.test.js Outdated
Comment on lines +26 to +31

it('does not pad a coordinate shorter than the stride', function () {
const offset = deflateCoordinate(flatCoordinates, 0, [], 2);
assert.strictEqual(offset, 0);
assert.deepEqual(flatCoordinates, []);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[] is not a valid coordinate, so this test case can be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That case covers the clamp, so it goes if the clamp goes.

Answered in the other thread: removing the clamp also breaks two existing GeoJSON reader tests on main, so it's a behaviour decision rather than just dropping a test case. Tell me which way you want it and I'll push it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@ahocevar

ahocevar commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

The loop now stops at Math.min(coordinate.length, stride) rather than at stride, because
clamping to stride alone pads a short coordinate with undefined. Empty coordinates are a
supported case — GeoJSON {"type": "Point", "coordinates": []} gets layout XY and stride 2 — and
two existing tests in test/node/ol/format/GeoJSON.test.js fail with the padding version.

According to the GeoJSON spec:

For type "Point", the "coordinates" member is a single position.

and

A position is an array of numbers. There MUST be two or more elements.

So {"type": "Point", "coordinates": []} is not valid GeoJSON.

GeoJSON processors MAY interpret Geometry objects with empty "coordinates" arrays as null objects.

But we don't need to fail on empty coordinates; we can return a null geometry.

@ahocevar

ahocevar commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

I just created #17615 to fix the underlying issue of the two failing GeoJSON tests you found when you tried the correct fix I suggested above.

You can proceed with the two tests failing. When #17615 is merged, we'll rebase your updated pull request to make it green.

@ethanstoner
ethanstoner force-pushed the fix-deflate-coordinate-stride branch from abc8f57 to f85320f Compare September 2, 2026 13:41
@ethanstoner

Copy link
Copy Markdown
Contributor Author

Description rewritten. The scaffolding text at the top is gone, and what is left is the issue reference, what the bug is, the reproduction, and what the tests cover.

Code now matches your suggestion: the loop bounds on stride, and the [] test case is removed. Ready for another look whenever suits.

@ahocevar

ahocevar commented Sep 3, 2026

Copy link
Copy Markdown
Member

@ethanstoner Please make sure that prettier does not report any errors:

/home/runner/work/openlayers/openlayers/test/node/ol/geom/flat/deflate.test.js
Error:   26:1  error  Delete `⏎`  prettier/prettier

@ethanstoner
ethanstoner force-pushed the fix-deflate-coordinate-stride branch from f85320f to 438d7a8 Compare September 3, 2026 21:58
@ethanstoner

Copy link
Copy Markdown
Contributor Author

Prettier is clean now, thanks for catching it. That blank line was left over from removing the [] case.

Current CI on this branch: Pre-Test, Build, Browser, Rendering and CodeQL all pass. Node is 2228 passed / 2 failed, and the 2 are the GeoJSON empty-coordinate-array tests you said to proceed with. #17615 is still open, so they will stay red here until it lands.

Ready for another look whenever suits.

@ahocevar
ahocevar force-pushed the fix-deflate-coordinate-stride branch from 5fd6ad5 to 958c4cb Compare September 4, 2026 20:43

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ethanstoner. Rebased.

@ahocevar
ahocevar merged commit 88704b3 into openlayers:main Sep 4, 2026
8 checks passed
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.

deflateCoordinate ignores stride parameter unlike the other functions in deflate.js

4 participants