@@ -664,6 +664,7 @@ instance (FromJSON a) => FromJSON [a] where
664664prependContext :: String -> Parser a -> Parser a
665665prependContext name = prependFailure ("parsing " ++ name ++ " failed, ")
666666
667+
667668-- | @'withObject' name f value@ applies @f@ to the 'Object' when @value@
668669-- is an 'Data.Aeson.Object' and fails otherwise.
669670--
@@ -910,6 +911,12 @@ type ConName = String
910911contextType :: TypeName -> Parser a -> Parser a
911912contextType = prependContext
912913
914+-- | Add the tagKey that will be looked up while building an ADT
915+contextTag :: Text -> Parser a -> Parser a
916+contextTag tagKey = prependFailure
917+ ("Missing required tagKey \"" ++ unpack tagKey ++
918+ "\". The parser returned error was: ")
919+
913920-- | Add the name of the constructor being parsed to a parser's error messages.
914921contextCons :: ConName -> TypeName -> Parser a -> Parser a
915922contextCons cname tname = prependContext (showCons cname tname)
@@ -1087,7 +1094,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
10871094 case sumEncoding opts of
10881095 TaggedObject{..} ->
10891096 withObject tname $ \obj -> do
1090− tag <- contextType tname $ obj .: tagKey
1097+ tag <- (contextType tname) . (contextTag tagKey) $ obj .: tagKey
10911098 fromMaybe (badTag tag <?> Key tagKey) $
10921099 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
10931100 where