diff --git a/src/Core/Services/MetadataProviders/Converters/DatabaseObjectConverter.cs b/src/Core/Services/MetadataProviders/Converters/DatabaseObjectConverter.cs index 710095620d..c6e694a394 100644 --- a/src/Core/Services/MetadataProviders/Converters/DatabaseObjectConverter.cs +++ b/src/Core/Services/MetadataProviders/Converters/DatabaseObjectConverter.cs @@ -45,8 +45,8 @@ public override void Write(Utf8JsonWriter writer, DatabaseObject value, JsonSeri // Add TypeName property in DatabaseObject object that we are serializing based on its type. (DatabaseTable, DatabaseView) // We add this property to differentiate between them in the dictionary. This extra property gets used in deserialization above. // for example if object is DatabaseTable then we need to add - // "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", - writer.WriteString(TYPE_NAME, value.GetType().AssemblyQualifiedName); + // "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config", + writer.WriteString(TYPE_NAME, GetTypeNameFromType(value.GetType())); // Add other properties of DatabaseObject foreach (PropertyInfo prop in value.GetType().GetProperties()) @@ -75,5 +75,23 @@ private static Type GetTypeFromName(string typeName) return type; } + + /// + /// Changes the Type.AssemblyQualifiedName to desired format + /// we cannot use the FullName as during deserialization it throws exception as object not found. + /// + private static string GetTypeNameFromType(Type type) + { + // AssemblyQualifiedName for the type looks like : + // "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + // we donot need version or culture or publickeytoken for serialization or deserialization, we need the first two parts. + + string assemblyQualifiedName = type.AssemblyQualifiedName!; + string[] parts = assemblyQualifiedName.Split(','); + + // typename would be : "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config" + string typeName = $"{parts[0]},{parts[1]}"; + return typeName; + } } } diff --git a/src/Service.Tests/Unittests/SerializationDeserializationTests.cs b/src/Service.Tests/Unittests/SerializationDeserializationTests.cs index b3dfd8bcae..e0b125dff7 100644 --- a/src/Service.Tests/Unittests/SerializationDeserializationTests.cs +++ b/src/Service.Tests/Unittests/SerializationDeserializationTests.cs @@ -336,7 +336,7 @@ private void InitializeObjects() /// /// During serialization we add TypeName to the DatabaseObject based on its type. TypeName is the AssemblyQualifiedName for the type. - /// Example : "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", + /// Example : "TypeName": "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config", /// 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 /// previously defined. /// 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 // Split TypeName into different parts // following splits different sections of : - // "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" + // "Azure.DataApiBuilder.Config.DatabasePrimitives.DatabaseTable, Azure.DataApiBuilder.Config" string[] typeNameSplitParts = typeName.Split(','); string namespaceString = typeNameSplitParts[0].Trim(); @@ -376,6 +376,8 @@ private void TestTypeNameChanges(DatabaseObject databaseobject, string objectNam string projectNameString = typeNameSplitParts[1].Trim(); Assert.AreEqual(projectNameString, "Azure.DataApiBuilder.Config"); + + Assert.AreEqual(typeNameSplitParts.Length, 2); } else {