Skip to content

Commit cc85a85

Browse files
committed
Add string vs integer parameter tests and examples
1 parent 70358cb commit cc85a85

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[request_definition]
2+
r = sub, obj, act, exp
3+
4+
[policy_definition]
5+
p = sub, obj, act, exp
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act && r.exp == "1"
15+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p, reader, data, read, 1
2+
g, alice, reader

src/main/java/org/casbin/CommandExecutor.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,28 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
156156
return mapper.writeValueAsString(responseBody);
157157
}
158158

159-
/**
160-
* @param value
161-
* @return
162-
*/
163159
private Object smartConvertValue(String value) {
164-
if (value == null || value.trim().isEmpty()) {
165-
return value;
160+
value = value.trim();
161+
162+
if (value.startsWith("\"") && value.endsWith("\"")) {
163+
return value.substring(1, value.length() - 1); // 去掉引号
164+
}
165+
166+
if (value.matches("-?\\d+")) {
167+
return Integer.valueOf(value); // Integer
166168
}
167-
168-
String trimmed = value.trim();
169-
170-
try {
171-
return Integer.valueOf(trimmed);
172-
} catch (NumberFormatException e) {
169+
170+
if (value.matches("-?\\d*\\.\\d+")) {
171+
return Double.valueOf(value);
173172
}
174-
175-
if ("true".equalsIgnoreCase(trimmed)) {
173+
174+
if ("true".equalsIgnoreCase(value)) {
176175
return Boolean.TRUE;
177-
} else if ("false".equalsIgnoreCase(trimmed)) {
176+
} else if ("false".equalsIgnoreCase(value)) {
178177
return Boolean.FALSE;
179178
}
180-
181-
try {
182-
return Double.valueOf(trimmed);
183-
} catch (NumberFormatException e) {
184-
}
185-
186-
return value;
179+
180+
return value;
187181
}
188-
}
182+
183+
}

src/test/java/org/casbin/ClientTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,10 @@ public void testKeyMatch5() {
332332
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/keymatch5_model.conf", "-p", "examples/keymatch5_policy.csv", "alice", "/alice_data/hello/123", "GET"}), "{\"allow\":true,\"explain\":[\"alice\",\"/alice_data/{resource}/.*\",\"GET\"]}");
333333
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/keymatch5_model.conf", "-p", "examples/keymatch5_policy.csv", "alice", "/alice_data/hello/123", "POST"}), "{\"allow\":false,\"explain\":[]}");
334334
}
335+
336+
@Test
337+
public void testStringVsIntegerParameter() {
338+
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/string_vs_integer_model.conf", "-p", "examples/string_vs_integer_policy.csv", "alice", "data", "read", "\"1\""}), "{\"allow\":true,\"explain\":[\"reader\",\"data\",\"read\",\"1\"]}");
339+
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/string_vs_integer_model.conf", "-p", "examples/string_vs_integer_policy.csv", "alice", "data", "read", "1"}), "{\"allow\":false,\"explain\":[]}");
340+
}
335341
}

0 commit comments

Comments
 (0)