diff --git a/sources/core/Stride.Core.Yaml.Tests/Serialization/SerializationTests.cs b/sources/core/Stride.Core.Yaml.Tests/Serialization/SerializationTests.cs index 4b7dd38d9e..83d3741494 100644 --- a/sources/core/Stride.Core.Yaml.Tests/Serialization/SerializationTests.cs +++ b/sources/core/Stride.Core.Yaml.Tests/Serialization/SerializationTests.cs @@ -816,6 +816,25 @@ public void DeserializeWithRepeatedSubObjects() Assert.Equal(22, family.Mother.Age); } + [Fact] + public void ThrowWithoutEmptyCtor() + { + try + { + SerializeThenDeserialize(new ClassWithNonEmptyCtor(default)); + Assert.Fail("An exception should have been thrown by this method before hitting this line, the class provided does not have an empty constructor"); + } + catch (Exception ex) + { + Assert.IsType(ex.InnerException); + } + } + + class ClassWithNonEmptyCtor + { + public ClassWithNonEmptyCtor(bool parameter) { } + } + [Fact] public void DeserializeEmptyDocument() diff --git a/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs b/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs index 51fe1112c0..fba1b3a04b 100644 --- a/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs +++ b/sources/core/Stride.Core.Yaml/Serialization/DefaultObjectFactory.cs @@ -99,13 +99,14 @@ public static Type GetDefaultImplementation(Type type) return type; } + /// public object Create(Type type) { type = GetDefaultImplementation(type); - // We can't instantiate primitive or arrays + // We can't instantiate primitives or arrays if (PrimitiveDescriptor.IsPrimitive(type) || type.IsArray) - return null; + throw new InstanceCreationException($"Failed to create instance of type '{type}', wrong factory."); if (type.GetConstructor(EmptyTypes) != null || type.IsValueType) { @@ -119,11 +120,12 @@ public object Create(Type type) } } - return null; + throw new InstanceCreationException($"Failed to create instance of type '{type}', type does not have a parameterless constructor."); } public class InstanceCreationException : Exception { + public InstanceCreationException(string message) : base(message) { } public InstanceCreationException(string message, Exception innerException) : base(message, innerException) { } } } diff --git a/sources/core/Stride.Core.Yaml/Serialization/IObjectFactory.cs b/sources/core/Stride.Core.Yaml/Serialization/IObjectFactory.cs index f22da8bb9f..30f5ec0ac3 100644 --- a/sources/core/Stride.Core.Yaml/Serialization/IObjectFactory.cs +++ b/sources/core/Stride.Core.Yaml/Serialization/IObjectFactory.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2015 SharpYaml - Alexandre Mutel +// Copyright (c) 2015 SharpYaml - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -56,8 +56,8 @@ namespace Stride.Core.Yaml.Serialization public interface IObjectFactory { /// - /// Creates an instance of the specified type. Returns null if instance cannot be created. + /// Creates an instance of the specified type. Throws with an appropriate exception if the type cannot be created. /// object Create(Type type); } -} \ No newline at end of file +}