fix(ios): safe AnyHashable-to-String cast in notification userInfo handling#11
Closed
sergio-silva-dito wants to merge 1 commit intomainfrom
Closed
fix(ios): safe AnyHashable-to-String cast in notification userInfo handling#11sergio-silva-dito wants to merge 1 commit intomainfrom
sergio-silva-dito wants to merge 1 commit intomainfrom
Conversation
…ndling Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Author
|
Substituído por abordagem mais simples: manter source como [AnyHashable: Any] evitando conversão desnecessária e perda de chaves. |
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.
Contexto
O plugin iOS recebe payloads de notificação via APNs com o tipo
[AnyHashable: Any]— padrão do sistema para dicionários que chegam do Objective-C. As chaves nesses dicionários são tipicamenteNSStringbridgeadas, nãoSwift.Stringnativas.O problema central é que cast bulk de
[AnyHashable: Any]para[String: Any]não é garantido em Swift: ele exige que todas as chaves já sejamSwift.Stringnativos, sem a canonicalização automática queAnyHashableoferece individualmente. Se qualquer chave não passar nessa verificação, o cast inteiro falha e retornanil.Problemas corrigidos
1.
emitNotificationClickEvent— inconsistência de tipos no fallbackSe
userInfo["data"]não puder ser castado para[String: Any], o fallback era o própriouserInfotipado como[AnyHashable: Any]. Isso causava uma inconsistência de tipos:sourceseria[AnyHashable: Any], mas os acessos subsequentes (source["notification"],source["reference"], etc.) dependem de chavesString— sem garantia de funcionamento correto dependendo do runtime.2.
channelFromUserInfo— notificações Dito potencialmente ignoradasO cast bulk para
[String: Any]pode falhar com payloads APNs reais. Se isso acontecer, a função retornanil,isDitoChannelretornafalsee todas as notificações Dito são silenciosamente ignoradas.Solução
Helper
toStringKeyedAdicionado um helper privado que converte
[AnyHashable: Any]para[String: Any]de forma segura, chave a chave:O pior caso é descartar uma chave individual que genuinamente não seja
String— nunca o payload inteiro.channelFromUserInfoO cast intermediário passa a usar
[AnyHashable: Any], que é sempre seguro para subdicionários vindos do APNs. O acesso subsequentedata["channel"] as? Stringpermanece correto porqueAnyHashablecanonicaliza tipos bridgeados do Foundation individualmente: ao criar umAnyHashablea partir de umNSString, Swift o normaliza internamente paraSwift.Stringantes de calcular hash e comparar igualdade. Assim, o subscript com umaStringSwift encontra corretamente uma chave armazenada comoNSString, e o cast escalaras? Stringfaz o bridge automaticamente.Essa canonicalização existe apenas no acesso individual via subscript — não no cast bulk de dicionário (
as? [String: Any]), que é exatamente o problema que esta correção evita.emitNotificationClickEventSubstituído o cast frágil pelo helper
toStringKeyed, eliminando a inconsistência de tipos:Test plan
notificationId,reference,logId,notificationName,userId) chegam corretamente no Flutter🤖 Generated with Claude Code