diff --git a/log4j-to-slf4j/pom.xml b/log4j-to-slf4j/pom.xml
index 24f9d4210080..bc01ffe9c19b 100644
--- a/log4j-to-slf4j/pom.xml
+++ b/log4j-to-slf4j/pom.xml
@@ -103,6 +103,11 @@
log4j-api-test
test
+
+ org.mockito
+ mockito-core
+ test
+
diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
index 7f8424c6a247..4f2ef190bb4c 100644
--- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
+++ b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java
@@ -16,6 +16,7 @@
*/
package org.apache.logging.slf4j;
+import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.spi.CleanableThreadContextMap;
@@ -75,13 +76,12 @@ public boolean containsKey(final String key) {
}
@Override
- @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map getCopy() {
- return MDC.getCopyOfContextMap();
+ final Map contextMap = MDC.getCopyOfContextMap();
+ return contextMap != null ? contextMap : new HashMap<>();
}
@Override
- @SuppressWarnings("unchecked") // nothing we can do about this, restricted by SLF4J API
public Map getImmutableMapOrNull() {
return MDC.getCopyOfContextMap();
}
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java
new file mode 100644
index 000000000000..6673de4597a9
--- /dev/null
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.slf4j;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import org.apache.logging.log4j.spi.ThreadContextMap;
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.Issue;
+import org.slf4j.MDCTestHelper;
+import org.slf4j.spi.MDCAdapter;
+
+class MDCContextMapTest {
+
+ @Test
+ @Issue("https://github.com/apache/logging-log4j2/issues/1426")
+ void nonNullGetCopy() {
+ final ThreadContextMap contextMap = new MDCContextMap();
+ final MDCAdapter mockAdapter = mock(MDCAdapter.class);
+ when(mockAdapter.getCopyOfContextMap()).thenReturn(null);
+ final MDCAdapter adapter = MDCTestHelper.replaceMDCAdapter(mockAdapter);
+ try {
+ assertThat(contextMap.getImmutableMapOrNull()).isNull();
+ assertThat(contextMap.getCopy()).isNotNull();
+ verify(mockAdapter, times(2)).getCopyOfContextMap();
+ verifyNoMoreInteractions(mockAdapter);
+ } finally {
+ MDCTestHelper.replaceMDCAdapter(adapter);
+ }
+ }
+}
diff --git a/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java b/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java
new file mode 100644
index 000000000000..0256131b8f23
--- /dev/null
+++ b/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.slf4j;
+
+import org.slf4j.spi.MDCAdapter;
+
+public class MDCTestHelper {
+
+ public static MDCAdapter replaceMDCAdapter(final MDCAdapter adapter) {
+ final MDCAdapter old = MDC.mdcAdapter;
+ MDC.mdcAdapter = adapter;
+ return old;
+ }
+}
diff --git a/src/changelog/.2.x.x/fix_NPE_in_closeable_thread_context.xml b/src/changelog/.2.x.x/fix_NPE_in_closeable_thread_context.xml
new file mode 100644
index 000000000000..9bd2fc1b0ee9
--- /dev/null
+++ b/src/changelog/.2.x.x/fix_NPE_in_closeable_thread_context.xml
@@ -0,0 +1,10 @@
+
+
+
+
+ Fix NPE in `CloseableThreadContext`.
+
+