-
Notifications
You must be signed in to change notification settings - Fork 331
Add support for updating JacksonEvent array elements #5772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5156e93
7ef4438
9d23c81
e43373f
916bd6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,8 @@ public class JacksonEvent implements Event { | |
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(JacksonEvent.class); | ||
|
|
||
| private static final int FILL_OUT_OF_BOUNDS_ELEMENTS_LIMIT = 0; | ||
|
|
||
| private static final String SEPARATOR = "/"; | ||
|
|
||
| private static final ObjectMapper mapper = JsonMapper.builder() | ||
|
|
@@ -195,8 +197,31 @@ private JsonNode getOrCreateNode(final JsonNode node, final String key) { | |
| JsonNode childNode = node.get(key); | ||
| if (childNode == null) { | ||
| childNode = mapper.createObjectNode(); | ||
| ((ObjectNode) node).set(key, childNode); | ||
| if (node.isArray()) { | ||
| int index = Integer.parseInt(key); | ||
| ArrayNode arrayNode = (ArrayNode) node; | ||
|
|
||
| int distanceFromArrayEnd = index - arrayNode.size(); | ||
| if (distanceFromArrayEnd >= FILL_OUT_OF_BOUNDS_ELEMENTS_LIMIT + 1) { | ||
| throw new IndexOutOfBoundsException( | ||
| String.format("Cannot expand array past the limit of size %s to reach index %s", arrayNode.size(), index)); | ||
| } | ||
| while (arrayNode.size() <= index) { | ||
| arrayNode.addNull(); | ||
| } | ||
|
Comment on lines
+209
to
+211
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have to fill nulls? You have null check in the below logic anyway right?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We decided to not allow extending the size of array by filling nulls. We will allow at most one more entry beyond the current size |
||
|
|
||
| JsonNode existing = arrayNode.get(index); | ||
| if (existing == null || !existing.isObject()) { | ||
| childNode = mapper.createObjectNode(); | ||
| arrayNode.set(index, childNode); | ||
| } else { | ||
| childNode = existing; | ||
| } | ||
| } else { | ||
| ((ObjectNode) node).set(key, childNode); | ||
| } | ||
| } | ||
|
|
||
| return childNode; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a test case for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think we should limit the number new null nodes can be added.