Patch for new component added

Helena Foo 66 Reputation points
2022-10-03T11:31:04.307+00:00

I'm trying to follow this guide how-to-manage-twin on how do patch a twin after updating a model.

The model has been updated with a new component that was not there before, The component is an object, with two properties (type double).

First I understand I have to add a path to the component properties,

[  
  {  
    "op": "add",   
    "path": "/ObjectProperty",   
    "value": {"Property1":"0", "Property2":"0"}  
  }  
]  

But I am also receiving an error message saying "ObjectProperty on Twin is missing $metadata section". Do I need to add a path or initialize a value for the metadata of this component as well?

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
220 questions
0 comments No comments
{count} votes

Accepted answer
  1. Matthijs van der Veer 4,376 Reputation points MVP
    2022-10-03T12:05:00.46+00:00

    It should be impossible to introduce a component in a model update without initializing it right away, from your question it's not clear to me whether that's the challenge you're facing.

    However, when you update the model and introduce a new component, you can do that with a single patch call with multiple patches. One entry to update the model and another entry to add the component. Whether you add an empty component, or one with values, you also need to add an empty $metadata model at the least. So in your example it will look something like this:

    [  
        {  
            "op": "replace",  
            "path": "/$metadata/$model",  
            "value": "dtmi:your:new:model;2"  
        },  
        {  
            "op": "add",  
            "path": "/ObjectProperty",  
            "value": {  
                "Property1": 0,  
                "Property2": 0,  
                "$metadata": {}  
            }  
        }  
    ]  
    

    Small note: you're patching a component that has properties of type double, but you're adding string values. I'm not sure if that works, so the example above is posted as numbers instead.


0 additional answers

Sort by: Most helpful