Skip to content

fix(android): accept null numeric props on ShapeSource - #4451

Merged
mfazekas merged 2 commits into
rnmapbox:mainfrom
gadcam:fix/android-shapesource-null-numbers
Nov 23, 2026
Merged

mfazekas merged 2 commits into
rnmapbox:mainfrom
gadcam:fix/android-shapesource-null-numbers

Conversation

@gadcam

@gadcam gadcam commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

On Android, unsetting a numeric prop on a mounted ShapeSource crashes. React Native sends null to the view manager, and RNMBXShapeSourceManager calls Dynamic.asInt()/asDouble() on it:

  • tolerance
  • buffer
  • maxZoomLevel
  • minZoomLevel
  • clusterRadius
  • clusterMaxZoomLevel

The integer setters now read if (value.isNull) null else value.asInt().toLong(), like RNMBXMarkerViewManager and RNMBXRasterSourceManager, and tolerance uses asDoubleOrNull(), like RNMBXCameraManager. RNMBXShapeSource stores the null, so the property goes back to the Mapbox default the next time the source is built: the builder already skips null fields. With clusterMaxZoomLevel set to null, the live setStyleSourceProperty update is skipped.

We hit this in production with tolerance, which our app never sets.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder (no spec or doc change)
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Screenshot OR Video

Tested on a physical Samsung Android phone with a debug build of /example, using the reproducer below in place of BugReportExample.js.

Before: the first tap on "Unset numeric props" throws.

java.lang.ClassCastException: Dynamic value from Object is not a number
  at com.facebook.react.bridge.DynamicFromObject.asInt(DynamicFromObject.kt:41)
  at com.rnmapbox.rnmbx.components.styles.sources.RNMBXShapeSourceManager.setClusterRadius(RNMBXShapeSourceManager.kt:106)
  at com.facebook.react.uimanager.ViewManager.updateProperties(ViewManager.java:102)
  at com.facebook.react.fabric.mounting.SurfaceMountingManager.updateProps(SurfaceMountingManager.java:811)

The crash we saw in production, with tolerance:

java.lang.ClassCastException: Dynamic value from Object is not a number
  at com.facebook.react.bridge.DynamicFromObject.asDouble(DynamicFromObject.kt:33)
  at com.rnmapbox.rnmbx.components.styles.sources.RNMBXShapeSourceManager.setTolerance(RNMBXShapeSourceManager.kt:175)
  at com.rnmapbox.rnmbx.components.styles.sources.RNMBXShapeSourceManagerDelegate.setProperty(RNMBXShapeSourceManagerDelegate.java:61)
  at com.facebook.react.uimanager.ViewManager.updateProperties(ViewManager.java:102)

After: 20 set/unset toggles, no exception in logcat, the app stays up and the layer still renders.

Component to reproduce the issue you're fixing

BugReportExample.js
import React, { useState } from 'react';
import { Button, Text, View } from 'react-native';
import { Camera, CircleLayer, MapView, ShapeSource } from '@rnmapbox/maps';

const shape = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: {},
      geometry: { type: 'Point', coordinates: [-74.00597, 40.71427] },
    },
  ],
};

const NUMERIC_PROPS = {
  tolerance: 0.5,
  buffer: 64,
  maxZoomLevel: 18,
  minZoomLevel: 1,
  clusterRadius: 40,
  clusterMaxZoomLevel: 12,
};

const BugReportExample = () => {
  const [isSet, setIsSet] = useState(true);
  const [toggles, setToggles] = useState(0);
  return (
    <View style={{ flex: 1 }}>
      <Button
        title={isSet ? 'Unset numeric props' : 'Set numeric props'}
        onPress={() => {
          setIsSet(!isSet);
          setToggles(toggles + 1);
        }}
      />
      <Text>toggles: {toggles}</Text>
      <MapView style={{ flex: 1 }}>
        <Camera centerCoordinate={[-74.00597, 40.71427]} zoomLevel={14} />
        <ShapeSource
          id="shape"
          shape={shape}
          cluster
          {...(isSet ? NUMERIC_PROPS : {})}
        >
          <CircleLayer id="circles" style={{ circleColor: 'red', circleRadius: 8 }} />
        </ShapeSource>
      </MapView>
    </View>
  );
};

export default BugReportExample;

🤖 Generated with Claude Code

gadcam and others added 2 commits November 22, 2026 16:57
Unsetting tolerance, buffer, maxZoomLevel, minZoomLevel, clusterRadius or
clusterMaxZoomLevel sends null to the manager, which threw a
ClassCastException from Dynamic.asInt()/asDouble(). Null now clears the
stored value so the Mapbox default applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@mfazekas mfazekas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this fixes it: I reproduced all six crashes on main on an emulator, and with this PR each prop unsets cleanly. Unsetting goes back to the Mapbox default once the source is rebuilt, which is how changing these props behaves today as well.

lineMetrics and clusterProperties still crash the same way when unset (asBoolean() / asMap() on null). If you'd like to cover them too, a follow-up PR would be welcome: if (lineMetrics.isNull) null else lineMetrics.asBoolean(), and an isNull check before asMap(). That would make every prop on ShapeSource safe to unset. Merging this one now.

@mfazekas
mfazekas merged commit f7ce4b8 into rnmapbox:main Nov 23, 2026
10 checks passed
@gadcam

gadcam commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @mfazekas for the lightning fast merge!
lineMetrics andclusterProperties are fixed in #4454
I also patched RNMBXShapeSourceManager.setHitbox as I found it was also an issue there in the process.

@gadcam

gadcam commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@mfazekas I realized we could catch the whole class of bug in a systematic way via static analysis and so the output of this work is this PR #4455
The static analysis identified candidates which where then filtered out if unlikely to happen in reality or if I could not find a way to trigger a bug.

This branch was previously deployed

1 inactive deployment
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.

2 participants