-
Notifications
You must be signed in to change notification settings - Fork 121
Decode meta_data incorrectly sent as an object
#16141
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
Changes from 11 commits
060141d
c574960
5746bc6
c66e524
2d989a2
fd8e12e
7696d51
d26c2be
177b484
fe6f10c
465ed5c
3d57295
ae1c3df
501b660
afe445f
fa134fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import Foundation | ||
| import WordPressShared | ||
| import NetworkingCore | ||
|
|
||
| /// Helper to extract specific data from inside `Product` metadata. | ||
| /// Sample Json: | ||
|
|
@@ -23,18 +24,33 @@ import WordPressShared | |
| /// | ||
| internal struct ProductMetadataExtractor: Decodable { | ||
|
|
||
| private typealias DecodableDictionary = [String: AnyDecodable] | ||
| private typealias AnyDictionary = [String: Any?] | ||
|
|
||
| /// Internal metadata representation | ||
| /// | ||
| private let metadata: [DecodableDictionary] | ||
| private let metadata: [MetaData] | ||
|
|
||
| /// Decode main metadata array as an untyped dictionary. | ||
| /// Initialize with already-decoded metadata array | ||
| /// | ||
| init(metadata: [MetaData]) { | ||
| self.metadata = metadata | ||
| } | ||
|
|
||
| /// Decode main metadata supporting both array and object formats. | ||
| /// This expects to decode the raw metadata array/object, not a JSON object with a meta_data field. | ||
| /// | ||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.singleValueContainer() | ||
| self.metadata = try container.decode([DecodableDictionary].self) | ||
|
|
||
| // Try to decode as array first (standard format) | ||
| if let metaDataArray = try? container.decode([MetaData].self) { | ||
| self.metadata = metaDataArray | ||
| } else if let metaDataDict = try? container.decode([String: MetaData].self) { | ||
| // Try to decode as object keyed by index strings | ||
| self.metadata = Array(metaDataDict.values) | ||
| } else { | ||
| self.metadata = [] | ||
| } | ||
| } | ||
|
|
||
| /// Searches product metadata for subscription data and converts it to a `ProductSubscription` if possible. | ||
|
|
@@ -62,22 +78,25 @@ internal struct ProductMetadataExtractor: Decodable { | |
|
|
||
| /// Filters product metadata using the provided prefix. | ||
| /// | ||
| private func filterMetadata(with prefix: String) -> [DecodableDictionary] { | ||
| metadata.filter { object in | ||
| let objectKey = object["key"]?.value as? String ?? "" | ||
| return objectKey.hasPrefix(prefix) | ||
| } | ||
| private func filterMetadata(with prefix: String) -> [MetaData] { | ||
| metadata.filter { $0.key.hasPrefix(prefix) } | ||
| } | ||
|
|
||
| /// Parses provided metadata to return a dictionary with each metadata object's key and value. | ||
| /// | ||
| private func getKeyValueDictionary(from metadata: [DecodableDictionary]) -> AnyDictionary { | ||
| metadata.reduce(AnyDictionary()) { (dict, object) in | ||
| var newDict = dict | ||
| let objectKey = object["key"]?.value as? String ?? "" | ||
| let objectValue = object["value"]?.value | ||
| newDict.updateValue(objectValue, forKey: objectKey) | ||
| return newDict | ||
| private func getKeyValueDictionary(from metadata: [MetaData]) -> AnyDictionary { | ||
| metadata.reduce(into: AnyDictionary()) { dict, object in | ||
|
||
| // For JSON values, decode them to get the actual object | ||
| if object.value.isJson { | ||
| if let data = object.value.stringValue.data(using: .utf8), | ||
| let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) { | ||
| dict[object.key] = jsonObject | ||
| } else { | ||
| dict[object.key] = object.value.stringValue | ||
| } | ||
| } else { | ||
| dict[object.key] = object.value.stringValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||||||
| import Foundation | ||||||||||
|
|
||||||||||
| /// Extension to support flexible decoding of MetaData arrays | ||||||||||
| /// Handles both standard array format and object format keyed by index strings | ||||||||||
| extension Array where Element == MetaData { | ||||||||||
|
|
||||||||||
| /// Custom decoding from container that supports both array and dictionary formats | ||||||||||
| public static func decodeFlexibly<K>(from container: KeyedDecodingContainer<K>, | ||||||||||
| forKey key: KeyedDecodingContainer<K>.Key) -> [MetaData] { | ||||||||||
| // Try to decode as array first (standard format) | ||||||||||
| if let metaDataArray = try? container.decode([MetaData].self, forKey: key) { | ||||||||||
| return metaDataArray | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Try to decode as object keyed by index strings | ||||||||||
| if let metaDataDict = try? container.decode([String: MetaData].self, forKey: key) { | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was checking if a plugin could produce
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I'm wary of even doing this much workaround for plugins messing with fields that don't belong to them, to be honest... it seems like a short road to insanity trying to pair that with our type safe models. It'll continue to fail gracefully even if the format is more broken than this. |
||||||||||
| return Array(metaDataDict.values) | ||||||||||
|
||||||||||
| return Array(metaDataDict.values) | |
| return metaDataDict.sorted { (lhs, rhs) in | |
| (Int(lhs.key) ?? 0) < (Int(rhs.key) ?? 0) | |
| }.map { $0.value } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it's true, the docs suggest:
When iterated over, values appear in this collection in the same order as they occur in the dictionary’s key-value pairs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be worth logging a case where metadata decoding fails silently? It could help troubleshoot similar issues more easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was investigating why we need this duplicating logic, and I think the whole
init(from decoder: Decoder) throwsandDecodablefromProductMetadataExtractorcan be removed. I couldn't find any code paths that rely on it.ProductMetadataExtractoris used only throughinit(metadata:)initializer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I'll check that.
However, I tried a lot last night to have a sane, non-duplicative approach, and failed hard. It was down to the subtle differences of what data source we were trying to map, and how decoded it already was.
Hopefully the cold light of day will let me delete more of this.