Skip to content

Fixed NullReferenceException in OpenXmlElement RawOuterXml field #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Version 2.12.1

### Fixed
- Fixed `OpenXmlElement.RawOuterXml` to properly set null values without throwing (#818)
- Allow rewriting of all malformed URIs regardless of target value (#835)

## Version 2.12.0 - 2020-12-09
Expand Down
10 changes: 1 addition & 9 deletions src/DocumentFormat.OpenXml/OpenXmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,7 @@ internal string RawOuterXml
{
get => _rawOuterXml;

set
{
if (string.IsNullOrEmpty(value))
{
_rawOuterXml = string.Empty;
}

_rawOuterXml = value;
}
set => _rawOuterXml = value ?? string.Empty;
}

private Framework.Metadata.ElementState _state;
Expand Down
36 changes: 36 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlElementTest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,41 @@ public void TestGetXPathIndex()
target = unknown2.GetXPathIndex();
Assert.Equal(2, target);
}

/// <summary>
/// Tests if empty string is set when
/// null value is given to setter
/// </summary>
[Fact]
public void TestSetRawOuterXmlField_NullValueIsSet_FieldShouldNotBeNull()
{
var openXmlElement = new Document();
openXmlElement.RawOuterXml = null;
Assert.NotNull(openXmlElement.RawOuterXml);
}

/// <summary>
/// Tests if proper string value is set when
/// setter is invoked
/// </summary>
[Fact]
public void TestSetRawOuterXmlField_ValueIsSet_FieldShouldContainSetValue()
{
var openXmlElement = new Document();
var testValue = "Some proper value";
openXmlElement.RawOuterXml = testValue;
Assert.Equal(testValue, openXmlElement.RawOuterXml);
}

/// <summary>
/// Tests if field is empty when empty string is set
/// </summary>
[Fact]
public void TestSetRawOuterXmlField_SetValueIsEmpty_FieldShouldBeEmpty()
{
var openXmlElement = new Document();
openXmlElement.RawOuterXml = string.Empty;
Assert.Equal(string.Empty, openXmlElement.RawOuterXml);
}
}
}