Fix ValueError when metric is not defined in AI Search NewServerEventSubscriber
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3548193. --> Reported by: [tmiguelv](https://www.drupal.org/user/3612421) Related to !901 !898 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>When creating a new search server through the AI Search module, a ValueError is thrown if the metric configuration is not properly defined in the backend configuration like in the Azure Search VDB Provider. The error occurs in NewServerEventSubscriber.php at line 53:</p> <p><code>ValueError: "0" is not a valid backing value for enum Drupal\ai\Enum\VdbSimilarityMetrics in Drupal\ai\Enum\VdbSimilarityMetrics::from()</code></p> <p>This happens because the code attempts to create a VdbSimilarityMetrics enum from a value that may be undefined, null, or an invalid value (like "0"), causing the application to crash during server creation.</p> <h4 id="summary-steps-reproduce">Steps to reproduce (required for bugs, but not feature requests)</h4> <p>Please provide information like AI modules enabled, which AI provider, browser, etc.</p> <ol> <li>Create a new search server using the AI Search module</li> <li>Leave the metric configuration undefined or set to an invalid value like with Azure Search VDB Provider module</li> <li>Save the server configuration</li> <li>The ValueError is thrown during the collection creation process</li> </ol> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Add validation to check if the metric is properly defined before attempting to create the enum. If the metric is not defined or invalid, fall back to a sensible default (CosineSimilarity).</p>
issue