GeezerVison is showing github.com — setMetadata() method merges existing with new data? · Issue #597 · googleapis/google-cloud-node
setMetadata() method merges existing with new data? #597Labels api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.type: questionRequest for information or clarification. Not an issue. DescriptionLooks like if I call setMetadata on an object, it merges the data with what's in production...
var bucket = ...; // Get a bucket somehow.
var file = bucket.file('my-file');
file.setMetadata({
metadata: {
first: 'first'
}
});
file.setMetadata({
metadata: {
second: 'second'
}
}, function(err, metadata) {
// metadata here is {first: 'first', second: 'second'}
// Expected it to be just {second: 'second'} ....
});
I suspect that we can't .. fix this all that much... but maybe we can at least document it ?
The options we have so far:
- Make metadata immutable: remove
setMetadata
- Operate on a single attribute at a time:
getMetadata(key) and setMetadata(key, value)
- Rename
setMetadata to patchMetadata or mergeMetadata
- Add some documentation about the behavior of
setMetadata
/cc @Capstan @aozarov Activityryanseys commented on May 13, 2015 Remember #459? Yeah we switched to PATCH because using PUT destroyed ACLs with BigQuery, making our datasets inaccessible. I'm not sure if this is also the case with Storage but I feel it would be appropriate to remain consistent across our APIs in this library. ryanseys commented on May 13, 2015 We can still document it for our users. If they want to delete an existing value, I believe the correct way is to set it to null. aozarov commented on May 13, 2015 ryanseys commented on May 13, 2015
I doubt if doing it (delete and set) should be done by the library as it can fail in the middle and leave it in an undesired state.
The API call operation is atomic so either the whole thing succeeds or fails. ryanseys commented on May 13, 2015 I think I see what you were saying. I meant that using PATCH you can delete existing values by setting them to null. We never make 2 calls for setting metadata. That requires you to know about the ones are set already, which is a getMetadata call away. We leave this as an exercise for the user if they want to do that. aozarov commented on May 13, 2015 What I was saying is that we should probably not do the following:
func setMetadata(blob, metadata) {
rpc.patch(blob, {"metadata": null})
rpc.patch(blob, {"metadata": metadata})
}
ryanseys commented on May 13, 2015 Oh we'd never do that. The end result is the same as just rpc.patch(blob, {"metadata": metadata})
I meant if you wanted to set completely new metadata, including destroying the ones currently set, you need to know the existing ones and set them to null so as to achieve the same result as using PUT. ryanseys commented on May 13, 2015 Like this:
// metadata is currently { a: '1', b: '2', c: '3' };
rpc.patch(blob, { metadata: { a: 'x', b: null, c: null } }) // need to know b and c exist so you can set them to null
// metadata is now { a: 'x' };
This is the same as:
// metadata is currently { a: '1', b: '2', c: '3' };
rpc.put(blob, { metadata: { a: 'x' } })
// metadata is now { a: 'x' };aozarov commented on May 13, 2015 jgeewax commented on May 13, 2015 ContributorAuthor More actions So back to the main issue:
My concern here is that set typically means "replace the value up there with the value I'm sending from down here". set to us means... "merge this value from down here into whatever data exists up there."
Can we rename anything ? or are we just OK with adding some docs that explain this? Or do we try to get Google to fix something ? What makes the most sense as a user of this library? aozarov commented on May 13, 2015 I doubt that a function that can only append is that useful.
One options (until fixed by the backed) would be to make it a read-only property which can only be set when creating a new file. Once apiary fix the issue property can become again read-write. added a commit that references this issue on Feb 2, 2026This issue will close once commit 7fcfd40 is merged into the 'main' branch.7fcfd40 added a commit that references this issue on Feb 3, 2026This issue will close once commit 88af461 is merged into the 'main' branch.88af461
MetadataMetadataapi: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.type: questionRequest for information or clarification. Not an issue. DevelopmentNo branches or pull requestsIssue actions
|