Skip to content

OCPBUGS-77912: Fix TypeError in OLS code import to console#16119

Open
rhamilto wants to merge 1 commit intoopenshift:mainfrom
rhamilto:OCPBUGS-77912
Open

OCPBUGS-77912: Fix TypeError in OLS code import to console#16119
rhamilto wants to merge 1 commit intoopenshift:mainfrom
rhamilto:OCPBUGS-77912

Conversation

@rhamilto
Copy link
Member

@rhamilto rhamilto commented Mar 9, 2026

Summary

Fixes the "Import to console" action from OpenShift Lightspeed that was failing with a TypeError on OpenShift 4.20+.

Bug: OCPBUGS-77912

Changes

  • Fixed race condition in getEditor() function in edit-yaml.tsx
  • Added null check before using in operator to prevent TypeError when monacoRef.current is undefined
  • Added comprehensive regression tests to prevent future breakage

Root Cause

When users clicked "Import to console" from OpenShift Lightspeed, the page redirected with ?ols=true query parameter. A race condition occurred where:

  1. The OLS code import useEffect ran when editorMounted became true
  2. But monacoRef.current was still undefined at that moment
  3. The expression 'editor' in monacoRef?.current threw TypeError: Cannot use 'in' operator to search for 'editor' in undefined

The Fix

// Before (caused TypeError)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
  'editor' in monacoRef?.current ? monacoRef.current.editor : undefined;

// After (fixed)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
  monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

Test Plan

  • Existing TypeScript compilation passes
  • ESLint/prettier passes
  • New regression tests pass (3/3)
  • Manual verification: Click "Import to console" from OLS AI response and verify YAML populates in editor

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved the safety of editor reference access to prevent potential crashes when the code editor instance is unavailable, uninitialized, or missing required properties.
  • Tests

    • Added comprehensive unit tests to validate that editor reference access safely and reliably handles various edge cases including undefined references, missing properties, and valid instances.

The "Import to console" action from OpenShift Lightspeed was failing
with "TypeError: Cannot use 'in' operator to search for 'editor' in
undefined" when redirecting to the YAML import page.

The issue occurred in getEditor() where the 'in' operator was used on
monacoRef.current before checking if it was defined. This created a
race condition where the OLS code import useEffect would run when
editorMounted became true, but monacoRef.current was still undefined.

The fix adds a check to ensure monacoRef.current exists before using
the 'in' operator, preventing the TypeError and allowing the OLS code
to be properly injected into the editor.

Added regression tests to verify the fix handles all cases correctly.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@openshift-ci-robot openshift-ci-robot added jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Mar 9, 2026
@openshift-ci-robot
Copy link
Contributor

@rhamilto: This pull request references Jira Issue OCPBUGS-77912, which is invalid:

  • expected the bug to target the "4.22.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Summary

Fixes the "Import to console" action from OpenShift Lightspeed that was failing with a TypeError on OpenShift 4.20+.

Bug: OCPBUGS-77912

Changes

  • Fixed race condition in getEditor() function in edit-yaml.tsx
  • Added null check before using in operator to prevent TypeError when monacoRef.current is undefined
  • Added comprehensive regression tests to prevent future breakage

Root Cause

When users clicked "Import to console" from OpenShift Lightspeed, the page redirected with ?ols=true query parameter. A race condition occurred where:

  1. The OLS code import useEffect ran when editorMounted became true
  2. But monacoRef.current was still undefined at that moment
  3. The expression 'editor' in monacoRef?.current threw TypeError: Cannot use 'in' operator to search for 'editor' in undefined

The Fix

// Before (caused TypeError)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
 'editor' in monacoRef?.current ? monacoRef.current.editor : undefined;

// After (fixed)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
 monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

Test Plan

  • Existing TypeScript compilation passes
  • ESLint/prettier passes
  • New regression tests pass (3/3)
  • Manual verification: Click "Import to console" from OLS AI response and verify YAML populates in editor

🤖 Generated with Claude Code

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot requested review from jhadvig and sg00dwin March 9, 2026 12:54
@openshift-ci openshift-ci bot added the component/core Related to console core functionality label Mar 9, 2026
@rhamilto
Copy link
Member Author

rhamilto commented Mar 9, 2026

/assign @logonoff

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 9, 2026
@openshift-ci-robot openshift-ci-robot added jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. and removed jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Mar 9, 2026
@openshift-ci-robot
Copy link
Contributor

@rhamilto: This pull request references Jira Issue OCPBUGS-77912, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.22.0) matches configured target version for branch (4.22.0)
  • bug is in the state ASSIGNED, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @yapei

Details

In response to this:

Summary

Fixes the "Import to console" action from OpenShift Lightspeed that was failing with a TypeError on OpenShift 4.20+.

Bug: OCPBUGS-77912

Changes

  • Fixed race condition in getEditor() function in edit-yaml.tsx
  • Added null check before using in operator to prevent TypeError when monacoRef.current is undefined
  • Added comprehensive regression tests to prevent future breakage

Root Cause

When users clicked "Import to console" from OpenShift Lightspeed, the page redirected with ?ols=true query parameter. A race condition occurred where:

  1. The OLS code import useEffect ran when editorMounted became true
  2. But monacoRef.current was still undefined at that moment
  3. The expression 'editor' in monacoRef?.current threw TypeError: Cannot use 'in' operator to search for 'editor' in undefined

The Fix

// Before (caused TypeError)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
 'editor' in monacoRef?.current ? monacoRef.current.editor : undefined;

// After (fixed)
const getEditor = (): editor.IStandaloneCodeEditor | undefined =>
 monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;

Test Plan

  • Existing TypeScript compilation passes
  • ESLint/prettier passes
  • New regression tests pass (3/3)
  • Manual verification: Click "Import to console" from OLS AI response and verify YAML populates in editor

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

  • Improved the safety of editor reference access to prevent potential crashes when the code editor instance is unavailable, uninitialized, or missing required properties.

  • Tests

  • Added comprehensive unit tests to validate that editor reference access safely and reliably handles various edge cases including undefined references, missing properties, and valid instances.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot requested a review from yapei March 9, 2026 12:57
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 9, 2026

📝 Walkthrough

Walkthrough

This change adds comprehensive unit test coverage for the getEditor helper function in the EditYAML component and tightens its implementation to safely handle falsy monacoRef.current values. The tests verify three scenarios: when the ref is undefined, when the ref lacks an editor property, and when the editor is present. The implementation fix corrects the safe access pattern from 'editor' in monacoRef?.current to monacoRef?.current && 'editor' in monacoRef.current, ensuring proper short-circuit evaluation. The modification prevents potential issues where the in operator could evaluate even when current is falsy.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title references OCPBUGS-77912 and mentions fixing a TypeError in OLS code import, which directly aligns with the changeset addressing a race condition in edit-yaml.tsx and adding regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
frontend/public/components/__tests__/edit-yaml.spec.tsx (2)

5-48: Good regression test coverage for the bug fix.

The three scenarios are well-chosen and the comments documenting OCPBUGS-77912 will help future maintainers understand why these tests exist.

One consideration: the getEditor logic is duplicated in each test case (lines 13-14, 29-30, 43-44). Since getEditor is a private function inside the component, this copy-paste approach is understandable, but it does mean these tests won't catch if someone accidentally changes the component's implementation without updating tests.

♻️ Optional: Extract duplicated getEditor logic
+// Mirrors the getEditor function from edit-yaml.tsx
+// If the implementation changes there, update this helper accordingly
+const createGetEditor = (monacoRef: React.RefObject<CodeEditorRef | undefined>) => () =>
+  monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
+
 describe('EditYAML: getEditor function', () => {
   it('should handle undefined monacoRef.current without throwing TypeError', () => {
     // This test verifies the fix for OCPBUGS-77912
     // The bug occurred when monacoRef.current was undefined and the 'in' operator was used
     const { result } = renderHook(() => useRef<CodeEditorRef>());
     const monacoRef = result.current;
 
-    // This simulates the getEditor function from edit-yaml.tsx
-    const getEditor = () =>
-      monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor : undefined;
+    const getEditor = createGetEditor(monacoRef);
 
     // Before the fix, this would throw: "TypeError: Cannot use 'in' operator to search for 'editor' in undefined"
     // After the fix, it should return undefined gracefully
     expect(() => getEditor()).not.toThrow();
     expect(getEditor()).toBeUndefined();
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/public/components/__tests__/edit-yaml.spec.tsx` around lines 5 - 48,
Tests duplicate the inline getEditor logic across three cases which makes
maintenance brittle; extract a single helper function (e.g., getEditorFromRef)
inside the describe block that accepts the monaco ref (monacoRef) and returns
monacoRef?.current && 'editor' in monacoRef.current ? monacoRef.current.editor :
undefined, then replace the three inline getEditor lambdas with calls to this
helper so all tests use the same logic (update the uses in the three it blocks
referencing monacoRef and the mockEditor).

39-41: Minor: Type coercion with as any reduces type safety.

The as any cast bypasses TypeScript's type checking. Since this is a test file and you're intentionally creating a partial mock, this is acceptable, but a slightly safer approach would be to use a more precise cast.

♻️ Optional: Use Partial type for mock
     const mockEditor = { getValue: jest.fn(), setValue: jest.fn() };
     // Set monacoRef.current to an object with an 'editor' property
-    monacoRef.current = { editor: mockEditor } as any;
+    monacoRef.current = { editor: mockEditor } as unknown as CodeEditorRef;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/public/components/__tests__/edit-yaml.spec.tsx` around lines 39 -
41, The test currently uses a blanket cast "as any" when assigning
monacoRef.current which weakens type safety; replace the any cast with a more
precise partial type cast (e.g., use Partial<typeof monacoRef.current> or
Partial<MonacoRefType> depending on your monacoRef declaration) when assigning {
editor: mockEditor } to monacoRef.current so the mockEditor and the editor
property are type-checked while still allowing a partial mock; update the
assignment of monacoRef.current and/or import or reference the appropriate
monacoRef type to perform the Partial<> cast instead of using as any.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@frontend/public/components/__tests__/edit-yaml.spec.tsx`:
- Around line 5-48: Tests duplicate the inline getEditor logic across three
cases which makes maintenance brittle; extract a single helper function (e.g.,
getEditorFromRef) inside the describe block that accepts the monaco ref
(monacoRef) and returns monacoRef?.current && 'editor' in monacoRef.current ?
monacoRef.current.editor : undefined, then replace the three inline getEditor
lambdas with calls to this helper so all tests use the same logic (update the
uses in the three it blocks referencing monacoRef and the mockEditor).
- Around line 39-41: The test currently uses a blanket cast "as any" when
assigning monacoRef.current which weakens type safety; replace the any cast with
a more precise partial type cast (e.g., use Partial<typeof monacoRef.current> or
Partial<MonacoRefType> depending on your monacoRef declaration) when assigning {
editor: mockEditor } to monacoRef.current so the mockEditor and the editor
property are type-checked while still allowing a partial mock; update the
assignment of monacoRef.current and/or import or reference the appropriate
monacoRef type to perform the Partial<> cast instead of using as any.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 78b9db18-51a7-4bd7-9ebc-b6271fe6ce45

📥 Commits

Reviewing files that changed from the base of the PR and between 874c37e and 67f5308.

📒 Files selected for processing (2)
  • frontend/public/components/__tests__/edit-yaml.spec.tsx
  • frontend/public/components/edit-yaml.tsx

@rhamilto
Copy link
Member Author

rhamilto commented Mar 9, 2026

/jira refresh
/cherrypick release-4.21 release-4.20

@openshift-cherrypick-robot

@rhamilto: once the present PR merges, I will cherry-pick it on top of release-4.21 in a new PR and assign it to you.

Details

In response to this:

/jira refresh
/cherrypick release-4.21 release-4.20

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@openshift-ci-robot
Copy link
Contributor

@rhamilto: This pull request references Jira Issue OCPBUGS-77912, which is valid.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.22.0) matches configured target version for branch (4.22.0)
  • bug is in the state POST, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @yapei

Details

In response to this:

/jira refresh
/cherrypick release-4.21 release-4.20

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Copy link
Member

@logonoff logonoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Mar 9, 2026
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 9, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: logonoff, rhamilto

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rhamilto
Copy link
Member Author

rhamilto commented Mar 9, 2026

/retest

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Mar 10, 2026

@rhamilto: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. component/core Related to console core functionality jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants