Skip to content

Commit 998188c

Browse files
committed
fix: Allow empty responses as well as null response in AppConfig
- AppConfig responses, where config hasn't changed, seem to be result in "empty" `SdkBytes` objects rather than `null` `Configuration` in the response.
1 parent 3440513 commit 998188c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/AppConfigProvider.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
3030
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
3131

32+
import static software.amazon.awssdk.utils.StringUtils.isBlank;
33+
3234
/**
3335
* Implements a {@link ParamProvider} on top of the AppConfig service. AppConfig provides
3436
* a mechanism to retrieve and update configuration of applications over time.
@@ -98,7 +100,7 @@ protected String getValue(String key) {
98100
// Get the value of the key. Note that AppConfig will return null if the value
99101
// has not changed since we last asked for it in this session - in this case
100102
// we return the value we stashed at last request.
101-
String value = response.configuration() != null ?
103+
String value = response.configuration() != null && response.configuration().asByteArray().length > 0 ?
102104
response.configuration().asUtf8String() : // if we have a new value, use it
103105
establishedSession != null ?
104106
establishedSession.lastConfigurationValue :

powertools-parameters/src/test/java/software/amazon/lambda/powertools/parameters/AppConfigProviderTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,29 @@ public void getValueRetrievesValue() {
9090
GetLatestConfigurationResponse thirdResponse = GetLatestConfigurationResponse.builder()
9191
.nextPollConfigurationToken("token4")
9292
.build();
93+
// Forth response returns empty, which means the provider should yield the previous value again
94+
GetLatestConfigurationResponse forthResponse = GetLatestConfigurationResponse.builder()
95+
.nextPollConfigurationToken("token5")
96+
.configuration(SdkBytes.fromUtf8String(""))
97+
.build();
9398
Mockito.when(client.startConfigurationSession(startSessionRequestCaptor.capture()))
9499
.thenReturn(firstSession);
95100
Mockito.when(client.getLatestConfiguration(getLatestConfigurationRequestCaptor.capture()))
96-
.thenReturn(firstResponse, secondResponse, thirdResponse);
101+
.thenReturn(firstResponse, secondResponse, thirdResponse, forthResponse);
97102

98103
// Act
99104
String returnedValue1 = provider.getValue(defaultTestKey);
100105
String returnedValue2 = provider.getValue(defaultTestKey);
101106
String returnedValue3 = provider.getValue(defaultTestKey);
107+
String returnedValue4 = provider.getValue(defaultTestKey);
102108

103109
// Assert
104110
assertThat(returnedValue1).isEqualTo(firstResponse.configuration().asUtf8String());
105111
assertThat(returnedValue2).isEqualTo(secondResponse.configuration().asUtf8String());
106112
assertThat(returnedValue3).isEqualTo(secondResponse.configuration()
107113
.asUtf8String()); // Third response is mocked to return null and should re-use previous value
114+
assertThat(returnedValue4).isEqualTo(secondResponse.configuration()
115+
.asUtf8String()); // Forth response is mocked to return empty and should re-use previous value
108116
assertThat(startSessionRequestCaptor.getValue().applicationIdentifier()).isEqualTo(applicationName);
109117
assertThat(startSessionRequestCaptor.getValue().environmentIdentifier()).isEqualTo(environmentName);
110118
assertThat(startSessionRequestCaptor.getValue().configurationProfileIdentifier()).isEqualTo(defaultTestKey);

0 commit comments

Comments
 (0)