|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2022 the original author or authors. |
| 2 | + * Copyright 2013-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
31 | 31 | import org.springframework.expression.TypedValue;
|
32 | 32 | import org.springframework.lang.Nullable;
|
33 | 33 | import org.springframework.util.Assert;
|
| 34 | +import org.springframework.util.StringUtils; |
34 | 35 |
|
35 | 36 | /**
|
36 | 37 | * A SpEL {@link PropertyAccessor} that knows how to read properties from JSON objects.
|
|
41 | 42 | * @author Paul Martin
|
42 | 43 | * @author Gary Russell
|
43 | 44 | * @author Pierre Lakreb
|
| 45 | + * @author Vladislav Fefelov |
44 | 46 | *
|
45 | 47 | * @since 3.0
|
46 | 48 | */
|
@@ -109,6 +111,9 @@ else if (target instanceof String) {
|
109 | 111 | * Return an integer if the String property name can be parsed as an int, or null otherwise.
|
110 | 112 | */
|
111 | 113 | private static Integer maybeIndex(String name) {
|
| 114 | + if (!isNumeric(name)) { |
| 115 | + return null; |
| 116 | + } |
112 | 117 | try {
|
113 | 118 | return Integer.valueOf(name);
|
114 | 119 | }
|
@@ -139,6 +144,22 @@ public void write(EvaluationContext context, Object target, String name, Object
|
139 | 144 | throw new UnsupportedOperationException("Write is not supported");
|
140 | 145 | }
|
141 | 146 |
|
| 147 | + /** |
| 148 | + * Check if the string is a numeric representation (all digits) or not. |
| 149 | + */ |
| 150 | + private static boolean isNumeric(String str) { |
| 151 | + if (!StringUtils.hasLength(str)) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + int length = str.length(); |
| 155 | + for (int i = 0; i < length; i++) { |
| 156 | + if (!Character.isDigit(str.charAt(i))) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
142 | 163 | private static TypedValue typedValue(JsonNode json) throws AccessException {
|
143 | 164 | if (json == null) {
|
144 | 165 | return TypedValue.NULL;
|
|
0 commit comments