Skip to content
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
2 changes: 1 addition & 1 deletion src/BMEcatSharp/Types/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public bool EmailComponentsSpecified
get
{
// HACK: called just before the payload gets serialized
EmailComponent.EmailsToEmailComponentsIfValueIsCreated(emails, ref emailComponents);
EmailComponent.EmailsToEmailComponents(emails, ref emailComponents);

if (emailComponents?.Count > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BMEcatSharp/Types/BMEBuyerAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public bool EmailComponentsSpecified
get
{
// HACK: called just before the payload gets serialized
EmailComponent.EmailsToEmailComponentsIfValueIsCreated(emails, ref emailComponents);
EmailComponent.EmailsToEmailComponents(emails, ref emailComponents);

if (emailComponents?.Count > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BMEcatSharp/Types/BMESupplierAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public bool EmailComponentsSpecified
get
{
// HACK: called just before the payload gets serialized
EmailComponent.EmailsToEmailComponentsIfValueIsCreated(emails, ref emailComponents);
EmailComponent.EmailsToEmailComponents(emails, ref emailComponents);

if (emailComponents?.Count > 0)
{
Expand Down
12 changes: 0 additions & 12 deletions src/BMEcatSharp/Types/EmailComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class EmailComponent
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static void EmailsToEmailComponentsIfValueIsCreated(Lazy<List<Email>?> emails, ref List<EmailComponent>? emailComponents)
{
if (!emails.IsValueCreated)
{
emailComponents = [];
return;
}

EmailsToEmailComponents(emails, ref emailComponents);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static void EmailsToEmailComponents(Lazy<List<Email>?> emails, ref List<EmailComponent>? emailComponents)
{
Expand Down
5 changes: 4 additions & 1 deletion src/OpenTransSharp.Tests/OpenTransSharp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
Expand Down Expand Up @@ -49,6 +49,9 @@
<None Update="CustomData.xsd">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Quotations\ticket_19_deserialize_email.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
83 changes: 83 additions & 0 deletions src/OpenTransSharp.Tests/Orders/OrderSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BMEcatSharp;
using System.Linq;

namespace OpenTransSharp.Tests.Orders;
Expand Down Expand Up @@ -197,4 +198,86 @@ public void Ticket17_Directly_set_Address_Emails_property_is_serialized_even_if_
serializedContent.Should().Contain("email@example.com");
serializedContent.Should().Contain("email.2@example.com");
}

[Test]
public void Ticket19_deserialized_and_immediately_serialized_address_keeps_email_values()
{
var options = new OpenTransXmlSerializerOptions();
var serializerFactory = new OpenTransXmlSerializerFactory(options);

var serializer = serializerFactory.Create<Address>();

var addressString = """
<ADDRESS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opentrans.org/XMLSchema/2.1"
xmlns:bmecat="http://www.bmecat.org/bmecat/2005">
<CONTACT_DETAILS>
<bmecat:CONTACT_ID>general-id</bmecat:CONTACT_ID>
<bmecat:CONTACT_NAME>general-name</bmecat:CONTACT_NAME>
</CONTACT_DETAILS>
<bmecat:EMAIL>general-mail</bmecat:EMAIL>
</ADDRESS>
""";
var deserialized = serializer.Deserialize<Address>(addressString);

var serializedContent = serializer.Serialize(deserialized);

serializedContent.Should().Contain("general-mail");
}

[Test]
public void Ticket19_deserialized_and_immediately_serialized_address_keeps_email_values_while_accessing_emails_inbetween()
{
var options = new OpenTransXmlSerializerOptions();
var serializerFactory = new OpenTransXmlSerializerFactory(options);

var serializer = serializerFactory.Create<Address>();

var addressString = """
<ADDRESS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opentrans.org/XMLSchema/2.1"
xmlns:bmecat="http://www.bmecat.org/bmecat/2005">
<CONTACT_DETAILS>
<bmecat:CONTACT_ID>general-id</bmecat:CONTACT_ID>
<bmecat:CONTACT_NAME>general-name</bmecat:CONTACT_NAME>
</CONTACT_DETAILS>
<bmecat:EMAIL>general-mail</bmecat:EMAIL>
</ADDRESS>
""";
var deserialized = serializer.Deserialize<Address>(addressString);

var emails = deserialized.Emails;

var serializedContent = serializer.Serialize(deserialized);

serializedContent.Should().Contain("general-mail");
}

[Test]
public void Ticket19_deserialized_and_immediately_serialized_address_keeps_email_values_while_mutating_emails_inbetween()
{
var options = new OpenTransXmlSerializerOptions();
var serializerFactory = new OpenTransXmlSerializerFactory(options);

var serializer = serializerFactory.Create<Address>();

var addressString = """
<ADDRESS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opentrans.org/XMLSchema/2.1"
xmlns:bmecat="http://www.bmecat.org/bmecat/2005">
<CONTACT_DETAILS>
<bmecat:CONTACT_ID>general-id</bmecat:CONTACT_ID>
<bmecat:CONTACT_NAME>general-name</bmecat:CONTACT_NAME>
</CONTACT_DETAILS>
<bmecat:EMAIL>general-mail</bmecat:EMAIL>
</ADDRESS>
""";
var deserialized = serializer.Deserialize<Address>(addressString);

var emails = deserialized.Emails;
emails.Clear();
emails.Add(new Email("another-email@gmail.com"));

var serializedContent = serializer.Serialize(deserialized);

serializedContent.Should().NotContain("general-mail");
serializedContent.Should().Contain("another-email@gmail.com");
}
}
10 changes: 10 additions & 0 deletions src/OpenTransSharp.Tests/Quotations/QuotationSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ public void Can_validate_Quotation_with_UDX()
Debug.WriteLine(serialized);
order.IsValid(target).Should().Be(true);
}

[Test]
public void Can_deserialize_Quotation_Emails_Ticket_19()
{
var quotation = File.ReadAllText("""Quotations\ticket_19_deserialize_email.xml""");
var deserialized = target.Deserialize<Quotation>(quotation);

var serialized = target.Serialize(deserialized);
serialized.Should().Contain("general-mail");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<QUOTATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:bmecat="http://www.bmecat.org/bmecat/2005" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsig="http://www.w3.org/2000/09/xmldsig#" version="2.1" xmlns="http://www.opentrans.org/XMLSchema/2.1">
<QUOTATION_HEADER>
<QUOTATION_INFO>
<QUOTATION_ID>149136</QUOTATION_ID>
<QUOTATION_DATE>2024-01-11T00:00:00</QUOTATION_DATE>
<bmecat:VALID_START_DATE>2024-01-11T00:00:00</bmecat:VALID_START_DATE>
<bmecat:VALID_END_DATE>2024-01-31T00:00:00</bmecat:VALID_END_DATE>
<PARTIES>
<PARTY>
<bmecat:PARTY_ID type="supplier_specific">123456</bmecat:PARTY_ID>
<PARTY_ROLE>buyer</PARTY_ROLE>
<ADDRESS>
<CONTACT_DETAILS>
<bmecat:CONTACT_ID>general-id</bmecat:CONTACT_ID>
<bmecat:CONTACT_NAME>general-name</bmecat:CONTACT_NAME>
</CONTACT_DETAILS>
<bmecat:EMAIL>general-mail</bmecat:EMAIL>
</ADDRESS>
</PARTY>
<PARTY>
<bmecat:PARTY_ID type="supplier_specific">400302</bmecat:PARTY_ID>
<PARTY_ROLE>delivery</PARTY_ROLE>
</PARTY>
<PARTY>
<bmecat:PARTY_ID type="supplier_specific">400302</bmecat:PARTY_ID>
<PARTY_ROLE>invoice_recipient</PARTY_ROLE>
</PARTY>
<PARTY>
<bmecat:PARTY_ID type="buyer_specific">400302</bmecat:PARTY_ID>
<PARTY_ROLE>supplier</PARTY_ROLE>
</PARTY>
</PARTIES>
<ORDER_PARTIES_REFERENCE>
<bmecat:BUYER_IDREF type="buyer_specific">400302</bmecat:BUYER_IDREF>
<bmecat:SUPPLIER_IDREF type="supplier_specific">400302</bmecat:SUPPLIER_IDREF>
<INVOICE_RECIPIENT_IDREF>400302</INVOICE_RECIPIENT_IDREF>
</ORDER_PARTIES_REFERENCE>
<bmecat:CURRENCY>EUR</bmecat:CURRENCY>
<bmecat:TRANSPORT>
<bmecat:INCOTERM>FCA</bmecat:INCOTERM>
<bmecat:TRANSPORT_REMARK lang="deu">UPS</bmecat:TRANSPORT_REMARK>
</bmecat:TRANSPORT>
<REMARKS type="dispatchnotification">8 weeks</REMARKS>
</QUOTATION_INFO>
</QUOTATION_HEADER>
<QUOTATION_ITEM_LIST>
<QUOTATION_ITEM>
<LINE_ITEM_ID>1.0</LINE_ITEM_ID>
<PRODUCT_ID>
<bmecat:SUPPLIER_PID>N01.544.660.13.004</bmecat:SUPPLIER_PID>
<bmecat:DESCRIPTION_SHORT lang="deu">Mitnehmer</bmecat:DESCRIPTION_SHORT>
</PRODUCT_ID>
<QUANTITY>24</QUANTITY>
<bmecat:ORDER_UNIT>C62</bmecat:ORDER_UNIT>
<PRICE_LINE_AMOUNT>2500.8</PRICE_LINE_AMOUNT>
<DELIVERY_DATE>
<DELIVERY_START_DATE>2024-05-03T16:45:36.3398861+02:00</DELIVERY_START_DATE>
<DELIVERY_END_DATE>2024-05-03T16:45:36.3398882+02:00</DELIVERY_END_DATE>
</DELIVERY_DATE>
</QUOTATION_ITEM>
</QUOTATION_ITEM_LIST>
<QUOTATION_SUMMARY>
<TOTAL_ITEM_NUM>1</TOTAL_ITEM_NUM>
</QUOTATION_SUMMARY>
</QUOTATION>
3 changes: 2 additions & 1 deletion src/OpenTransSharp/Types/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace OpenTransSharp;
/// <br/>
/// This element is used to transfer address information of a business partner.
/// </summary>
[OpenTransXmlRoot("ADDRESS")]
public class Address
{
/// <summary>
Expand Down Expand Up @@ -270,7 +271,7 @@ public bool EmailComponentsSpecified
get
{
// HACK: called just before the payload gets serialized
EmailComponent.EmailsToEmailComponentsIfValueIsCreated(emails, ref emailComponents);
EmailComponent.EmailsToEmailComponents(emails, ref emailComponents);

if (emailComponents?.Count > 0)
{
Expand Down
19 changes: 19 additions & 0 deletions src/OpenTransSharp/Xml/OpenTransXmlRootAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace OpenTransSharp.Xml;

public class OpenTransXmlRootAttribute : XmlRootAttribute
{
public OpenTransXmlRootAttribute()
{
Init();
}

public OpenTransXmlRootAttribute(string elementName) : base(elementName)
{
Init();
}

private void Init()
{
Namespace = "http://www.opentrans.org/XMLSchema/2.1";
}
}