fix: Prevent crash from duplicate category pattern keys#223
Merged
Conversation
A fatal error was thrown when the logic encountered keys of the same value twice.
jkmassel
approved these changes
Nov 7, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A fatal error was thrown when the logic encountered keys of the same value twice.
What?
Fixed a crash in PatternsViewModel caused by duplicate pattern category keys when initializing the category labels dictionary.
Why?
The app was experiencing a fatal crash:
Swift/NativeDictionary.swift:792: Fatal error: Duplicate values for key: 'about' at PatternsViewModel.swift:13.This occurred when the JavaScript/Gutenberg code sent pattern categories with duplicate name values (e.g., multiple entries with the name "about"). The
Dictionary(uniqueKeysWithValues:)initializer requires all keys to be unique and crashes when duplicates are encountered, making the block inserter completely unusable when this condition occurred.How?
Replaced the crash-prone
Dictionary(uniqueKeysWithValues:)initializer withDictionary(_:uniquingKeysWith:), which gracefully handles duplicate keys by keeping the first label value and discarding subsequent duplicates.Before:
After:
This maintains the intended functionality of looking up localized category labels while preventing crashes when duplicate category names are present in the data.
Testing Instructions
Important
I only encountered the crash occurred when using a WPCOM Simple site, which presumably had duplicate keys.
GutenbergKitrepository.Accessibility Testing Instructions
N/A no UI changes.
Screenshots or screencast
N/A no visual changes.