Example JSON Patch of replace EReference operation #200
-
Hi, Example model component in the server: "transitions": [
{
"$id": "//@transitions.0",
"description": "fromRedtoYellow",
"input": {
"$type": "file:/<skipped>/statemachine.ecore#//Input",
"$ref": "//@inputs.1"
},
"from": {
"$type": "file:/<skipped>/statemachine.ecore#//State",
"$ref": "//@states.0"
},
"to": {
"$type": "file:/<skipped>/statemachine.ecore#//State",
"$ref": "//@states.2"
}
}
] If I want to change the I've tried the following with a replace patch: {
"data": {
"type": "modelserver.patch",
"data": [{
"op":"replace",
"path":"/transitions/0/input/$ref",
"value": "//@inputs.2"
}]
}
} And it complains about If the replacing value is the object: {
"data": {
"type": "modelserver.patch",
"data": [{
"op":"replace",
"path":"/transitions/0/input/",
"value": {
"$type" : "http://nl.vu.cs.bumble/statemachine#//Input",
"$ref" : "@inputs.1"
}
}]
}
} The server's response is to remove the input part: {
"type": "success",
"data": {
"message": "Model 'TrafficSignals.statemachine' successfully updated",
"patch": [
{
"op": "remove",
"path": "/transitions/0/input"
}
],
"allPatches": [
{
"modelUri": "file:/Users/yunabell/Desktop/bumble/emfcloud-modelserver-collaboration-plugin/examples/org.eclipse.emfcloud.modelserver.example/.temp/workspace/TrafficSignals.statemachine",
"patch": [
{
"op": "remove",
"path": "/transitions/0/input"
}
]
}
]
}
} I also tried using an add patch: {
"data": {
"type": "modelserver.patch",
"data": [{
"op":"add",
"path":"/transitions/0/input/",
"value": {
"$type": "http://nl.vu.cs.bumble/statemachine#//Input",
"$ref": "$command.exec.res#//@changeReference/@inputs.2"
}
}]
}
} Although it responds "success", the reference is missing: "type": "success",
"data": {
"message": "Model 'TrafficSignals.statemachine' successfully updated",
"patch": [
{
"op": "replace",
"path": "/transitions/0/input/$ref",
"value": "#//"
}
], How I should fix it? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, $ref is used to identify (referenced) objects, but should be omitted in the path you're trying to edit (It's a metadata attribute generated for Json-messages, but doesn't actually exist in the model on the server side). So the operation path should look like this: {
"path": "object-id/input"
} The value should also omit the $type attribute (although this probably doesn't matter). The server already knows the type of existing objects; the $type attribute is explicitly added for clients, again because json/javascript clients don't have this information (You would need to specify the $type when creating new objects, though) So the operation should look like this: Using json-patch pointers: {
"op":"replace",
"path":"/transitions/0/input",
"value": {
"$ref": "//@inputs.2"
}
} Or using EMF-IDs: {
"op":"replace",
"path":"//@transitions.0/input",
"value": {
"$ref": "//@inputs.2"
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi, A replace operation is equivalent to a remove, followed by an add. It seems that the remove works fine (As shown by the result), but the add fails. This could be caused in some cases, where the value is incorrectly specified (So the value is set to null instead of throwing an exception or setting the correct value). You can try the following, to make sure the object URI is properly resolved: {
"op":"replace",
"path":"#//@transitions.0/input",
"value": {
"$ref" : "#//@inputs.1"
}
} EMF-like paths should include the fragment symbol (#) to distinguish them from Json Pointers, otherwise the Invalid Object Property error will be thrown. You can also try specifying the full URI, including the model URI (resource URI): {
"op":"replace",
"path":"<yourmodeluri>#//@transitions.0/input",
"value": {
"$ref" : "<yourmodeluri>#//@inputs.1"
}
} because I'm not sure we support the case where the resource URI is implicit. Note: to avoid weird syntactic issues like this, you should use the utility methods provided on the client, whenever possible. On the Typescript modelserver-client, there is a patch-utils module providing a "replace" function, where you can directly specify the objets you are editing. The library will be able to build the correct paths from these objects, limiting the risk of syntactic errors in the patch. This library builds all EMF paths like this:
Or simply:
for referencing objects (when you don't need a feature) |
Beta Was this translation helpful? Give feedback.
Hi,
A replace operation is equivalent to a remove, followed by an add. It seems that the remove works fine (As shown by the result), but the add fails. This could be caused in some cases, where the value is incorrectly specified (So the value is set to null instead of throwing an exception or setting the correct value).
You can try the following, to make sure the object URI is properly resolved:
EMF-like paths should include the fragment symbol (#) to distinguish them from Json Pointers, otherwise the Invalid Object Property error will be thrown.
You can also t…