Skip to content
Merged

fix #38

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
4 changes: 4 additions & 0 deletions Frends.JSON.ConvertXMLStringToJToken/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.1.0] - 2026-06-11
### Fixed
- Fix a case when we want to return null value for non-string types.

## [2.0.0] - 2026-05-27
### Breaking
- The `XSD` parameter has moved from the **Input** tab to the **Options** tab and is now only used (and shown) when `TypeCorrection` is `Schema`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@

// Default behaviour is unchanged: the xsi:type wrapper and string value are preserved.
Assert.IsNotNull(price);
Assert.AreEqual(JTokenType.String, price["#text"].Type);

Check warning on line 100 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
Assert.AreEqual("12.5", price["#text"].ToString());

Check warning on line 101 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
}

[TestMethod]
Expand All @@ -116,15 +116,15 @@
};

var result = JSON.ConvertXMLStringToJToken(input, new Options { TypeCorrection = TypeCorrectionMode.Attributes });
var root = (JObject)((JObject)result.Jtoken)["root"];

Check warning on line 119 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Converting null literal or possible null value to non-nullable type.

Assert.IsTrue(result.Success);
Assert.AreEqual(JTokenType.Float, root["price"].Type);

Check warning on line 122 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.

Check warning on line 122 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
Assert.AreEqual(12.5, root["price"].Value<double>());

Check warning on line 123 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Possible null reference argument for parameter 'value' in 'double Extensions.Value<double>(IEnumerable<JToken> value)'.
Assert.AreEqual(JTokenType.Integer, root["quantity"].Type);

Check warning on line 124 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
Assert.AreEqual(3, root["quantity"].Value<int>());

Check warning on line 125 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Possible null reference argument for parameter 'value' in 'int Extensions.Value<int>(IEnumerable<JToken> value)'.
Assert.AreEqual(JTokenType.Integer, root["huge"].Type);

Check warning on line 126 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
Assert.AreEqual(JTokenType.Boolean, root["inStock"].Type);

Check warning on line 127 in Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs

View workflow job for this annotation

GitHub Actions / build / Build on ubuntu-22.04

Dereference of a possibly null reference.
Assert.IsTrue(root["inStock"].Value<bool>());
Assert.AreEqual(JTokenType.String, root["name"].Type);
}
Expand Down Expand Up @@ -848,4 +848,90 @@
Assert.AreEqual(JTokenType.String, price["#text"].Type);
Assert.AreEqual("12.5", price["#text"].ToString());
}
}

[TestMethod]
public void SchemaMode_FoobarEmptyNillableNonString_ShouldBecomeNull()
{
var input = new Input()
{
XML = @"<root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<foobar1 xsi:nil='true'/>
<foobar2 xsi:nil='true'/>
<foobar3 xsi:nil='true'/>
</root>"
};

var options = new Options()
{
TypeCorrection = TypeCorrectionMode.Schema,
XSD = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='root'>
<xs:complexType>
<xs:sequence>
<xs:element name='foobar1' type='xs:int' nillable='true' />
<xs:element name='foobar2' type='xs:boolean' nillable='true' />
<xs:element name='foobar3' type='xs:double' nillable='true' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>"
};

var result = JSON.ConvertXMLStringToJToken(input, options);
var foobar1 = ((JObject)result.Jtoken)["root"]?["foobar1"];
var foobar2 = ((JObject)result.Jtoken)["root"]?["foobar2"];
var foobar3 = ((JObject)result.Jtoken)["root"]?["foobar3"];
Assert.IsNotNull(foobar1);
Assert.IsNotNull(foobar2);
Assert.IsNotNull(foobar3);
Assert.AreEqual(JTokenType.Null, foobar1.Type);
Assert.AreEqual(JTokenType.Null, foobar2.Type);
Assert.AreEqual(JTokenType.Null, foobar3.Type);
}

[TestMethod]
public void SchemaMode_FoobarNonString_ShouldBecomeValidType()
{
var input = new Input()
{
XML = @"<root>
<foobar1>3</foobar1>
<foobar2>2.3</foobar2>
<foobar3>true</foobar3>
</root>"
};

var options = new Options()
{
TypeCorrection = TypeCorrectionMode.Schema,
XSD = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='root'>
<xs:complexType>
<xs:sequence>
<xs:element name='foobar1' type='xs:int' nillable='true' />
<xs:element name='foobar2' type='xs:double' nillable='true' />
<xs:element name='foobar3' type='xs:boolean' nillable='true' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>"
};

var result = JSON.ConvertXMLStringToJToken(input, options);
var foobar1 = ((JObject)result.Jtoken)["root"]?["foobar1"];
var foobar2 = ((JObject)result.Jtoken)["root"]?["foobar2"];
var foobar3 = ((JObject)result.Jtoken)["root"]?["foobar3"];

Assert.IsNotNull(foobar1);
Assert.IsNotNull(foobar2);
Assert.IsNotNull(foobar3);

Assert.AreEqual(JTokenType.Integer, foobar1.Type);
Assert.AreEqual(JTokenType.Float, foobar2.Type);
Assert.AreEqual(JTokenType.Boolean, foobar3.Type);

Assert.AreEqual(3, foobar1.Value<int>());
Assert.AreEqual(2.3, foobar2.Value<double>());
Assert.AreEqual(true, foobar3.Value<bool>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,13 @@ private static bool ApplyXsiNil(
if (nilFlag == true)
{
nilProperty.Remove();
ReplaceOrClearWrapper(obj, textProperty, JValue.CreateNull());
var typePropertyForNil = FindXsiTypeProperty(obj, xmlnsScope);
typePropertyForNil?.Remove();

if (obj.Parent != null)
obj.Replace(JValue.CreateNull());
else
ReplaceOrClearWrapper(obj, textProperty, JValue.CreateNull());
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand All @@ -14,15 +14,15 @@
<PackageProjectUrl>https://frends.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/FrendsPlatform/Frends.JSON2/tree/main/Frends.JSON.ConvertXMLStringToJToken</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<None Include="FrendsTaskMetadata.json" Pack="true" PackagePath="/">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="migration.json" PackagePath="/" Pack="true" />
<Content Include="../CHANGELOG.md" PackagePath="/" Pack="true" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand Down
Loading