Skip to content

Add SW003: Enforce [JsonConverter(typeof(JsonStringEnumConverter))] on enums#1

Draft
cliedeman with Copilot wants to merge 1 commit into
mainfrom
copilot/add-analyzer-for-enum-annotations
Draft

Add SW003: Enforce [JsonConverter(typeof(JsonStringEnumConverter))] on enums#1
cliedeman with Copilot wants to merge 1 commit into
mainfrom
copilot/add-analyzer-for-enum-annotations

Conversation

Copilot AI commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Enums without [JsonConverter(typeof(JsonStringEnumConverter))] serialize as integers under System.Text.Json, producing brittle, unreadable API contracts. SW003 flags any user-defined enum missing this annotation.

Changes

  • EnumJsonStringEnumConverterAnalyzer.cs — New analyzer (ID: SW003, Category: Serialization, Severity: Warning)
    • No-ops when System.Text.Json is not referenced
    • Accepts both JsonStringEnumConverter (non-generic) and JsonStringEnumConverter<TEnum> (.NET 8+)
    • Uses OriginalDefinition equality (not inheritance walk) for the sealed converter type
  • EnumJsonStringEnumConverterAnalyzerTests.cs — 4 test cases: missing attribute, correct attribute, wrong converter type, non-enum types
  • sw003-enum-json-string-enum-converter.md — Rule documentation

Example

// ⚠️ SW003 — serializes as 0, 1, ...
public enum OrderStatus { Pending, Shipped }

// ✅ OK — serializes as "Pending", "Shipped"
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum OrderStatus { Pending, Shipped }

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