Skip to content

Commit 1536325

Browse files
committed
Merge pull request docker-java#438 from rfhayashi/npe_exposed_ports
fix npe on ContainerConfig.getExposedPorts
2 parents aa38878 + bbe940f commit 1536325

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/main/java/com/github/dockerjava/api/model/ContainerConfig.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public class ContainerConfig {
8181

8282
@JsonIgnore
8383
public ExposedPort[] getExposedPorts() {
84-
return exposedPorts.getExposedPorts();
84+
if (exposedPorts == null) {
85+
return new ExposedPort[0];
86+
} else {
87+
return exposedPorts.getExposedPorts();
88+
}
8589
}
8690

8791
public boolean isNetworkDisabled() {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.testng.annotations.Test;
5+
6+
import java.io.IOException;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class ContainerConfigTest {
11+
12+
@Test
13+
public void missingExposedPortsReturnEmptyArray() throws IOException {
14+
String s = "{}";
15+
ObjectMapper objectMapper = new ObjectMapper();
16+
ContainerConfig config = objectMapper.readValue(s, ContainerConfig.class);
17+
assertEquals(0, config.getExposedPorts().length);
18+
}
19+
20+
@Test
21+
public void nullExposedPortsReturnEmptyArray() throws IOException {
22+
String s = "{\"ExposedPorts\": null}";
23+
ObjectMapper objectMapper = new ObjectMapper();
24+
ContainerConfig config = objectMapper.readValue(s, ContainerConfig.class);
25+
assertEquals(0, config.getExposedPorts().length);
26+
}
27+
28+
@Test
29+
public void exposedPortsReturnArray() throws IOException {
30+
String s = "{\"ExposedPorts\": {\"22/tcp\": {}, \"80/tcp\": {}}}";
31+
ObjectMapper objectMapper = new ObjectMapper();
32+
ContainerConfig config = objectMapper.readValue(s, ContainerConfig.class);
33+
ExposedPort[] ports = config.getExposedPorts();
34+
assertEquals(2, ports.length);
35+
ExposedPort port22tcp = ports[0];
36+
assertEquals(22, port22tcp.getPort());
37+
assertEquals(InternetProtocol.TCP, port22tcp.getProtocol());
38+
ExposedPort port80tcp = ports[1];
39+
assertEquals(80, port80tcp.getPort());
40+
assertEquals(InternetProtocol.TCP, port80tcp.getProtocol());
41+
}
42+
43+
}

0 commit comments

Comments
 (0)