-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add converter for product search facets (#375)
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...tools.Sdk/Tests/commercetools.Api.Serialization.Tests/ProductSearchDeserializationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...tools.Sdk/Tests/commercetools.Api.Serialization.Tests/Resources/ProductSearch/facets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
80 changes: 80 additions & 0 deletions
80
...ols.Sdk/commercetools.Sdk.Api/Serialization/JsonConverters/ProductSearchFacetConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters