Skip to content

Commit 9504f1d

Browse files
authored
Change TypeName in DatabaseObject Serialization to exclude unwanted parts (#2154)
## What is this change? Change TypeName in DatabaseObjectConverter during DatabaseObject Serialization to exclude the unwanted parts. Currently TypeName field added during DatabaseObject serialization is set to Type.AssemblyQualifiedName, which looks like : "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 1.0.0.0 was the default value set, hence when the version changes, deserialization will fail if the initial serialization had version as 1.0.0.0. Updating this TypeName to only include the necessary parts : "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config" Tested again by using Type.FullName which is "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable", but serialization fails saying DatabaseTable not found. ## Why make this change? Due to recent changes to update the version number based of release, deserialization on graphql workload failed, it is looking for version 1.0.0.0 which doesnot exists. Hence removing the dependency on version number. ## How was this tested? - Unit Tests
1 parent 6ce6fe5 commit 9504f1d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Core/Services/MetadataProviders/Converters/DatabaseObjectConverter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public override void Write(Utf8JsonWriter writer, DatabaseObject value, JsonSeri
4545
// Add TypeName property in DatabaseObject object that we are serializing based on its type. (DatabaseTable, DatabaseView)
4646
// We add this property to differentiate between them in the dictionary. This extra property gets used in deserialization above.
4747
// for example if object is DatabaseTable then we need to add
48-
// "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
49-
writer.WriteString(TYPE_NAME, value.GetType().AssemblyQualifiedName);
48+
// "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config",
49+
writer.WriteString(TYPE_NAME, GetTypeNameFromType(value.GetType()));
5050

5151
// Add other properties of DatabaseObject
5252
foreach (PropertyInfo prop in value.GetType().GetProperties())
@@ -75,5 +75,23 @@ private static Type GetTypeFromName(string typeName)
7575

7676
return type;
7777
}
78+
79+
/// <summary>
80+
/// Changes the Type.AssemblyQualifiedName to desired format
81+
/// we cannot use the FullName as during deserialization it throws exception as object not found.
82+
/// </summary>
83+
private static string GetTypeNameFromType(Type type)
84+
{
85+
// AssemblyQualifiedName for the type looks like :
86+
// "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
87+
// we donot need version or culture or publickeytoken for serialization or deserialization, we need the first two parts.
88+
89+
string assemblyQualifiedName = type.AssemblyQualifiedName!;
90+
string[] parts = assemblyQualifiedName.Split(',');
91+
92+
// typename would be : "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config"
93+
string typeName = $"{parts[0]},{parts[1]}";
94+
return typeName;
95+
}
7896
}
7997
}

src/Service.Tests/Unittests/SerializationDeserializationTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private void InitializeObjects()
336336

337337
/// <summary>
338338
/// During serialization we add TypeName to the DatabaseObject based on its type. TypeName is the AssemblyQualifiedName for the type.
339-
/// Example : "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
339+
/// Example : "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config",
340340
/// If there is a code refactor which changes the path of this object, the deserialization with old value will fail, as it wont be able to find the object in the location
341341
/// previously defined.
342342
/// Note : Currently this is being used by GraphQL workload, failure of this test case needs to be
@@ -367,7 +367,7 @@ private void TestTypeNameChanges(DatabaseObject databaseobject, string objectNam
367367

368368
// Split TypeName into different parts
369369
// following splits different sections of :
370-
// "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
370+
// "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config"
371371
string[] typeNameSplitParts = typeName.Split(',');
372372

373373
string namespaceString = typeNameSplitParts[0].Trim();
@@ -376,6 +376,8 @@ private void TestTypeNameChanges(DatabaseObject databaseobject, string objectNam
376376

377377
string projectNameString = typeNameSplitParts[1].Trim();
378378
Assert.AreEqual(projectNameString, "Azure.DataApiBuilder.Config");
379+
380+
Assert.AreEqual(typeNameSplitParts.Length, 2);
379381
}
380382
else
381383
{

0 commit comments

Comments
 (0)