The Prophet ﷺ said, "When one of you is invited to a meal while he is fasting, let him say, 'I am fasting.'"
عن النبي ﷺ قال: إذا دعي أحدكم إلى طعام وهو صائم، فليقل: إني صائم.
Muslim narrated it in the Book of Fasting (1150) through the route of Sufyan bin Uyayna, from Abu Zinad, from Aaraj, from Abu Hurairah, as mentioned.Nawawi said in Sharh Muslim (9/235), "There is no disagreement that the fasting person is not obligated to eat. But if his fast is obligatory, it is not permissible for him to eat, because one may not withdraw from an obligatory act. If it is voluntary, then breaking the fast or continuing it are both permissible. If continuing the fast would be difficult for the host of the food, then breaking it is preferable, otherwise completing the fast is preferable. And Allah knows best."