wsyjh8 opened a new pull request, #16313:
URL: https://github.com/apache/dubbo/pull/16313

   ## What
   Fixes the `UnsupportedOperationException` thrown by 
`DubboDefaultPropertiesEnvironmentPostProcessor#addOrReplace` when a 
`defaultProperties` property source is already present and is backed by an 
**immutable** map.
   
   ## Why
   `addOrReplace` merged Dubbo's defaults into an existing `defaultProperties` 
`MapPropertySource` by mutating its backing map in place: 
`target.getSource().put(...)`. `MapPropertySource.getSource()` returns the map 
instance the source was constructed with, and Spring does not guarantee it is 
mutable. Because this post-processor runs at `LOWEST_PRECEDENCE`, another 
`EnvironmentPostProcessor` (e.g. Nacos / Spring Cloud bootstrap) may already 
have registered `defaultProperties` backed by an immutable map (e.g. Guava 
`ImmutableMap`), so the `put` throws:
   ```
   java.lang.UnsupportedOperationException
       at com.google.common.collect.ImmutableMap.put(ImmutableMap.java:814)
       at 
org.apache.dubbo.spring.boot.env.DubboDefaultPropertiesEnvironmentPostProcessor.addOrReplace(DubboDefaultPropertiesEnvironmentPostProcessor.java:134)
   ```
   
   ## How
   Instead of mutating the existing backing map in place, copy its entries into 
a new mutable `HashMap`, merge the Dubbo defaults (only for keys not already 
present — unchanged semantics), and `MutablePropertySources.replace(...)` the 
source at its existing position. Behaviour for the normal (mutable) case is 
observably identical (same keys, same precedence); the immutable case no longer 
throws. No public method signature or behaviour change.
   
   ## Compatibility
   Java 8 language level preserved (no `var`, no `Map.of`, no streams; 
`HashMap` copy-ctor + `MutablePropertySources.replace`, available since Spring 
3.1).
   
   ## Tests
   Added two unit tests in `DubboDefaultPropertiesEnvironmentPostProcessorTest`:
   - `testPostProcessEnvironmentOnImmutableDefaultProperties` — registers a 
`defaultProperties` source backed by `Collections.unmodifiableMap(...)` and 
asserts `addOrReplace` no longer throws (fails with 
`UnsupportedOperationException` on current code, passes with this fix).
   - `testImmutableDefaultPropertiesDoesNotOverrideExistingKey` — verifies 
precedence is preserved on an immutable-backed source: a key already present 
(`dubbo.config.multiple=false`) is not overridden by Dubbo's default.
   
   All existing cases still pass; `spotless:check` is clean.
   
   Fixes #16268
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to