Skip to content

Commit

Permalink
fix mapping of pOBox field (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Jan 28, 2022
1 parent 97e6202 commit bbe6a93
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using commercetools.Api.Models.Common;
using Xunit;

namespace commercetools.Api.Serialization.Tests
{
public class AddressTests: IClassFixture<SerializationFixture>
{
private readonly SerializationFixture _serializationFixture;

public AddressTests(SerializationFixture serializationFixture)
{
this._serializationFixture = serializationFixture;
}

[Fact]
public void poBoxAddress()
{
var serializerService = this._serializationFixture.SerializerService;
var serialized = "{\"pOBox\": \"12345\"}";
var deserialized = serializerService.Deserialize<Address>(serialized);
Assert.NotNull(deserialized);
Assert.Equal("12345", deserialized.POBox);

Assert.Equal("{\"pOBox\":\"12345\"}", serializerService.Serialize(deserialized));
}
[Fact]
public void poBoxAddressDraft()
{
var serializerService = this._serializationFixture.SerializerService;
var serialized = "{\"pOBox\": \"12345\"}";
var deserialized = serializerService.Deserialize<AddressDraft>(serialized);
Assert.NotNull(deserialized);
Assert.Equal("12345", deserialized.POBox);

Assert.Equal("{\"pOBox\":\"12345\"}", serializerService.Serialize(deserialized));
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json;
using System;
using System.Collections.Generic;
using System.Text.Json;
using commercetools.Api.Models.Products;
using commercetools.Api.Models.Types;
using commercetools.Base.Registration;
Expand All @@ -15,6 +17,23 @@ public class SerializerService : ISerializerService
{
private readonly JsonSerializerOptions _serializerOptions;

public static readonly JsonNamingPolicy MappedCamelCase = new MappingCamelCaseNamingPolicy();

public class MappingCamelCaseNamingPolicy : JsonNamingPolicy
{
private readonly JsonNamingPolicy _policy = CamelCase;

private readonly Dictionary<string, string> _fieldMapping = new Dictionary<string, string>
{
{ "POBox", "pOBox" }
};

public override string ConvertName(string name)
{
return _fieldMapping.ContainsKey(name) ? _fieldMapping[name] : _policy.ConvertName(name);
}
}

public SerializerService(
ITypeRetriever typeRetriever,
IMapperTypeRetriever<IFieldContainer> customFieldsMapperTypeRetriever,
Expand All @@ -24,8 +43,9 @@ public SerializerService(
_serializerOptions = new JsonSerializerOptions
{
IgnoreNullValues = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = MappedCamelCase
};

_serializerOptions.Converters.Add(new MessageDeliveryConverter(this));
_serializerOptions.Converters.Add(new CustomDateTimeConverter());
_serializerOptions.Converters.Add(new FieldContainerConverter(customFieldsMapperTypeRetriever, this));
Expand Down

0 comments on commit bbe6a93

Please sign in to comment.