Skip to content

Commit 2f01c44

Browse files
committed
Sync with underscore-java.
1 parent 61e9cb9 commit 2f01c44

File tree

5 files changed

+132
-20
lines changed

5 files changed

+132
-20
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ main ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main ]
20+
schedule:
21+
- cron: '31 14 * * 3'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'java' ]
36+
37+
steps:
38+
- name: Checkout repository
39+
uses: actions/checkout@v2
40+
41+
# Initializes the CodeQL tools for scanning.
42+
- name: Initialize CodeQL
43+
uses: github/codeql-action/init@v1
44+
with:
45+
languages: ${{ matrix.language }}
46+
# If you wish to specify custom queries, you can do so here or in a config file.
47+
# By default, queries listed here will override any specified in a config file.
48+
# Prefix the list here with "+" to use these queries and those in the config file.
49+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
50+
51+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
52+
# If this step fails, then you should remove it and run the build manually (see below)
53+
- name: Autobuild
54+
uses: github/codeql-action/autobuild@v1
55+
56+
# ℹ️ Command-line programs to run using the OS shell.
57+
# 📚 https://git.io/JvXDl
58+
59+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
60+
# and modify them (or add more) to build your code if your project
61+
# uses a compiled language
62+
63+
#- run: |
64+
# make bootstrap
65+
# make release
66+
67+
- name: Perform CodeQL Analysis
68+
uses: github/codeql-action/analyze@v1

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
java: [11, 16]
11+
java: [11, 17]
1212

1313
steps:
1414
- uses: actions/checkout@v2

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class U<T> extends Underscore<T> {
6363
new HashSet<>(Arrays.asList("GET", "POST", "PUT", "DELETE"));
6464
private static final int BUFFER_LENGTH_1024 = 1024;
6565
private static final int RESPONSE_CODE_400 = 400;
66+
private static final String ROOT = "root";
6667
private static String upper = "[A-Z\\xc0-\\xd6\\xd8-\\xde\\u0400-\\u04FF]";
6768
private static String lower = "[a-z\\xdf-\\xf6\\xf8-\\xff]+";
6869
private static String selfClosing = "-self-closing";
@@ -115,7 +116,8 @@ public enum Mode {
115116
FORCE_ATTRIBUTE_USAGE_AND_DEFINE_ROOT_NAME,
116117
REPLACE_NULL_WITH_EMPTY_VALUE,
117118
REPLACE_EMPTY_STRING_WITH_EMPTY_VALUE,
118-
REMOVE_FIRST_LEVEL_XML_TO_JSON
119+
REMOVE_FIRST_LEVEL_XML_TO_JSON,
120+
FORCE_ADD_ROOT_JSON_TO_XML
119121
}
120122

121123
public U(final Iterable<T> iterable) {
@@ -2036,12 +2038,6 @@ public void checkServerTrusted(
20362038
}
20372039
}
20382040

2039-
static class NoHostnameVerifier implements javax.net.ssl.HostnameVerifier {
2040-
public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
2041-
return true;
2042-
}
2043-
}
2044-
20452041
private static void setupConnection(
20462042
final java.net.HttpURLConnection connection,
20472043
final String method,
@@ -2065,8 +2061,6 @@ private static void setupConnection(
20652061
if (connection instanceof javax.net.ssl.HttpsURLConnection) {
20662062
((javax.net.ssl.HttpsURLConnection) connection)
20672063
.setSSLSocketFactory(new BaseHttpSslSocketFactory());
2068-
((javax.net.ssl.HttpsURLConnection) connection)
2069-
.setHostnameVerifier(new NoHostnameVerifier());
20702064
}
20712065
if (headerFields != null) {
20722066
for (final Map.Entry<String, List<String>> header : headerFields.entrySet()) {
@@ -2464,6 +2458,11 @@ public static String jsonToXml(
24642458
replaceEmptyStringWithEmptyValue((Map) object),
24652459
identStep,
24662460
newRootName);
2461+
} else if (mode == Mode.FORCE_ADD_ROOT_JSON_TO_XML
2462+
&& !Xml.XmlValue.getMapKey(object).equals(ROOT)) {
2463+
final Map<String, Object> map = U.newLinkedHashMap();
2464+
map.put(Underscore.isNull(newRootName) ? ROOT : newRootName, object);
2465+
result = Xml.toXml(map, identStep);
24672466
} else {
24682467
result = Xml.toXml((Map) object, identStep);
24692468
}

src/main/java/com/github/underscore/lodash/Xml.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,11 +1662,9 @@ private static boolean checkResult(
16621662
} else if (headerAttributes.containsKey(STANDALONE.substring(1))) {
16631663
((Map) result).put(STANDALONE, headerAttributes.get(STANDALONE.substring(1)));
16641664
} else if (fromType == FromType.FOR_CONVERT
1665-
&& ((Map.Entry) ((Map) result).entrySet().iterator().next()).getKey().equals(ROOT)
1666-
&& (((Map.Entry) ((Map) result).entrySet().iterator().next()).getValue()
1667-
instanceof List
1668-
|| ((Map.Entry) ((Map) result).entrySet().iterator().next()).getValue()
1669-
instanceof Map)) {
1665+
&& Xml.XmlValue.getMapKey(result).equals(ROOT)
1666+
&& (Xml.XmlValue.getMapValue(result) instanceof List
1667+
|| Xml.XmlValue.getMapValue(result) instanceof Map)) {
16701668
if (xml.startsWith(XML_HEADER)) {
16711669
return true;
16721670
} else {

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,6 @@ public void fetchPut() {
598598
// resultChain.item());
599599
}
600600

601-
@Test
602-
public void noHostnameVerifier() {
603-
new U.NoHostnameVerifier().verify("", (javax.net.ssl.SSLSession) null);
604-
}
605-
606601
@Test(expected = UnsupportedOperationException.class)
607602
public void fetchWrongUrl() {
608603
U.fetch("ttt");
@@ -788,6 +783,58 @@ public void renameRoot() {
788783
"json"));
789784
}
790785

786+
@Test
787+
public void forceAddRoot() {
788+
assertEquals(
789+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
790+
+ "<root>\n"
791+
+ " <data array=\"true\">\n"
792+
+ " <a>b</a>\n"
793+
+ " </data>\n"
794+
+ "</root>",
795+
U.jsonToXml(
796+
"{\n"
797+
+ " \"data\": [\n"
798+
+ " {\n"
799+
+ " \"a\": \"b\"\n"
800+
+ " }\n"
801+
+ " ]\n"
802+
+ "}",
803+
U.Mode.FORCE_ADD_ROOT_JSON_TO_XML));
804+
assertEquals(
805+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
806+
+ "<newroot>\n"
807+
+ " <data array=\"true\">\n"
808+
+ " <a>b</a>\n"
809+
+ " </data>\n"
810+
+ "</newroot>",
811+
U.jsonToXml(
812+
"{\n"
813+
+ " \"data\": [\n"
814+
+ " {\n"
815+
+ " \"a\": \"b\"\n"
816+
+ " }\n"
817+
+ " ]\n"
818+
+ "}",
819+
U.Mode.FORCE_ADD_ROOT_JSON_TO_XML, "newroot"));
820+
assertEquals(
821+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
822+
+ "<root>\n"
823+
+ " <element array=\"true\">\n"
824+
+ " <a>b</a>\n"
825+
+ " </element>\n"
826+
+ "</root>",
827+
U.jsonToXml(
828+
"{\n"
829+
+ " \"root\": [\n"
830+
+ " {\n"
831+
+ " \"a\": \"b\"\n"
832+
+ " }\n"
833+
+ " ]\n"
834+
+ "}",
835+
U.Mode.FORCE_ADD_ROOT_JSON_TO_XML));
836+
}
837+
791838
@Test
792839
public void updateMapKey() {
793840
Map<String, Object> map = U.newLinkedHashMap();

0 commit comments

Comments
 (0)