fix: Fixes JSON TypeConverter so that it can deserialize full name types (#1466)

This commit is contained in:
mdodkins 2023-08-22 05:25:09 +01:00 • committed by GitHub
parent 52ca1fe686
commit 976680699c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 24 deletions

View file

@ -1,35 +1,39 @@
using System; using System;
using System.Reflection; using System.Reflection;
namespace Server.Tests namespace Server.Tests;
internal class ServerFixture : IDisposable
{ {
internal class ServerFixture : IDisposable // Global setup
static ServerFixture()
{ {
// Global setup Core.Assembly = Assembly.GetExecutingAssembly(); // Server.Tests.dll
static ServerFixture()
{
Core.Assembly = Assembly.GetExecutingAssembly();
Core.LoopContext = new EventLoopContext();
Core.Expansion = Expansion.EJ;
// Load Configurations // Load Configurations
ServerConfiguration.Load(true); ServerConfiguration.Load(true);
// Configure / Initialize // Load an empty assembly list into the resolver
TestMapDefinitions.ConfigureTestMapDefinitions(); ServerConfiguration.AssemblyDirectories.Add(Core.BaseDirectory);
AssemblyHandler.LoadAssemblies(new[]{ "ModernUO.dll" });
// Configure the world Core.LoopContext = new EventLoopContext();
World.Configure(); Core.Expansion = Expansion.EJ;
Timer.Init(0); // Configure / Initialize
TestMapDefinitions.ConfigureTestMapDefinitions();
// Load the world // Configure the world
World.Load(); World.Configure();
}
public void Dispose() Timer.Init(0);
{
Timer.Init(0); // Load the world
} World.Load();
}
public void Dispose()
{
Timer.Init(0);
} }
} }

View file

@ -0,0 +1,37 @@
using System;
using System.Buffers;
using System.Text.Json;
using Server.Json;
using Xunit;
namespace Server.Tests;
public class TypeConverterTests : IClassFixture<ServerFixture>
{
[Fact]
public void TestReadAfterWrite()
{
// Arrange
Type itemType = typeof(Item);
JsonSerializerOptions options = new JsonSerializerOptions();
TypeConverter converter = new TypeConverter();
var bufferWriter = new ArrayBufferWriter<byte>();
using var jsonWriter = new Utf8JsonWriter(bufferWriter);
converter.Write(jsonWriter, itemType, options);
// Act
jsonWriter.Flush();
var jsonData = bufferWriter.WrittenSpan;
Utf8JsonReader jsonReader = new Utf8JsonReader(jsonData);
jsonReader.Read();
// Assert
Type readType = converter.Read(ref jsonReader, typeof(Type), options);
Assert.Equal(typeof(Item), readType);
}
}

View file

@ -32,7 +32,7 @@ public class TypeConverter : JsonConverter<Type>
} }
var typeName = reader.GetString(); var typeName = reader.GetString();
var type = AssemblyHandler.FindTypeByName(typeName); var type = AssemblyHandler.FindTypeByName(typeName) ?? AssemblyHandler.FindTypeByName(typeName, true);
if (type == null) if (type == null)
{ {
logger.Warning("Attempted to deserialize type {Type} which does not exist.", typeName); logger.Warning("Attempted to deserialize type {Type} which does not exist.", typeName);

View file

@ -18,7 +18,7 @@ namespace Server.Tests
// Load UOContent.dll into the type resolver // Load UOContent.dll into the type resolver
ServerConfiguration.AssemblyDirectories.Add(Core.BaseDirectory); ServerConfiguration.AssemblyDirectories.Add(Core.BaseDirectory);
var assembles = new [] { "UOContent.dll" }; var assembles = new [] { "ModernUO.dll", "UOContent.dll" };
AssemblyHandler.LoadAssemblies(assembles); AssemblyHandler.LoadAssemblies(assembles);
// Load Skills // Load Skills