Skip to content

Commit 5c74367

Browse files
committed
Check app setting and filter map
1 parent ea4df5f commit 5c74367

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/main/java/com/microsoft/azure/functions/worker/binding/RpcHttpRequestDataSource.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.stream.Collectors;
1010

1111
import com.microsoft.azure.functions.rpc.messages.NullableTypes;
12+
import com.microsoft.azure.functions.worker.Util;
1213
import org.apache.commons.lang3.reflect.TypeUtils;
1314

1415
import com.microsoft.azure.functions.HttpMethod;
@@ -92,8 +93,14 @@ public Builder createResponseBuilder(HttpStatus status) {
9293
}
9394

9495
private static Map<String, String> convertFromNullableMap(Map<String, NullableTypes.NullableString> nullableMap) {
95-
return nullableMap.entrySet().stream().collect(
96-
Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue())
97-
);
96+
if (Util.isTrue(System.getenv("FUNCTIONS_WORKER_NULLABLE_VALUES_ENABLED"))) {
97+
return nullableMap.entrySet().stream().collect(
98+
Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue())
99+
);
100+
} else {
101+
return nullableMap.entrySet().stream()
102+
.filter(e -> e.getValue().getValue() != null && !e.getValue().getValue().trim().isEmpty())
103+
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getValue()));
104+
}
98105
}
99106
}

src/test/java/com/microsoft/azure/functions/worker/binding/tests/RpcHttpRequestDataSourceTest.java

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import com.microsoft.azure.functions.rpc.messages.RpcHttp;
1313
import com.microsoft.azure.functions.rpc.messages.TypedData;
1414
import com.microsoft.azure.functions.worker.binding.*;
15+
import com.microsoft.azure.functions.worker.broker.JavaFunctionBroker;
16+
import com.microsoft.azure.functions.worker.handler.FunctionEnvironmentReloadRequestHandler;
17+
import com.microsoft.azure.functions.worker.reflect.DefaultClassLoaderProvider;
1518
import org.junit.jupiter.api.Test;
1619

1720
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -54,8 +57,15 @@ public static RpcHttp getTestRpcHttp(
5457
}
5558

5659
@Test
57-
public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsEmpty() throws Exception {
58-
60+
public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsEmpty_EnvSettingEnabled() throws Exception {
61+
DefaultClassLoaderProvider classLoader = new DefaultClassLoaderProvider();
62+
JavaFunctionBroker broker = new JavaFunctionBroker(classLoader);
63+
FunctionEnvironmentReloadRequestHandler envHandler = new FunctionEnvironmentReloadRequestHandler(broker);
64+
Map<String, String> existingVariables = System.getenv();
65+
Map<String, String> newEnvVariables = new HashMap<>();
66+
newEnvVariables.putAll(existingVariables);
67+
newEnvVariables.put("FUNCTIONS_WORKER_NULLABLE_VALUES_ENABLED", "true");
68+
envHandler.setEnv(newEnvVariables);
5969
Method httpRequestMessageStringBodyMethod = getFunctionMethod("HttpRequestStringBody");
6070
Map<String, String> queryMap = new HashMap<String, String>() {{
6171
put("name", "");
@@ -71,6 +81,31 @@ public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsEmpty() t
7181
assertEquals(requestMsg.getQueryParameters().get("name"), "");
7282
}
7383

84+
@Test
85+
public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsEmpty_EnvSettingDisabled() throws Exception {
86+
DefaultClassLoaderProvider classLoader = new DefaultClassLoaderProvider();
87+
JavaFunctionBroker broker = new JavaFunctionBroker(classLoader);
88+
FunctionEnvironmentReloadRequestHandler envHandler = new FunctionEnvironmentReloadRequestHandler(broker);
89+
Map<String, String> existingVariables = System.getenv();
90+
Map<String, String> newEnvVariables = new HashMap<>();
91+
newEnvVariables.putAll(existingVariables);
92+
newEnvVariables.put("FUNCTIONS_WORKER_NULLABLE_VALUES_ENABLED", "false");
93+
envHandler.setEnv(newEnvVariables);
94+
Method httpRequestMessageStringBodyMethod = getFunctionMethod("HttpRequestStringBody");
95+
Map<String, String> queryMap = new HashMap<String, String>() {{
96+
put("name", "");
97+
}};
98+
Parameter[] parameters = httpRequestMessageStringBodyMethod.getParameters();
99+
String sourceKey = "testRpcHttp";
100+
RpcHttp input = getTestRpcHttp(null, null, queryMap);
101+
RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource(sourceKey, input);
102+
Optional<BindingData> actualBindingData = rpcHttp.computeByName(sourceKey,
103+
parameters[0].getParameterizedType());
104+
BindingData actualArg = actualBindingData.orElseThrow(WrongMethodTypeException::new);
105+
HttpRequestMessage<?> requestMsg = (HttpRequestMessage<?>) actualArg.getValue();
106+
assertEquals(requestMsg.getQueryParameters().get("name"), null);
107+
}
108+
74109
@Test
75110
public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsNonEmpty() throws Exception {
76111

@@ -90,8 +125,15 @@ public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsNonEmpty(
90125
}
91126

92127
@Test
93-
public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersEmpty() throws Exception {
94-
128+
public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersEmpty_EnvSettingEnabled() throws Exception {
129+
DefaultClassLoaderProvider classLoader = new DefaultClassLoaderProvider();
130+
JavaFunctionBroker broker = new JavaFunctionBroker(classLoader);
131+
FunctionEnvironmentReloadRequestHandler envHandler = new FunctionEnvironmentReloadRequestHandler(broker);
132+
Map<String, String> existingVariables = System.getenv();
133+
Map<String, String> newEnvVariables = new HashMap<>();
134+
newEnvVariables.putAll(existingVariables);
135+
newEnvVariables.put("FUNCTIONS_WORKER_NULLABLE_VALUES_ENABLED", "true");
136+
envHandler.setEnv(newEnvVariables);
95137
Method httpRequestMessageStringBodyMethod = getFunctionMethod("HttpRequestStringBody");
96138
Map<String, String> headersMap = new HashMap<String, String>() {{
97139
put("cookie", "");
@@ -107,6 +149,31 @@ public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersEmpty() throw
107149
assertEquals(requestMsg.getHeaders().get("cookie"), "");
108150
}
109151

152+
@Test
153+
public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersEmpty_EnvSettingDisabled() throws Exception {
154+
DefaultClassLoaderProvider classLoader = new DefaultClassLoaderProvider();
155+
JavaFunctionBroker broker = new JavaFunctionBroker(classLoader);
156+
FunctionEnvironmentReloadRequestHandler envHandler = new FunctionEnvironmentReloadRequestHandler(broker);
157+
Map<String, String> existingVariables = System.getenv();
158+
Map<String, String> newEnvVariables = new HashMap<>();
159+
newEnvVariables.putAll(existingVariables);
160+
newEnvVariables.put("FUNCTIONS_WORKER_NULLABLE_VALUES_ENABLED", "false");
161+
envHandler.setEnv(newEnvVariables);
162+
Method httpRequestMessageStringBodyMethod = getFunctionMethod("HttpRequestStringBody");
163+
Map<String, String> headersMap = new HashMap<String, String>() {{
164+
put("cookie", "");
165+
}};
166+
Parameter[] parameters = httpRequestMessageStringBodyMethod.getParameters();
167+
String sourceKey = "testRpcHttp";
168+
RpcHttp input = getTestRpcHttp(null, headersMap, null);
169+
RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource(sourceKey, input);
170+
Optional<BindingData> actualBindingData = rpcHttp.computeByName(sourceKey,
171+
parameters[0].getParameterizedType());
172+
BindingData actualArg = actualBindingData.orElseThrow(WrongMethodTypeException::new);
173+
HttpRequestMessage<?> requestMsg = (HttpRequestMessage<?>) actualArg.getValue();
174+
assertEquals(requestMsg.getHeaders().get("cookie"), null);
175+
}
176+
110177
@Test
111178
public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersNonEmpty() throws Exception {
112179

0 commit comments

Comments
 (0)