Skip to content

Commit b354378

Browse files
authored
Improve handling for remove and setting retrieval
Improve handling of remove patches without an index but which have an container. When retrieving settings from emf like paths, the code should first check whether an object can be retrieved and only then do some path checks. This way paths containing only an object id won't fail.
1 parent 74f9937 commit b354378

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

bundles/org.eclipse.emfcloud.modelserver.common/src/org/eclipse/emfcloud/modelserver/common/patch/AbstractJsonPatchHelper.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -187,47 +187,38 @@ protected Command getRemoveCommand(final String modelURI, final ResourceSet reso
187187
String jsonPath = path.asText();
188188
SettingValue setting = getSetting(modelURI, resourceSet, jsonPath);
189189

190+
// If neither the index nor the feature are present, we delete an Object
191+
EObject eObject = setting.getEObject();
192+
190193
// If the index is present, we remove a value from a list
191194
if (setting.getIndex().isPresent()) {
192195
int index = setting.getIndex().get();
193196
if (index == CommandParameter.NO_INDEX) {
194197
// If the index is "-", remove the last value from the list
195-
Object currentValue = setting.getEObject().eGet(setting.getFeature());
198+
Object currentValue = eObject.eGet(setting.getFeature());
196199
if (currentValue instanceof Collection) {
197200
index = ((Collection<?>) currentValue).size() - 1;
198201
}
199202
}
200-
return RemoveCommand.create(getEditingDomain(setting.getEObject()), setting.getEObject(),
201-
setting.getFeature(), index);
203+
return RemoveCommand.create(getEditingDomain(eObject), eObject, setting.getFeature(), index);
204+
}
205+
206+
// if we are in a feature which is a list, remove the element from said feature
207+
if (eObject.eContainmentFeature() != null && eObject.eContainmentFeature().isMany()) {
208+
return RemoveCommand.create(getEditingDomain(eObject), eObject.eContainer(), eObject.eContainmentFeature(),
209+
eObject);
202210
}
203211

204212
// If the feature is present, but index is absent, we unset the value (i.e. set it to default value)
205213
if (setting.getFeature() != null) {
206-
Object defaultValue = getDefaultValue(setting.getFeature());
207-
return SetCommand.create(getEditingDomain(setting.getEObject()), setting.getEObject(), setting.getFeature(),
208-
defaultValue);
214+
return SetCommand.create(getEditingDomain(eObject), eObject, setting.getFeature(), SetCommand.UNSET_VALUE);
209215
}
210216

211-
// If neither the index nor the feature are present, we delete an Object
212-
EObject eObjectToDelete = setting.getEObject();
213-
214-
Command deleteCommand = DeleteCommand.create(getEditingDomain(eObjectToDelete), eObjectToDelete);
217+
Command deleteCommand = DeleteCommand.create(getEditingDomain(eObject), eObject);
215218

216219
return deleteCommand;
217220
}
218221

219-
protected Object getDefaultValue(final EStructuralFeature feature) {
220-
Object defaultValue = feature.getDefaultValue();
221-
if (defaultValue == null && feature.isMany()) {
222-
// Special case for lists default value.
223-
// If the feature represents a collection, and 'null' is specified as the default
224-
// value, we need to use an empty collection instead. Otherwise, SetCommand(object, feature, null)
225-
// will cause a NPE.
226-
return Collections.emptyList();
227-
}
228-
return defaultValue;
229-
}
230-
231222
protected Command getAddCommand(final String modelURI, final ResourceSet resourceSet, final JsonNode patchAction)
232223
throws JsonPatchException {
233224
JsonNode path = patchAction.get(PATH);
@@ -599,18 +590,17 @@ protected Object getValueFromList(final String jsonPath, final String[] segments
599590
*/
600591
protected SettingValue getSettingFromCustomPath(final String modelURI, final String jsonPath)
601592
throws JsonPatchException {
602-
int lastSegmentPos = jsonPath.lastIndexOf('/');
603-
if (lastSegmentPos < 0 || lastSegmentPos >= jsonPath.length()) {
604-
throw new JsonPatchException("Failed to parse Json Path: " + jsonPath);
605-
}
606-
607593
// The path may represent an EObject (without feature or index). In that case,
608594
// directly return it.
609595
EObject eObject = getEObject(modelURI, URI.createURI(jsonPath));
610596
if (eObject != null) {
611597
return new SettingValue(eObject);
612598
}
613599

600+
int lastSegmentPos = jsonPath.lastIndexOf('/');
601+
if (lastSegmentPos < 0 || lastSegmentPos >= (jsonPath.length() - 1)) {
602+
throw new JsonPatchException("Failed to parse Json Path: " + jsonPath);
603+
}
614604
// XXX if the edited object is the root element, its positional URI will be '/'
615605
// Since we expect path as <id>/<feature>, the expected path in that case is
616606
// "//feature" and not "/feature" (Although "/feature" would be a valid Json Pointer path)

0 commit comments

Comments
 (0)