Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

handle more serialization/deserialization cases #1804

Merged
merged 2 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/ApiService/ApiService/onefuzzlib/orm/EntityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.OneFuzz.Service.OneFuzzLib.Orm;

public abstract record EntityBase
{
public ETag? ETag { get; set; }
[JsonIgnore] public ETag? ETag { get; set; }
public DateTimeOffset? TimeStamp { get; set; }

//public ApiService.OneFuzzLib.Orm.IOrm<EntityBase>? Orm { get; set; }
Expand Down Expand Up @@ -174,7 +174,7 @@ public TableEntity ToTableEntity<T>(T typedEntity) where T : EntityBase
else
{
var serialized = JsonSerializer.Serialize(value, _options);
tableEntity.Add(prop.columnName, serialized);
tableEntity.Add(prop.columnName, serialized.Trim('"'));
}

}
Expand Down Expand Up @@ -208,7 +208,7 @@ public T ToRecord<T>(TableEntity entity) where T : EntityBase

var fieldName = ef.columnName;
var obj = entity[fieldName];
if (obj == null)
if (obj == null)
{
return null;
}
Expand Down Expand Up @@ -256,8 +256,23 @@ public T ToRecord<T>(TableEntity entity) where T : EntityBase
}
else
{
var value = entity.GetString(fieldName);
return JsonSerializer.Deserialize(value, ef.type, options: _options); ;
if (objType == typeof(string))
{
var value = entity.GetString(fieldName);
if (value.StartsWith('[') || value.StartsWith('{') || value == "null")
{
return JsonSerializer.Deserialize(value, ef.type, options: _options);
}
else
{
return JsonSerializer.Deserialize($"\"{value}\"", ef.type, options: _options);
}
}
else
{
var value = entity.GetString(fieldName);
return JsonSerializer.Deserialize(value, ef.type, options: _options);
}
}
}
).ToArray();
Expand Down
65 changes: 63 additions & 2 deletions src/ApiService/Tests/OrmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,68 @@ record Entity1(
TestFlagEnum TheFlag,
[property: JsonPropertyName("a__special__name")] string Renamed,
TestObject TheObject,
TestObject? TestNull
TestObject? TestNull,

Uri TestUri,
Uri? TestUriNull

) : EntityBase();


[Fact]
public void TestBothDirections()
{
var uriString = "https://localhost:9090";
var converter = new EntityConverter();
var entity1 = new Entity1(
Guid.NewGuid(),
"test",
DateTimeOffset.UtcNow,
123,
12.44,
TestEnum.TheTwo, TestFlagEnum.FlagOne | TestFlagEnum.FlagTwo,
"renamed",
new TestObject
{
TheName = "testobject",
TheEnum = TestEnum.TheTwo,
TheFlag = TestFlagEnum.FlagOne | TestFlagEnum.FlagTwo
},
null,
new Uri(uriString),
null
);


var tableEntity = converter.ToTableEntity(entity1);
var fromTableEntity = converter.ToRecord<Entity1>(tableEntity);
var eq = fromTableEntity == entity1;

Assert.Equal(fromTableEntity.TimeStamp, entity1.TimeStamp);
Assert.Equal(fromTableEntity.Id, entity1.Id);
Assert.Equal(fromTableEntity.Renamed, entity1.Renamed);
Assert.Equal(fromTableEntity.TestNull, entity1.TestNull);
Assert.Equal(fromTableEntity.TestUri, entity1.TestUri);
Assert.Equal(fromTableEntity.TestUriNull, entity1.TestUriNull);
Assert.Equal(fromTableEntity.TheDate, entity1.TheDate);
Assert.Equal(fromTableEntity.TheEnum, entity1.TheEnum);

Assert.Equal(fromTableEntity.TheFlag, entity1.TheFlag);
Assert.Equal(fromTableEntity.TheFloat, entity1.TheFloat);
Assert.Equal(fromTableEntity.TheName, entity1.TheName);
Assert.Equal(fromTableEntity.TheNumber, entity1.TheNumber);
Assert.Equal(fromTableEntity.TimeStamp, entity1.TimeStamp);

Assert.Equal(fromTableEntity.TheObject.TheEnum, entity1.TheObject.TheEnum);
Assert.Equal(fromTableEntity.TheObject.TheFlag, entity1.TheObject.TheFlag);
Assert.Equal(fromTableEntity.TheObject.TheName, entity1.TheObject.TheName);
}


[Fact]
public void TestConvertToTableEntity()
{
var uriString = "https://localhost:9090";
var converter = new EntityConverter();
var entity1 = new Entity1(
Guid.NewGuid(),
Expand All @@ -62,7 +117,10 @@ public void TestConvertToTableEntity()
TheEnum = TestEnum.TheTwo,
TheFlag = TestFlagEnum.FlagOne | TestFlagEnum.FlagTwo
},
null);
null,
new Uri(uriString),
null
);
var tableEntity = converter.ToTableEntity(entity1);

Assert.NotNull(tableEntity);
Expand All @@ -75,6 +133,9 @@ public void TestConvertToTableEntity()
Assert.Equal("flag_one,flag_two", tableEntity.GetString("the_flag"));
Assert.Equal("renamed", tableEntity.GetString("a__special__name"));

Assert.Equal(uriString, tableEntity.GetString("test_uri"));


var json = JsonNode.Parse(tableEntity.GetString("the_object"))?.AsObject() ?? throw new InvalidOperationException("Could not parse objec");

json.TryGetPropertyValue("the_name", out var theName);
Expand Down