Skip to content

Restore EnumMemberAttribute for .net10#655

Merged
abergs merged 2 commits into
passwordless-lib:mainfrom
vpetrusevici:restore-enum-member
Apr 20, 2026
Merged

Restore EnumMemberAttribute for .net10#655
abergs merged 2 commits into
passwordless-lib:mainfrom
vpetrusevici:restore-enum-member

Conversation

@vpetrusevici

Copy link
Copy Markdown
Contributor

Summary

This PR updates the attribute pattern used for enum members across the project to ensure full compatibility with both System.Text.Json (specifically in .NET 9+) and Newtonsoft.Json.

Problem

Previously, enum members used a conditional #if NET9_0_OR_GREATER block with an #else clause that toggled between [JsonStringEnumMemberName] and [EnumMember].

#if NET9_0_OR_GREATER
    [JsonStringEnumMemberName("value")]
#else
    [EnumMember(Value = "value")]
#endif

While this worked for System.Text.Json, it broke compatibility with Newtonsoft.Json (and tools like NSwag that rely on it) when running on .NET 9+. Since Newtonsoft.Json does not recognize [JsonStringEnumMemberName], it would fall back to default naming, leading to serialization mismatches in environments where System.Text.Json is not the primary serializer.

Solution

The #else block has been removed, making [EnumMember] unconditional. [JsonStringEnumMemberName] remains wrapped in #if NET9_0_OR_GREATER to take advantage of native .NET 9 features while preserving the metadata needed by other libraries.

New Pattern:

#if NET9_0_OR_GREATER
    [JsonStringEnumMemberName("public-key")]
#endif
    [EnumMember(Value = "public-key")]
    PublicKey,

Key Changes

  • Removed #else from enum member attributes in all model files.
  • Ensured [EnumMember] is always present for every enum member.
  • Maintained existing #if logic for JsonConverter declarations and internal logic where appropriate.
  • Verified that these changes preserve correct serialization behavior in NSwag and other Newtonsoft.Json-dependent consumers.

@codecov

codecov Bot commented Apr 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.64%. Comparing base (1830c17) to head (f720524).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #655   +/-   ##
=======================================
  Coverage   78.64%   78.64%           
=======================================
  Files          98       98           
  Lines        2548     2548           
  Branches      425      425           
=======================================
  Hits         2004     2004           
  Misses        434      434           
  Partials      110      110           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vpetrusevici

Copy link
Copy Markdown
Contributor Author

@abergs sorry, we need to restore this attribute :)
It's still needs for Newtonsoft.Json cases...

@abergs

abergs commented Apr 16, 2026

Copy link
Copy Markdown
Collaborator

@vpetrusevici Have we verified that the original issue is still resolved when EnumMember is added?

And, is there a way to add tests to confirm the proper behavior in serialization?

@vpetrusevici

Copy link
Copy Markdown
Contributor Author

Yes. I'll try to add test, including Newtonsoft

@vpetrusevici

Copy link
Copy Markdown
Contributor Author

@vpetrusevici Have we verified that the original issue is still resolved when EnumMember is added?

And, is there a way to add tests to confirm the proper behavior in serialization?

tests added

@vpetrusevici

Copy link
Copy Markdown
Contributor Author

@abergs anything else? :)

@abergs
abergs merged commit b141b59 into passwordless-lib:main Apr 20, 2026
9 checks passed
@abergs

abergs commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator

All good, preview.3 will be rolling out.

@vpetrusevici
vpetrusevici deleted the restore-enum-member branch April 21, 2026 07:56
@vpetrusevici

Copy link
Copy Markdown
Contributor Author

@abergs hi! did you release it?

@vpetrusevici

Copy link
Copy Markdown
Contributor Author

@abergs, preview.3 is still unavailable :(

@abergs

abergs commented May 5, 2026

Copy link
Copy Markdown
Collaborator

@vpetrusevici Seems there was a github issue while running tests. Running everything again.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants