Skip to content

Commit 7abb65a

Browse files
aidilumarovtwsouthwick
authored andcommitted
Fixed OpenXmlElement.RawOuterXml to properly set null values without throwing (dotnet#818)
1 parent 9917159 commit 7abb65a

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
### Fixed
1010
- Fixed bug where properties on `OpenXmlCompositeElement` instances could not be set to null to remove element (#850)
11+
- Fixed `OpenXmlElement.RawOuterXml` to properly set null values without throwing (#818)
1112
- Allow rewriting of all malformed URIs regardless of target value (#835)
1213

1314
## Version 2.12.0 - 2020-12-09

src/DocumentFormat.OpenXml/OpenXmlElement.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,7 @@ internal string RawOuterXml
154154
{
155155
get => _rawOuterXml;
156156

157-
set
158-
{
159-
if (string.IsNullOrEmpty(value))
160-
{
161-
_rawOuterXml = string.Empty;
162-
}
163-
164-
_rawOuterXml = value;
165-
}
157+
set => _rawOuterXml = value ?? string.Empty;
166158
}
167159

168160
private Framework.Metadata.ElementState _state;

test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlElementTest2.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,42 @@ public void TestGetXPathIndex()
145145
Assert.Equal(2, target);
146146
}
147147

148+
/// <summary>
149+
/// Tests if empty string is set when
150+
/// null value is given to setter
151+
/// </summary>
152+
[Fact]
153+
public void TestSetRawOuterXmlField_NullValueIsSet_FieldShouldNotBeNull()
154+
{
155+
var openXmlElement = new Document();
156+
openXmlElement.RawOuterXml = null;
157+
Assert.NotNull(openXmlElement.RawOuterXml);
158+
}
159+
160+
/// <summary>
161+
/// Tests if proper string value is set when
162+
/// setter is invoked
163+
/// </summary>
164+
[Fact]
165+
public void TestSetRawOuterXmlField_ValueIsSet_FieldShouldContainSetValue()
166+
{
167+
var openXmlElement = new Document();
168+
var testValue = "Some proper value";
169+
openXmlElement.RawOuterXml = testValue;
170+
Assert.Equal(testValue, openXmlElement.RawOuterXml);
171+
}
172+
173+
/// <summary>
174+
/// Tests if field is empty when empty string is set
175+
/// </summary>
176+
[Fact]
177+
public void TestSetRawOuterXmlField_SetValueIsEmpty_FieldShouldBeEmpty()
178+
{
179+
var openXmlElement = new Document();
180+
openXmlElement.RawOuterXml = string.Empty;
181+
Assert.Equal(string.Empty, openXmlElement.RawOuterXml);
182+
}
183+
148184
[Fact]
149185
public void CanSetNullValue()
150186
{

0 commit comments

Comments
 (0)