From 5272c2c33acfa44f7d7e99075772795efff8104d Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 24 Mar 2023 09:37:54 +0100 Subject: [PATCH] [RNMobile] Fix encoding of `MediaInfo` `metadata` property (iOS) (#49304) * Encode metadata property manually in `encodeForJS` * Remove `encode` from MediaInfo We are not customizing the encoding of the MediaInfo so we can rely on the default implementation. * Change metadata type to Dictionary --- .../ios/GutenbergBridgeDelegate.swift | 21 ++++--------------- .../ios/RNReactNativeGutenbergBridge.swift | 3 ++- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift index ccfc207741e4a..9a2aefbfdf81b 100644 --- a/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift +++ b/packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift @@ -5,34 +5,21 @@ public struct MediaInfo: Encodable { public let title: String? public let caption: String? public let alt: String? - public let metadata: Encodable + public let metadata: [String: Any] private enum CodingKeys: String, CodingKey { - case id, url, type, title, caption, alt, metadata + case id, url, type, title, caption, alt } - public init(id: Int32?, url: String?, type: String?, caption: String? = nil, title: String? = nil, alt: String? = nil, metadata: Encodable? = nil) { + public init(id: Int32?, url: String?, type: String?, caption: String? = nil, title: String? = nil, alt: String? = nil, metadata: [String: Any] = [:]) { self.id = id self.url = url self.type = type self.caption = caption self.title = title self.alt = alt - self.metadata = metadata ?? [:] as [String: String] + self.metadata = metadata } - - public func encode (to encoder: Encoder) throws - { - var container = encoder.container (keyedBy: CodingKeys.self) - try container.encode (id, forKey: .id) - try container.encode (url, forKey: .url) - try container.encode (type, forKey: .type) - try container.encode (title, forKey: .title) - try container.encode (caption, forKey: .caption) - try container.encode (alt, forKey: .alt) - var metadataContainer = container.nestedUnkeyedContainer(forKey: .metadata) - try metadata.encode(to: metadataContainer.superEncoder()) - } } /// Definition of capabilities to enable in the Block Editor diff --git a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift index 162bdc3fec7d7..e751bbe4d838c 100644 --- a/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift +++ b/packages/react-native-bridge/ios/RNReactNativeGutenbergBridge.swift @@ -468,12 +468,13 @@ extension MediaInfo { func encodeForJS() -> [String: Any] { guard let data = try? JSONEncoder().encode(self), - let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else + var jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { assertionFailure("Encoding of MediaInfo failed") return [String: Any]() } + jsonObject["metadata"] = self.metadata return jsonObject } }