Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 20814ae

Browse files
glucaspavelbucek
authored andcommitted
JERSEY-3088: Traverse maps during field processing.
Change-Id: Ie627b3582b591bdf9dde827d40b2f308c2feb54c
1 parent ac34707 commit 20814ae

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

incubator/declarative-linking/src/main/java/org/glassfish/jersey/linking/FieldProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -47,6 +47,7 @@
4747
import java.util.logging.Logger;
4848
import java.util.HashSet;
4949
import java.util.List;
50+
import java.util.Map;
5051
import java.util.Set;
5152
import javax.ws.rs.core.Link;
5253
import javax.ws.rs.core.UriInfo;
@@ -133,7 +134,7 @@ private void processLinks(Object entity, Object resource, Object instance,
133134
}
134135
}
135136

136-
// If entity is an array or collection then process members
137+
// If entity is an array, collection, or map then process members
137138
Class<?> instanceClass = instance.getClass();
138139
if (instanceClass.isArray() && Object[].class.isAssignableFrom(instanceClass)) {
139140
Object array[] = (Object[]) instance;
@@ -145,6 +146,11 @@ private void processLinks(Object entity, Object resource, Object instance,
145146
for (Object member : iterable) {
146147
processMember(entity, resource, member, processed, uriInfo, rmc);
147148
}
149+
} else if (instance instanceof Map) {
150+
Map map = (Map) instance;
151+
for (Object member : map.entrySet()) {
152+
processMember(entity, resource, member, processed, uriInfo, rmc);
153+
}
148154
}
149155

150156
// Recursively process all member fields

incubator/declarative-linking/src/test/java/org/glassfish/jersey/linking/FieldProcessorTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2016 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -43,8 +43,10 @@
4343
import java.net.URI;
4444
import java.util.Arrays;
4545
import java.util.Collections;
46+
import java.util.HashMap;
4647
import java.util.Iterator;
4748
import java.util.List;
49+
import java.util.Map;
4850
import java.util.logging.Filter;
4951
import java.util.logging.LogRecord;
5052
import java.util.logging.Logger;
@@ -346,6 +348,21 @@ public void testCollection() {
346348
assertEquals("widgets/20", list.get(1).link);
347349
}
348350

351+
@Test
352+
public void testMap() {
353+
LOG.info("Map");
354+
FieldProcessor<Map> instance = new FieldProcessor(Map.class);
355+
TestClassE item1 = new TestClassE("10");
356+
TestClassE item2 = new TestClassE("20");
357+
Map<String, TestClassE> map = new HashMap<>();
358+
for (TestClassE item : Arrays.asList(item1, item2)) {
359+
map.put(item.getId(), item);
360+
}
361+
instance.processLinks(map, mockUriInfo, mockRmc);
362+
assertEquals("widgets/10", map.get("10").link);
363+
assertEquals("widgets/20", map.get("20").link);
364+
}
365+
349366
public static class TestClassG {
350367

351368
@InjectLink(value = TEMPLATE_B, style = InjectLink.Style.RELATIVE_PATH)

0 commit comments

Comments
 (0)