Skip to content

Commit

Permalink
add converter for product search facets (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Nov 11, 2024
1 parent 4672750 commit 7e31746
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using commercetools.Sdk.Api.Models.ProductSearches;
using FluentAssertions;
using Xunit;

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

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

[Fact]
public void FacetDeserialization()
{
var serializerService = this._serializationFixture.SerializerService;
var serialized = File.ReadAllText("Resources/ProductSearch/facets.json");
var deserialized = serializerService.Deserialize<IProductPagedSearchResponse>(serialized);
Assert.NotNull(deserialized);
var facets = deserialized.Facets;
var bucket = Assert.IsAssignableFrom<IProductSearchFacetResultBucket>(facets[0]);
Assert.Equal("supplierName", bucket.Name);
Assert.Equal("Example Inc.", bucket.Buckets[0].Key);
Assert.Equal(77, bucket.Buckets[0].Count);
var count = Assert.IsAssignableFrom<IProductSearchFacetResultCount>(facets[1]);
Assert.Equal("supplierCounts", count.Name);
Assert.Equal(10, count.Value);

var serialize = serializerService.Serialize(deserialized);

Assert.Equal("{\"total\":0,\"offset\":0,\"limit\":0,\"facets\":[{\"name\":\"supplierName\",\"buckets\":[{\"key\":\"Example Inc.\",\"count\":77}]},{\"name\":\"supplierCounts\",\"value\":10}]}", serialize);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"total":0,
"offset": 0,
"limit": 0,
"facets": [
{
"name": "supplierName",
"buckets": [
{
"key": "Example Inc.",
"count": 77
}
]
},
{
"name": "supplierCounts",
"value": 10
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using commercetools.Sdk.Api.Models.Products;
using commercetools.Base.Serialization;
using commercetools.Base.Serialization.MapperTypeRetrievers;
using commercetools.Sdk.Api.Models.ProductSearches;
using commercetools.Sdk.Api.Serialization.MapperTypeRetrievers;
using Attribute = commercetools.Sdk.Api.Models.Products.Attribute;
using Type = System.Type;

namespace commercetools.Sdk.Api.Serialization.JsonConverters
{
public class ProductSearchFacetConverter : JsonConverter<IProductSearchFacetResult>
{
private readonly ISerializerService _serializerService;

public ProductSearchFacetConverter(ISerializerService serializerService)
{
this._serializerService = serializerService;
}

public override bool CanConvert(Type typeToConvert)
{
return typeof(IProductSearchFacetResult).IsAssignableFrom(typeToConvert);
}

public override IProductSearchFacetResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonDocument = JsonDocument.ParseValue(ref reader);
IAttribute attribute;
var rootElement = jsonDocument.RootElement;
if (rootElement.ValueKind == JsonValueKind.Object)
{
var nameProp = rootElement.GetProperty("name");
if (rootElement.TryGetProperty("buckets", out var bucketsElement))
{
return new ProductSearchFacetResultBucket
{
Name = nameProp.GetString(),
Buckets = bucketsElement.ToObject<List<IProductSearchFacetResultBucketEntry>>(_serializerService)
};
}
if (rootElement.TryGetProperty("value", out var valueElement))
{
return new ProductSearchFacetResultCount
{
Name = nameProp.GetString(),
Value = valueElement.GetInt64()
};
}

return new ProductSearchFacetResult
{
Name = nameProp.GetString()
};
}

return null;
}

public override void Write(Utf8JsonWriter writer, IProductSearchFacetResult value,
JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString("name", value.Name);
if (value is ProductSearchFacetResultBucket bucket)
{
writer.WritePropertyName("buckets");
JsonSerializer.Serialize(writer, bucket.Buckets, options);
}
else if (value is ProductSearchFacetResultCount count) {
writer.WritePropertyName("value");
JsonSerializer.Serialize(writer, count.Value, options);
}

writer.WriteEndObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public SerializerService(
_serializerOptions.Converters.Add(new MessageDeliveryConverter(this));
_serializerOptions.Converters.Add(new CustomDateTimeConverter());
_serializerOptions.Converters.Add(new CustomDateConverter());
_serializerOptions.Converters.Add(new ProductSearchFacetConverter(this));
_serializerOptions.Converters.Add(new FieldContainerConverter(customFieldsMapperTypeRetriever, this));
_serializerOptions.Converters.Add(new AttributeConverter(attributesMapperTypeRetriever, attributeTypeRetriever, this));
_serializerOptions.Converters.Add(new DeserializeAsConverterFactory(
Expand Down

0 comments on commit 7e31746

Please sign in to comment.