Skip to content

Fix WriteArray in encoders to also write null.#3266

Merged
marcschier merged 5 commits into
masterfrom
jsonencfix
Oct 16, 2025
Merged

Fix WriteArray in encoders to also write null.#3266
marcschier merged 5 commits into
masterfrom
jsonencfix

Conversation

@marcschier

@marcschier marcschier commented Oct 15, 2025

Copy link
Copy Markdown
Collaborator

Proposed changes

WriteArray needs to handle null object in variant case (and enumeration) which it does not today, while all other types correctly write a null. Instead it crashes with null ref when used with Complex type system.

Related Issues

Types of changes

What types of changes does your code introduce?
Put an x in the boxes that apply. You can also fill these out after creating the PR.

  • Bugfix (non-breaking change which fixes an issue)
  • Enhancement (non-breaking change which adds functionality)
  • Test enhancement (non-breaking change to increase test coverage)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected, requires version increase of Nuget packages)
  • Documentation Update (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc.
  • I have signed the CLA.
  • I ran tests locally with my changes, all passed.
  • I fixed all failing tests in the CI pipelines.
  • I fixed all introduced issues with CodeQL and LGTM.
  • I have added tests that prove my fix is effective or that my feature works and increased code coverage.
  • I have added necessary documentation (if appropriate).
  • Any dependent changes have been merged and published in downstream modules.

Further comments

The change was tested against the simulation server:

{E82844B8-C64F-42E6-8084-B0A98BC3C19C}

@codecov

codecov Bot commented Oct 15, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 38.18182% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.85%. Comparing base (f1721b6) to head (4b86171).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
Stack/Opc.Ua.Core/Types/Encoders/BinaryEncoder.cs 34.78% 10 Missing and 5 partials ⚠️
Stack/Opc.Ua.Core/Types/Encoders/XmlEncoder.cs 23.52% 8 Missing and 5 partials ⚠️
Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs 60.00% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3266      +/-   ##
==========================================
+ Coverage   57.64%   57.85%   +0.21%     
==========================================
  Files         365      365              
  Lines       79419    79423       +4     
  Branches    13865    13870       +5     
==========================================
+ Hits        45780    45950     +170     
+ Misses      29382    29302      -80     
+ Partials     4257     4171      -86     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a null reference exception in encoder implementations by updating the WriteArray methods to properly handle null arrays for Variant and Enumeration types. Previously, the encoders would crash when encountering null arrays in these specific cases.

  • Added null checks for Variant and Enumeration array types in all three encoder implementations
  • Refactored conditional logic to use pattern matching with is null or Type[] syntax
  • Added comprehensive test coverage for null array encoding/decoding scenarios

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
Tests/Opc.Ua.Core.Tests/Types/Encoders/EncoderTests.cs Adds new test method EncodeNullArray to verify null array handling across all encoder types
Stack/Opc.Ua.Core/Types/Encoders/XmlEncoder.cs Updates WriteArray to handle null arrays for Variant and Enumeration types using pattern matching
Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs Updates WriteArray to handle null arrays for Variant and Enumeration types using pattern matching
Stack/Opc.Ua.Core/Types/Encoders/BinaryEncoder.cs Updates WriteArray to handle null arrays for Variant and Enumeration types using pattern matching

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread Stack/Opc.Ua.Core/Types/Encoders/XmlEncoder.cs Outdated
Comment thread Stack/Opc.Ua.Core/Types/Encoders/BinaryEncoder.cs Outdated
Comment thread Stack/Opc.Ua.Core/Types/Encoders/BinaryEncoder.cs
@marcschier
marcschier dismissed opcfoundation-org’s stale review October 16, 2025 06:24

Will handle the issue in separate tracked issue.

@marcschier
marcschier merged commit ca43b64 into master Oct 16, 2025
165 of 168 checks passed
@marcschier
marcschier deleted the jsonencfix branch October 16, 2025 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonEncoder throws exception when encountering array with null value

5 participants