mirror of https://github.com/Squidex/squidex.git
committed by
GitHub
50 changed files with 694 additions and 201 deletions
@ -0,0 +1,65 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.IO; |
|||
using MongoDB.Bson.Serialization; |
|||
using MongoDB.Bson.Serialization.Serializers; |
|||
|
|||
namespace Squidex.Infrastructure.MongoDb |
|||
{ |
|||
public sealed class BsonEscapedDictionarySerializer<TValue, TInstance> : ClassSerializerBase<TInstance> where TInstance : Dictionary<string, TValue?>, new() |
|||
{ |
|||
public static void Register() |
|||
{ |
|||
try |
|||
{ |
|||
BsonSerializer.RegisterSerializer(new BsonEscapedDictionarySerializer<TValue, TInstance>()); |
|||
} |
|||
catch (BsonSerializationException) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
|
|||
protected override TInstance DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) |
|||
{ |
|||
var reader = context.Reader; |
|||
|
|||
var result = new TInstance(); |
|||
|
|||
reader.ReadStartDocument(); |
|||
|
|||
while (reader.ReadBsonType() != BsonType.EndOfDocument) |
|||
{ |
|||
var key = reader.ReadName().BsonToJsonName(); |
|||
|
|||
result.Add(key, BsonSerializer.Deserialize<TValue>(reader)); |
|||
} |
|||
|
|||
reader.ReadEndDocument(); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TInstance value) |
|||
{ |
|||
var writer = context.Writer; |
|||
|
|||
writer.WriteStartDocument(); |
|||
|
|||
foreach (var property in value) |
|||
{ |
|||
writer.WriteName(property.Key.JsonToBsonName()); |
|||
|
|||
BsonSerializer.Serialize(writer, property.Value); |
|||
} |
|||
|
|||
writer.WriteEndDocument(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,102 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure.Queries |
|||
{ |
|||
public class PropertyPathTests |
|||
{ |
|||
[Fact] |
|||
public void Should_create() |
|||
{ |
|||
var path = new PropertyPath(new[] { "path", "to", "property" }); |
|||
|
|||
Assert.Equal(new[] { "path", "to", "property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_convert_to_string() |
|||
{ |
|||
var path = new PropertyPath(new[] { "path", "to", "property" }); |
|||
|
|||
Assert.Equal("path.to.property", path.ToString()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_exception_for_empty_path() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => new PropertyPath(Array.Empty<string>())); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_exception_for_empty_path_from_string() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => { PropertyPath p = string.Empty; }); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_exception_for_empty_path_from_null_string() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => { PropertyPath p = (string)null!; }); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_throw_exception_for_empty_path_from_list() |
|||
{ |
|||
Assert.Throws<ArgumentException>(() => { PropertyPath p = new List<string>(); }); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_dot_string() |
|||
{ |
|||
PropertyPath path = "path.to.property"; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_broken_dot_string() |
|||
{ |
|||
PropertyPath path = ".path...to...property."; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_slash_string() |
|||
{ |
|||
PropertyPath path = "path/to/property"; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_broken_slash_string() |
|||
{ |
|||
PropertyPath path = "/path///to///property/"; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_dot_string_and_escape() |
|||
{ |
|||
PropertyPath path = "path.to.complex\\.property"; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "complex.property" }, path.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_from_slash_string_and_escape() |
|||
{ |
|||
PropertyPath path = "path.to.complex\\/property"; |
|||
|
|||
Assert.Equal(new[] { "path", "to", "complex/property" }, path.ToArray()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
{ |
|||
Data: { |
|||
Json: { |
|||
iv: null |
|||
} |
|||
}, |
|||
Status: Published, |
|||
Id: Guid_1, |
|||
Version: 1, |
|||
_links: { |
|||
delete: { |
|||
Method: DELETE |
|||
}, |
|||
draft/create: { |
|||
Method: POST |
|||
}, |
|||
previous: { |
|||
Method: GET |
|||
}, |
|||
self: { |
|||
Method: GET |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue