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

Support naming conventions for enum members #773

Open
pardahlman opened this issue Jan 30, 2023 · 4 comments
Open

Support naming conventions for enum members #773

pardahlman opened this issue Jan 30, 2023 · 4 comments

Comments

@pardahlman
Copy link

Is your feature request related to a problem? Please describe.

I have a YAML file where both properties and their values use snake case.

project:
    type: database_migration

The corresponding C# DTO uses enum

enum ProjectType
{
  DatabaseMigration
}

I have configred YamlDotNet to use UnderscoredNamingConvention, but the deserialization fails:

YamlDotNet.Core.YamlException: (Line: 4, Col: 11, Idx: 36) - (Line: 4, Col: 20, Idx: 45): Exception during deserialization
 ---> System.ArgumentException: Requested value 'database_migration' was not found.
   at System.Enum.TryParseByName(RuntimeType enumType, String originalValueString, ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, UInt64& result)
   at System.Enum.TryParseInt32Enum(RuntimeType enumType, String originalValueString, ReadOnlySpan`1 value, Int32 minInclusive, Int32 maxInclusive, Boolean ignoreCase, Boolean throwOnFailure, TypeCode type, Int32& result)
   at System.Enum.TryParse(Type enumType, String value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at YamlDotNet.Serialization.NodeDeserializers.ScalarNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value)

My initial analysis suggests that it's the call to Enum.Parse in ScalarNodeDeserializer that is performed with the underscored value (database_migration). Looking further into how INamingConvention is used, it appears that it's only used for property values (I might be wrong here!).

Describe the solution you'd like

I would like to deserialize YAML with snake case property values into C# enums defined in pascal case. It would be preferable if I could serialize C# DTOs using pascal case enums values into YAML with snake case property values (e.g. roundtrip).

Describe alternatives you've considered

Using a C# enum with snake case works, but is not desirable

enum ProjectType
{
  database_migration
}
@EdwardCooke
Copy link
Collaborator

I doubt it will work, but can you try the yamlmember attribute and set the alias property? I suspect it won’t though. If you want to implement that if it doesn’t we’re always open to pull requests.

@pardahlman
Copy link
Author

Thanks for getting back on this. I'll take a look at yamlmember and get back to you if I find the time to implement this feature.

@EdwardCooke EdwardCooke changed the title Support naming conventions for values Support naming conventions for enum members Feb 7, 2023
@SlimeNull
Copy link

This is indeed necessary.

EdwardCooke added a commit that referenced this issue Jan 22, 2024
Enables #773 

Adds naming conventions for convert enums to/from scalars
Adds .NET8 binaries
Adds support for custom formatting of enums, easy to expose enums as integers or whatever else.
Quotes necessary enum strings (Null for example)

Breaking: For those that get impacted pass in NullNamingConvention.Instance to the EnumNamingConvetion arguments on the constructor
Breaking: Removed many of the redundant constructors for the classes, pass in the old default values to the new constructors

+semver:breaking
@aaubry
Copy link
Owner

aaubry commented Jan 23, 2024

A fix for this issue has been released in version 15.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants