Skip to content

fix: Prevent crash from duplicate category pattern keys#223

Merged
dcalhoun merged 1 commit intotrunkfrom
fix/duplicate-pattern-category-keys-crash
Nov 7, 2025
Merged

fix: Prevent crash from duplicate category pattern keys#223
dcalhoun merged 1 commit intotrunkfrom
fix/duplicate-pattern-category-keys-crash

Conversation

@dcalhoun
Copy link
Member

@dcalhoun dcalhoun commented Nov 7, 2025

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 with Dictionary(_:uniquingKeysWith:), which gracefully handles duplicate keys by keeping the first label value and discarding subsequent duplicates.

Before:

let categoryLabels = Dictionary(
    uniqueKeysWithValues: patternCategories.map { ($0.name, $0.label) }
)

After:

let categoryLabels = Dictionary(
    patternCategories.map { ($0.name, $0.label) },
    uniquingKeysWith: { first, _ in first }
)

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.

  1. Clone the WordPress-iOS app integration logic: Integrate Native Block Inserter WordPress-iOS#24708
  2. Configure the project to use the local GutenbergKit repository.
  3. Run the WP-iOS app.
  4. Enable the native inserter.
  5. Open the post editor for a WPCOM Simple site.
  6. Tap the block inserter (+) button.
  7. Tap the patterns button in the top-right corner.

Accessibility Testing Instructions

N/A no UI changes.

Screenshots or screencast

N/A no visual changes.

A fatal error was thrown when the logic encountered keys of the same
value twice.
@dcalhoun dcalhoun added the [Type] Bug An existing feature does not function as intended label Nov 7, 2025
@dcalhoun dcalhoun marked this pull request as ready for review November 7, 2025 19:05
@dcalhoun dcalhoun enabled auto-merge (squash) November 7, 2025 19:33
@dcalhoun dcalhoun merged commit 28d6bd0 into trunk Nov 7, 2025
11 checks passed
@dcalhoun dcalhoun deleted the fix/duplicate-pattern-category-keys-crash branch November 7, 2025 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants