From 8d23b14da65f7bc3de2ac7279306711ea62e9a3c Mon Sep 17 00:00:00 2001 From: Mad Dinh Date: Fri, 21 Aug 2026 11:03:12 -0700 Subject: [PATCH] Fix nullable float props crashing ViewManagers on Android (#58038) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes https://github.com/react/react-native/issues/55350. A codegen'd Fabric component that declares an optional float prop: ```js type NativeProps = Readonly<{ ...ViewProps, testFloatNullable?: WithDefault, }>; ``` crashes the app at startup as soon as the `ViewManager` implements it with a `ReactProp` annotation: ``` java.lang.RuntimeException: Unrecognized type: class java.lang.Float for method: MyNativeViewManager#setTestFloatNullable at ViewManagersPropertyCache.createPropSetter(...) at ViewManagerPropertyUpdater$FallbackViewManagerSetter.(...) at ViewManager.getNativeProps(...) ``` `WithDefault` is the only way to express a genuinely nullable float in a codegen spec — a plain optional `CodegenTypes.Float` is emitted as a primitive `float` defaulting to `0f`. So codegen generates a `Nullable Float` setter in the ViewManager interface, and then the runtime refuses to bind it. ### Root cause `GeneratePropsJavaInterface` emits exactly four boxed (nullable) prop types: | Codegen type annotation | Emitted Java type | Handled by `ViewManagersPropertyCache` | | --- | --- | --- | | `BooleanTypeAnnotation` with `default: null` | `Nullable Boolean` | `BoxedBooleanPropSetter` | | `ReservedPropTypeAnnotation` / `ColorPrimitive` | `Nullable Integer` | `BoxedColorPropSetter` | | `Int32EnumTypeAnnotation` | `Nullable Integer` | `BoxedIntPropSetter` | | `FloatTypeAnnotation` with `default: null` | `Nullable Float` | **nothing — falls through to `else`** | `createPropSetter` maps `java.lang.Boolean` and `java.lang.Integer`, but has no branch for `java.lang.Float`, so it hits the `else` and throws. This adds the missing `BoxedFloatPropSetter`, mirroring `BoxedIntPropSetter`: unbox `Double` (all JS numbers arrive as `Double`) to `Float`, and pass `null` straight through so the setter actually receives the absent value. `DoubleTypeAnnotation` is deliberately not covered — codegen always emits it as a primitive `double`, never boxed, so there is no nullable-double setter to bind. Note this only affects ViewManagers that use `ReactProp`. A codegen'd ViewManager that routes props through its generated delegate never reaches `ViewManagersPropertyCache`, which is why the crash looks intermittent — the annotation is what pulls in `FallbackViewManagerSetter`. ## Changelog: [ANDROID] [FIXED] - Fix `RuntimeException: Unrecognized type: class java.lang.Float` when a `ViewManager` implements a nullable float prop (`WithDefault`) with `ReactProp` Pull Request resolved: https://github.com/react/react-native/pull/58038 Test Plan: Added `testBoxedFloatSetter` to `ReactPropAnnotationSetterTest`, alongside the existing `testBoxedBooleanSetter` / `testBoxedIntSetter`, plus a `boxedFloatProp` on the ViewManager under test. It drives `viewManager.updateProperties(...)`, which is the same `FallbackViewManagerSetter` → `getNativePropSettersForViewManagerClass` → `createPropSetter` path as the crash stack trace, and asserts the setter receives `3.5f`, `-7.0f` and `null`. ``` $ ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \ --tests "com.facebook.react.uimanager.ReactPropAnnotationSetterTest" BUILD SUCCESSFUL in 3m 16s tests=18 failures=0 errors=0 skipped=0 ok testBoxedBooleanSetter ok testBoxedFloatSetter <- new ok testBoxedIntSetter ok testFloatSetter ... (18/18) ``` Reverting only `ViewManagersPropertyCache.kt` and keeping the new test reproduces the reported crash verbatim: ``` tests=18 failures=18 errors=0 java.lang.RuntimeException: Unrecognized type: class java.lang.Float for method: ReactPropAnnotationSetterTest$ViewManagerUnderTest#setBoxedFloatProp ``` All 18 fail rather than just the new one, and that is the bug's real shape: `getNativePropSettersForViewManagerClass` builds the setter map for the entire ViewManager class in one pass and caches it, so a single unbindable prop takes down every other prop on that manager. That is why the reported crash happens at startup, while `getNativeProps()` is collecting view manager constants, rather than when the offending prop is first set. Kotlin formatting: ``` $ ./gradlew ktfmtCheck BUILD SUCCESSFUL ``` `ViewManagersPropertyCache` is an `internal object` and `BoxedFloatPropSetter` is a `private class`, so `ReactAndroid.api` is unchanged. Reviewed By: christophpurrer Differential Revision: D116928062 Pulled By: javache fbshipit-source-id: 54b60b2f9ab213f7dc1f56fa3ed26de990245d8a --- .../uimanager/ViewManagersPropertyCache.kt | 13 +++++++++++ .../ReactPropAnnotationSetterTest.kt | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.kt index 9e8c8bcf843..116ed4ce00d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.kt @@ -259,6 +259,18 @@ internal object ViewManagersPropertyCache { } } + private class BoxedFloatPropSetter(prop: ReactProp, setter: Method) : + PropSetter(prop, "number", setter) { + + override fun getValueOrDefault(value: Any?, context: Context): Any? { + if (value != null) { + // All numbers from JS are Doubles which can't be simply cast to Float + return if (value is Double) value.toFloat() else value as Float + } + return null + } + } + private class BoxedIntPropSetter : PropSetter { constructor(prop: ReactProp, setter: Method) : super(prop, "number", setter) @@ -391,6 +403,7 @@ internal object ViewManagersPropertyCache { DoublePropSetter(annotation, method, annotation.defaultDouble) String::class.java -> StringPropSetter(annotation, method) java.lang.Boolean::class.java -> BoxedBooleanPropSetter(annotation, method) + java.lang.Float::class.java -> BoxedFloatPropSetter(annotation, method) java.lang.Integer::class.java -> if ("Color" == annotation.customType) { BoxedColorPropSetter(annotation, method) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt index 0813c85acaa..c0446503690 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt @@ -39,6 +39,8 @@ class ReactPropAnnotationSetterTest { fun onBoxedBooleanSetterCalled(value: Boolean?) + fun onBoxedFloatSetterCalled(value: Float?) + fun onBoxedIntSetterCalled(value: Int?) fun onArraySetterCalled(value: ReadableArray?) @@ -128,6 +130,11 @@ class ReactPropAnnotationSetterTest { viewManagerUpdatesReceiver.onBoxedIntSetterCalled(value) } + @ReactProp(name = "boxedFloatProp") + fun setBoxedFloatProp(v: View?, value: Float?) { + viewManagerUpdatesReceiver.onBoxedFloatSetterCalled(value) + } + @ReactProp(name = "arrayProp") fun setArrayProp(v: View?, value: ReadableArray?) { viewManagerUpdatesReceiver.onArraySetterCalled(value) @@ -314,6 +321,22 @@ class ReactPropAnnotationSetterTest { Mockito.reset(updatesReceiverMock) } + @Test + fun testBoxedFloatSetter() { + viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", 3.5)) + Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(3.5f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", -7.0)) + Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(-7.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedFloatProp", null)) + Mockito.verify(updatesReceiverMock).onBoxedFloatSetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + @Test fun testArraySetter() { val array: ReadableArray = JavaOnlyArray()