Fix missing message_id in api command on json rpc
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sun, 2 Nov 2025 17:45:23 +0000 (18:45 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sun, 2 Nov 2025 17:45:23 +0000 (18:45 +0100)
music_assistant/controllers/webserver.py

index ffa91ef97019561f87b8fba37083861b47b218f6..30d8ca9efb913a72c5ebc97056adc4bbadd07b0f 100644 (file)
@@ -19,6 +19,7 @@ from typing import TYPE_CHECKING, Any, Final
 
 import aiofiles
 from aiohttp import WSMsgType, web
+from mashumaro.exceptions import MissingField
 from music_assistant_frontend import where as locate_frontend
 from music_assistant_models.api import (
     CommandMessage,
@@ -34,7 +35,7 @@ from music_assistant.constants import CONF_BIND_IP, CONF_BIND_PORT, VERBOSE_LOG_
 from music_assistant.helpers.api import APICommandHandler, parse_arguments
 from music_assistant.helpers.api_docs import generate_openapi_spec
 from music_assistant.helpers.audio import get_preview_stream
-from music_assistant.helpers.json import json_dumps
+from music_assistant.helpers.json import json_dumps, json_loads
 from music_assistant.helpers.util import get_ip_addresses
 from music_assistant.helpers.webserver import Webserver
 from music_assistant.models.core_controller import CoreController
@@ -247,6 +248,16 @@ class WebserverController(CoreController):
             error = f"Invalid JSON: {cmd_data}"
             self.logger.error("Unhandled JSONRPC API error: %s", error)
             return web.Response(status=400, text=error)
+        except MissingField as e:
+            # be forgiving if message_id is missing
+            cmd_data_dict = json_loads(cmd_data)
+            if e.field_name == "message_id" and "command" in cmd_data_dict:
+                cmd_data_dict["message_id"] = "unknown"
+                command_msg = CommandMessage.from_dict(cmd_data_dict)
+            else:
+                error = f"Missing field in JSON: {e!s}"
+                self.logger.error("Unhandled JSONRPC API error: %s", error)
+                return web.Response(status=400, text=error)
 
         # work out handler for the given path/command
         handler = self.mass.command_handlers.get(command_msg.command)