Skip to content

Commit

Permalink
Overwrite representation type.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jul 25, 2022
1 parent 7e033cb commit c7508e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public sealed class MongoTextIndexEntity<T>
[BsonIgnoreIfNull]
[BsonElement("go")]
[BsonJson]
[BsonRepresentation(BsonType.Document)]
public Geometry GeoObject { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ public sealed class BsonJsonSerializer<T> : ClassSerializerBase<T?>, 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
Expand All @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/Squidex/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class TestWrapper<T>
public T Value { get; set; }
}

public class TestWrapperDocument<T>
{
[BsonJson]
[BsonRepresentation(BsonType.Document)]
public T Value { get; set; }
}

public class TestWrapperString<T>
{
[BsonJson]
Expand Down Expand Up @@ -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<TestObject>
{
Value = TestObject.CreateWithValues()
};

var deserialized = source.SerializeAndDeserializeBson();

deserialized.Should().BeEquivalentTo(source);
}

[Fact]
public void Should_serialize_and_deserialize_as_string()
{
Expand Down

0 comments on commit c7508e8

Please sign in to comment.