Skip to content
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

feat(storage): allow user to delete custom metadata to match underlying SDKs behavior #12508

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SettableMetadata {
final String? contentType;

/// Additional user-defined custom metadata.
final Map<String, String>? customMetadata;
final Map<String, String?>? customMetadata;

/// Returns the settable metadata as a [Map].
Map<String, dynamic> asMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,26 @@ storage_interop.SettableMetadata settableMetadataToFbSettableMetadata(
storage_interop.UploadMetadata settableMetadataToFbUploadMetadata(
SettableMetadata metadata,
{String? md5Hash}) {
// We are removing null values from the customMetadata map.
// since we are setting the whole map, whereas in Android and iOS we can
// set individual keys.
final listOfCustomMetadata = metadata.customMetadata?.entries
.where((entry) => entry.value != null)
.map((entry) => MapEntry(entry.key, entry.value))
.cast<MapEntry<String, String>>();

final Map<String, String>? filteredCustomMetadata =
listOfCustomMetadata != null
? Map.fromEntries(listOfCustomMetadata)
: null;

return storage_interop.UploadMetadata(
cacheControl: metadata.cacheControl,
contentDisposition: metadata.contentDisposition,
contentEncoding: metadata.contentEncoding,
contentLanguage: metadata.contentLanguage,
contentType: metadata.contentType,
customMetadata: metadata.customMetadata,
customMetadata: filteredCustomMetadata,
md5Hash: md5Hash,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SettableMetadataCache {
return _cache;
}

final newMetadata = <String, String>{
final newMetadata = <String, String?>{
...?incoming.customMetadata,
...?_cache.customMetadata,
};
Expand Down
21 changes: 21 additions & 0 deletions tests/integration_test/firebase_storage/reference_e2e.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,27 @@ void setupReferenceTests() {
expect(fullMetadata.customMetadata!['foo'], 'bar');
});

test('delete metadata', () async {
Reference ref = storage.ref('flutter-tests').child('flt-ok.txt');
FullMetadata fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar'}));
expect(fullMetadata.customMetadata!['foo'], 'bar');

fullMetadata = await ref.updateMetadata(
SettableMetadata(
customMetadata: {
'foo': null,
},
),
);
expect(fullMetadata.customMetadata!['foo'], isNull);

// Setting it again
fullMetadata = await ref
.updateMetadata(SettableMetadata(customMetadata: {'foo': 'bar2'}));
expect(fullMetadata.customMetadata!['foo'], 'bar2');
});

test('errors if property does not exist', () async {
Reference ref = storage.ref('flutter-tests/iDoNotExist.jpeg');

Expand Down