Skip to content

Commit 2a1096e

Browse files
Shawyeokclaude
andcommitted
fix: appId dropped when creating PropertiesCompatibleFileConfigRepository for non-default appId
DefaultConfigFactory.createPropertiesCompatibleFileConfigRepository() received an appId parameter but called ConfigService.getConfigFile(namespace, format) — the two-arg overload that ignores appId and resolves against the default app.id from app.properties. Fixes the bug by: 1. Adding ConfigService.getConfigFile(appId, namespace, format) that delegates to the already-correct ConfigManager.getConfigFile(appId, namespace, format). 2. Updating DefaultConfigFactory to call the new three-arg overload so the caller-specified appId is preserved. Adds tests: - DefaultConfigFactoryTest.testCreatePropertiesCompatibleFileConfigRepositoryForwardsCustomAppId: verifies ConfigManager is invoked with the supplied appId, never the default. - ConfigServiceTest.testGetConfigFileWithCustomAppId: verifies the new ConfigService.getConfigFile(appId, ns, format) overload returns a ConfigFile whose getAppId() equals the requested appId. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a9a6edc commit 2a1096e

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

apollo-client/src/main/java/com/ctrip/framework/apollo/ConfigService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ public static ConfigFile getConfigFile(String namespace, ConfigFileFormat config
101101
return s_instance.getManager().getConfigFile(namespace, configFileFormat);
102102
}
103103

104+
/**
105+
* Get the config file instance for the appId and namespace.
106+
*
107+
* @param appId the appId of the config
108+
* @param namespace the namespace of the config, e.g. "application.yml"
109+
* @param configFileFormat the config file format
110+
* @return config file instance
111+
*/
112+
public static ConfigFile getConfigFile(String appId, String namespace,
113+
ConfigFileFormat configFileFormat) {
114+
return s_instance.getManager().getConfigFile(appId, namespace, configFileFormat);
115+
}
116+
104117
public static ConfigMonitor getConfigMonitor(){
105118
return s_instance.getMonitor();
106119
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spi/DefaultConfigFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ PropertiesCompatibleFileConfigRepository createPropertiesCompatibleFileConfigRep
163163
String appId, String namespace, ConfigFileFormat format) {
164164
String actualNamespaceName = trimNamespaceFormat(namespace, format);
165165
PropertiesCompatibleConfigFile configFile = (PropertiesCompatibleConfigFile) ConfigService
166-
.getConfigFile(actualNamespaceName, format);
166+
.getConfigFile(appId, actualNamespaceName, format);
167167

168168
return new PropertiesCompatibleFileConfigRepository(configFile);
169169
}

apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ public void testMockConfigFactoryForConfigFile() throws Exception {
103103
assertEquals(someNamespaceFileName + ":" + someConfigFileFormat.getValue(), configFile.getContent());
104104
}
105105

106+
@Test
107+
public void testGetConfigWithCustomAppId() throws Exception {
108+
String customAppId = "customAppId";
109+
String someNamespace = "mock";
110+
String someKey = "someKey";
111+
MockInjector.setInstance(ConfigFactory.class, someNamespace, new MockConfigFactory());
112+
113+
Config config = ConfigService.getConfig(customAppId, someNamespace);
114+
115+
assertEquals(customAppId + ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR + someNamespace + ":" + someKey,
116+
config.getProperty(someKey, null));
117+
}
118+
119+
@Test
120+
public void testGetConfigFileWithCustomAppId() throws Exception {
121+
String customAppId = "customAppId";
122+
String someNamespace = "mock";
123+
ConfigFileFormat someConfigFileFormat = ConfigFileFormat.YML;
124+
String someNamespaceFileName =
125+
String.format("%s.%s", someNamespace, someConfigFileFormat.getValue());
126+
MockInjector.setInstance(ConfigFactory.class, someNamespaceFileName, new MockConfigFactory());
127+
128+
ConfigFile configFile = ConfigService.getConfigFile(customAppId, someNamespace, someConfigFileFormat);
129+
130+
assertEquals(customAppId, configFile.getAppId());
131+
assertEquals(someNamespaceFileName, configFile.getNamespace());
132+
}
133+
106134
private static class MockConfig extends AbstractConfig {
107135
private final String m_appId;
108136
private final String m_namespace;

0 commit comments

Comments
 (0)