Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes sealed from a number of classes and respects empty strings in enummember #985

Merged
merged 5 commits into from
Sep 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void HandleEnum(ITypeSymbol type)
if (enumMember != null)
{
var argument = enumMember.NamedArguments.FirstOrDefault(x => x.Key == "Value");
if (!string.IsNullOrWhiteSpace(argument.Value.Value as string))
if ((argument.Value.Value as string) != null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not is string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually could have gotten rid of the string check altogether since I’m just checking if it’s null. The value property there is always going to be either null or a string.

{
memberValue = (string)argument.Value.Value!;
}
Expand Down
10 changes: 8 additions & 2 deletions YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public void EnumSerializationUsesEnumMemberAttribute()
public void EnumSerializationUsesEnumMemberAttributeWithEmptyValue()
{
var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actual = serializer.Serialize(EnumMemberedEnum.EmptyValue);
Assert.Equal("EmptyValue", actual.TrimNewLines());
var actual = serializer.Serialize(new EnumMemberedEnumHarness { Test = EnumMemberedEnum.EmptyValue });
Assert.Equal("Test: ''", actual.TrimNewLines());
}

[Fact]
Expand All @@ -243,6 +243,12 @@ public void EnumSerializationUsesEnumMemberAttributeWithNullValue()
Assert.Equal("NullValue", actual.TrimNewLines());
}

[YamlSerializable]
public class EnumMemberedEnumHarness
{
public EnumMemberedEnum Test { get; set; }
}

[YamlSerializable]
public enum EnumMemberedEnum
{
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,8 @@ public void EnumSerializationUsesEnumMemberAttribute()
public void EnumSerializationUsesEnumMemberAttributeWithEmptyValue()
{
var serializer = new SerializerBuilder().Build();
var actual = serializer.Serialize(EnumMemberedEnum.EmptyValue);
Assert.Equal("EmptyValue", actual.TrimNewLines());
var actual = serializer.Serialize(new { Test = EnumMemberedEnum.EmptyValue });
Assert.Equal("Test: ''", actual.TrimNewLines());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace YamlDotNet.Serialization.ObjectFactories
/// <summary>
/// Creates objects using Activator.CreateInstance.
/// </summary>
public sealed class DefaultObjectFactory : ObjectFactoryBase
public class DefaultObjectFactory : ObjectFactoryBase
{
private readonly Dictionary<Type, ConcurrentDictionary<Type, MethodInfo[]>> stateMethods = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Wraps another <see cref="ITypeInspector"/> and applies caching.
/// </summary>
public sealed class CachedTypeInspector : TypeInspectorSkeleton
public class CachedTypeInspector : TypeInspectorSkeleton
{
private readonly ITypeInspector innerTypeDescriptor;
private readonly ConcurrentDictionary<Type, List<IPropertyDescriptor>> cache = new ConcurrentDictionary<Type, List<IPropertyDescriptor>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Aggregates the results from multiple <see cref="ITypeInspector" /> into a single one.
/// </summary>
public sealed class CompositeTypeInspector : TypeInspectorSkeleton
public class CompositeTypeInspector : TypeInspectorSkeleton
{
private readonly IEnumerable<ITypeInspector> typeInspectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// Wraps another <see cref="ITypeInspector"/> and applies a
/// naming convention to the names of the properties.
/// </summary>
public sealed class NamingConventionTypeInspector : TypeInspectorSkeleton
public class NamingConventionTypeInspector : TypeInspectorSkeleton
{
private readonly ITypeInspector innerTypeDescriptor;
private readonly INamingConvention namingConvention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Returns the properties of a type that are both readable and writable.
/// </summary>
public sealed class ReadableAndWritablePropertiesTypeInspector : TypeInspectorSkeleton
public class ReadableAndWritablePropertiesTypeInspector : TypeInspectorSkeleton
{
private readonly ITypeInspector innerTypeDescriptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Returns the properties and fields of a type that are readable.
/// </summary>
public sealed class ReadableFieldsTypeInspector : ReflectionTypeInspector
public class ReadableFieldsTypeInspector : ReflectionTypeInspector
{
private readonly ITypeResolver typeResolver;

Expand All @@ -46,7 +46,7 @@ public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object
.Select(p => (IPropertyDescriptor)new ReflectionFieldDescriptor(p, typeResolver));
}

private sealed class ReflectionFieldDescriptor : IPropertyDescriptor
protected class ReflectionFieldDescriptor : IPropertyDescriptor
{
private readonly FieldInfo fieldInfo;
private readonly ITypeResolver typeResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Returns the properties of a type that are readable.
/// </summary>
public sealed class ReadablePropertiesTypeInspector : ReflectionTypeInspector
public class ReadablePropertiesTypeInspector : ReflectionTypeInspector
{
private readonly ITypeResolver typeResolver;
private readonly bool includeNonPublicProperties;
Expand Down Expand Up @@ -60,7 +60,7 @@ public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object
.Select(p => (IPropertyDescriptor)new ReflectionPropertyDescriptor(p, typeResolver));
}

private sealed class ReflectionPropertyDescriptor : IPropertyDescriptor
protected class ReflectionPropertyDescriptor : IPropertyDescriptor
{
private readonly PropertyInfo propertyInfo;
private readonly ITypeResolver typeResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string GetEnumValue(object enumValue)
if (enumMembers.Length > 0)
{
var attribute = enumMembers[0].GetCustomAttribute<EnumMemberAttribute>();
if (!string.IsNullOrWhiteSpace(attribute?.Value))
if (attribute?.Value != null)
{
result = attribute.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.TypeInspectors
/// <summary>
/// Returns the properties of a type that are writable.
/// </summary>
public sealed class WritablePropertiesTypeInspector : ReflectionTypeInspector
public class WritablePropertiesTypeInspector : ReflectionTypeInspector
{
private readonly ITypeResolver typeResolver;
private readonly bool includeNonPublicProperties;
Expand Down Expand Up @@ -61,7 +61,7 @@ public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object
.ToArray();
}

private sealed class ReflectionPropertyDescriptor : IPropertyDescriptor
protected class ReflectionPropertyDescriptor : IPropertyDescriptor
{
private readonly PropertyInfo propertyInfo;
private readonly ITypeResolver typeResolver;
Expand Down