diff --git a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Text/MongoTextIndexEntity.cs b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Text/MongoTextIndexEntity.cs index bb42b0ff45..ff828c1418 100644 --- a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Text/MongoTextIndexEntity.cs +++ b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Text/MongoTextIndexEntity.cs @@ -57,6 +57,7 @@ public sealed class MongoTextIndexEntity [BsonIgnoreIfNull] [BsonElement("go")] [BsonJson] + [BsonRepresentation(BsonType.Document)] public Geometry GeoObject { get; set; } } } diff --git a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs index 7dd7fbc6a3..cdef916180 100644 --- a/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs +++ b/backend/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs @@ -20,7 +20,22 @@ public sealed class BsonJsonSerializer : ClassSerializerBase, IRepresenta public BsonType ActualRepresentation { - get => Representation == BsonType.Undefined ? BsonJsonConvention.Representation : Representation; + get + { + var result = Representation; + + if (result == BsonType.Undefined) + { + result = BsonJsonConvention.Representation; + } + + if (result == BsonType.Undefined) + { + result = BsonType.Document; + } + + return result; + } } public JsonSerializerOptions Options @@ -35,7 +50,7 @@ public BsonJsonSerializer() public BsonJsonSerializer(BsonType representation) { - if (representation is not BsonType.Undefined and not BsonType.String and not BsonType.Binary) + if (representation is not BsonType.Undefined and not BsonType.String and not BsonType.Binary and not BsonType.Document) { throw new ArgumentException("Unsupported representation.", nameof(representation)); } diff --git a/backend/src/Squidex/appsettings.json b/backend/src/Squidex/appsettings.json index fc6a95de8b..1b37dd060b 100644 --- a/backend/src/Squidex/appsettings.json +++ b/backend/src/Squidex/appsettings.json @@ -487,7 +487,7 @@ // Defines how key-value-store values are represented in MongoDB (e.g. app, rule, schema). // - // SUPPORTED: Undefined (Objects), String, Binary (from slow to fast). + // SUPPORTED: Document, String, Binary (from slow to fast). "valueRepresentation": "Undefined", "atlas": { diff --git a/backend/tests/Squidex.Infrastructure.Tests/MongoDb/BsonJsonSerializerTests.cs b/backend/tests/Squidex.Infrastructure.Tests/MongoDb/BsonJsonSerializerTests.cs index 08efc8f912..1cff8eedb4 100644 --- a/backend/tests/Squidex.Infrastructure.Tests/MongoDb/BsonJsonSerializerTests.cs +++ b/backend/tests/Squidex.Infrastructure.Tests/MongoDb/BsonJsonSerializerTests.cs @@ -21,6 +21,13 @@ public class TestWrapper public T Value { get; set; } } + public class TestWrapperDocument + { + [BsonJson] + [BsonRepresentation(BsonType.Document)] + public T Value { get; set; } + } + public class TestWrapperString { [BsonJson] @@ -124,6 +131,19 @@ public void Should_serialize_and_deserialize() deserialized.Should().BeEquivalentTo(source); } + [Fact] + public void Should_serialize_and_deserialize_as_document() + { + var source = new TestWrapperDocument + { + Value = TestObject.CreateWithValues() + }; + + var deserialized = source.SerializeAndDeserializeBson(); + + deserialized.Should().BeEquivalentTo(source); + } + [Fact] public void Should_serialize_and_deserialize_as_string() {