Skip to content

fix: resolve config refresh failure with spring.config.import on 2025.0.x - #4335

Open
wushiyuanmaimob wants to merge 4 commits into
alibaba:2025.0.xfrom
wushiyuanmaimob:fix/nacos-config-refresh-mismatch-4331
Open

fix: resolve config refresh failure with spring.config.import on 2025.0.x#4335
wushiyuanmaimob wants to merge 4 commits into
alibaba:2025.0.xfrom
wushiyuanmaimob:fix/nacos-config-refresh-mismatch-4331

Conversation

@wushiyuanmaimob

Copy link
Copy Markdown

Describe what this PR does / why we need it

Fix two defects in NacosPropertySourceRefreshListener that prevented dynamic config refresh when using spring.config.import=nacos: on the 2025.0.x branch.

Defect 1: PropertySource name mismatch

  • ConfigData path uses group@dataId naming (NacosConfigDataLoader.java:146)
  • RefreshListener looked for dataId,group (NacosPropertySourceRefreshListener.java:103)
  • Result: target.get(sourceName) always returned null, so refresh never happened

Defect 2: File extension hardcoded

  • RefreshListener hardcoded "properties" as file extension (NacosPropertySourceRefreshListener.java:108)
  • Actual extension (yml/json/xml) was stored in NacosItemConfig.suffix but not propagated
  • Result: yml/json/xml configs were parsed as properties after refresh, causing errors

Does this pull request fix one issue?

Fixes #4331

Describe how you did it

  1. Add suffix field to NacosPropertySource to preserve file extension
  2. Update NacosPropertySourceBuilder and NacosConfigDataLoader to pass suffix when creating NacosPropertySource
  3. Update NacosPropertySourceRefreshListener to:
    • Try both naming conventions (bootstrap dataId,group and ConfigData group@dataId)
    • Read actual file extension from NacosPropertySource.getSuffix() instead of hardcoding "properties"

Describe how to verify it

  1. Set up a Spring Boot 3.x app with spring.config.import=nacos:test-config.yml
  2. Change the config value in Nacos
  3. Verify the app's Environment reflects the new value (previously it would stay at old value)

Regression tests added in NacosPropertySourceRefreshListenerTest.

Special notes for reviews

  • The module has pre-existing checkstyle violations unrelated to this PR
  • Due to project dependency issues in the current branch, tests cannot run locally, but the fix logic is straightforward and covered by unit tests

Verify two defects in NacosPropertySourceRefreshListener:
1. PropertySource name mismatch between ConfigData path (group@dataId) and bootstrap path (dataId,group)
2. File extension hardcoded as 'properties' instead of using actual suffix (yml/json/xml)

Related to alibaba#4331

Signed-off-by: wushiyuan <wushiyuanwork@outlook.com>
….0.x

Fix two defects in NacosPropertySourceRefreshListener that prevented
dynamic config refresh when using spring.config.import=nacos:

1. PropertySource name mismatch: ConfigData path uses 'group@dataId'
   naming while RefreshListener looked for 'dataId,group'. Now tries
   both naming conventions.

2. File extension hardcoded: RefreshListener always used 'properties'
   extension, breaking yml/json/xml configs. Now reads actual suffix
   from NacosPropertySource.

Changes:
- Add suffix field to NacosPropertySource to preserve file extension
- Update NacosPropertySourceBuilder and NacosConfigDataLoader to pass suffix
- Update NacosPropertySourceRefreshListener to handle both naming formats
  and use actual file extension

Fixes alibaba#4331

Signed-off-by: wushiyuan <wushiyuanwork@outlook.com>
@CLAassistant

CLAassistant commented May 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

The tests were adding plain MapPropertySource to propertySources, but the
production code checks for NacosPropertySource type. This caused the refresh
logic to never execute, resulting in test failures where values weren't updated.

Fixed by:
1. Adding NacosPropertySource wrapper to propertySources instead of inner MapPropertySource
2. Specifying "yml" suffix in NacosPropertySource constructor to match test data

Signed-off-by: sywu14 <wushiyuanwork@outlook.com>
@wushiyuanmaimob
wushiyuanmaimob force-pushed the fix/nacos-config-refresh-mismatch-4331 branch from 58c013c to 05418be Compare May 27, 2026 02:01
… tests

- YAML parser returns Integer for numeric values, use String.valueOf()
- After replace, new NacosPropertySource uses standard dataId,group naming
@bengbengbalabalabeng

Copy link
Copy Markdown

Will the current PR see any further progress?

NacosPropertySourceBuilder nacosPropertySourceBuilder = new NacosPropertySourceBuilder(nacosConfigManager.getConfigService(), nacosConfigManager.getNacosConfigProperties()
.getTimeout());
String sourceName = String.join(NacosConfigProperties.COMMAS, event.dataId, event.group);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Based on the fix in #4341, it looks like the naming mismatch issue should no longer occur.

@oss-sentinel-ai oss-sentinel-ai 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.

Summary

This PR fixes two real defects in the config refresh path when using spring.config.import=nacos:: (1) PropertySource name mismatch between ConfigData (group@dataId) and bootstrap (dataId,group) naming conventions, and (2) hardcoded "properties" suffix during refresh. The dual-name lookup approach is pragmatic. However, there are a few concerns worth addressing before merge.

Findings

  • [Critical] NacosPropertySourceRefreshListener.java:125 — When the ConfigData path is taken, sourceName is set to configDataName (group@dataId), but the rebuilt NacosPropertySource from nacosPropertySourceBuilder.build() constructs its internal name as dataId,group (bootstrap convention). After target.replace(sourceName, newProperSource), the map key is group@dataId but the object's getName() returns dataId,group. This inconsistency can cause subtle bugs in downstream code that relies on NacosPropertySource.getName() matching its key in the PropertySources map. Consider passing the original sourceName into the builder or using a constructor that accepts an explicit name.
  • [Warning] This PR and #4349 both fix the same hardcoded-suffix bug with different approaches (immutable constructor param vs. mutable setter). They will conflict if both are merged. Please coordinate with the other PR author to align on a single approach.
  • [Info] Test file path src/test/java/com.alibaba.cloud.nacos/refresh/ uses dots instead of slashes for the package directory. This may cause test discovery issues depending on the build configuration.

Suggestions

// In NacosPropertySourceRefreshListener, when rebuilding for ConfigData path:
// Ensure the rebuilt source name matches the map key
String sourceName = bootstrapName;
NacosPropertySource existing = (NacosPropertySource) target.get(sourceName);
if (existing == null) {
    sourceName = configDataName;
    existing = (NacosPropertySource) target.get(sourceName);
}
// existing.getSuffix() now carries the correct extension — good.
// But verify the rebuilt source name matches sourceName.

Automated review by github-manager-bot

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.

4 participants