Skip to content

Commit

Permalink
[RNMobile] Fix encoding of MediaInfo metadata property (iOS) (#49304
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
fluiddot authored Mar 24, 2023
1 parent 4d560c8 commit 5272c2c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
21 changes: 4 additions & 17 deletions packages/react-native-bridge/ios/GutenbergBridgeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

1 comment on commit 5272c2c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 5272c2c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4509297701
📝 Reported issues:

Please sign in to comment.