From 12d75bbde14dfba0a7888d6b7c0837e49c42ba39 Mon Sep 17 00:00:00 2001
From: Alexander Turinske <aturinske@gitlab.com>
Date: Fri, 7 Mar 2025 16:09:01 -0800
Subject: [PATCH] Add fallback for yaml editor

- sometimes getModel returns nul and the ui errors
- add a check for this
- update tests

Changelog: fixed
---
 .../source_editor_security_policy_schema_ext.js |  6 ++++--
 ...ce_editor_security_policy_schema_ext_spec.js | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/app/assets/javascripts/editor/extensions/source_editor_security_policy_schema_ext.js b/app/assets/javascripts/editor/extensions/source_editor_security_policy_schema_ext.js
index 2949b6b6caca5..0c884d22b637d 100644
--- a/app/assets/javascripts/editor/extensions/source_editor_security_policy_schema_ext.js
+++ b/app/assets/javascripts/editor/extensions/source_editor_security_policy_schema_ext.js
@@ -2,6 +2,8 @@ import { registerSchema } from '~/ide/utils';
 import axios from '~/lib/utils/axios_utils';
 import { getBaseURL, joinPaths } from '~/lib/utils/url_utility';
 
+const DEFAULT_FILENAME = '*.yaml';
+
 export const getSecurityPolicyListUrl = ({ namespacePath, namespaceType = 'group' }) => {
   const isGroup = namespaceType === 'group';
   return joinPaths(
@@ -62,7 +64,7 @@ export class SecurityPolicySchemaExtension {
           namespaceType,
           policyType,
         });
-        const modelFileName = instance.getModel().uri.path.split('/').pop();
+        const modelFileName = instance.getModel()?.uri.path.split('/').pop() || DEFAULT_FILENAME;
 
         registerSchema({
           uri: getSecurityPolicySchemaUrl({ namespacePath, namespaceType }),
@@ -76,7 +78,7 @@ export class SecurityPolicySchemaExtension {
           namespacePath: projectPath,
           namespaceType: 'project',
         });
-        const modelFileName = instance.getModel().uri.path.split('/').pop();
+        const modelFileName = instance.getModel()?.uri.path.split('/').pop() || DEFAULT_FILENAME;
 
         registerSchema({
           uri,
diff --git a/spec/frontend/editor/source_editor_security_policy_schema_ext_spec.js b/spec/frontend/editor/source_editor_security_policy_schema_ext_spec.js
index 921ff586ead09..f37b0137d79d8 100644
--- a/spec/frontend/editor/source_editor_security_policy_schema_ext_spec.js
+++ b/spec/frontend/editor/source_editor_security_policy_schema_ext_spec.js
@@ -246,6 +246,23 @@ describe('SecurityPolicySchemaExtension', () => {
           fileMatch: ['policy.yml'],
         });
       });
+
+      it('registers the schema even if `getModel` fails', async () => {
+        instance.getModel = jest.fn().mockImplementation(() => undefined);
+
+        await instance.registerSecurityPolicyEditorSchema({
+          namespacePath: mockNamespacePath,
+          namespaceType: 'project',
+          policyType: 'scan_execution_policy',
+        });
+
+        expect(registerSchema).toHaveBeenCalledTimes(1);
+        expect(registerSchema).toHaveBeenCalledWith({
+          uri: `${TEST_HOST}/${mockNamespacePath}/-/security/policies/schema`,
+          schema: mockScanExecutionPolicyProperties,
+          fileMatch: ['*.yaml'],
+        });
+      });
     });
   });
 
-- 
GitLab