-
Notifications
You must be signed in to change notification settings - Fork 25
Fix: Enum schema generation bugs (raw values and backticks) #129
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The @Schemable macro now respects Swift's CodingKeys enum when generating JSON schemas. When a type defines a CodingKeys enum with custom string raw values, those values are used as property names in the generated schema. Key features: - Automatic extraction of CodingKeys mapping from enum definitions - Support for partial CodingKeys (missing entries fall back to property names) - Proper priority order: @SchemaOptions(.key) > CodingKeys > keyStrategy > property name - Works with both struct and class types - Handles nested types with their own CodingKeys Implementation: - Added extractCodingKeys() method to parse CodingKeys enum via SwiftSyntax - Updated schema generation to check CodingKeys mapping before other strategies - Maintains backward compatibility (no breaking changes)
…ature/coding-keys-support-clean
This fix ensures String-backed enums (enums conforming to RawRepresentable<String>)
generate schemas using their raw values instead of case names, making them compatible
with Swift's Codable behavior.
**Changes:**
- Updated SchemableEnumCase to extract and store raw values from enum cases
- Modified SchemaGenerator to use raw values in both enum value lists and switch statements
- Updated PollExampleTests to use raw values in test data
- Regenerated test snapshots to reflect correct schema output
- Enhanced documentation to clarify behavior for simple vs String-backed enums
**Example:**
```swift
enum Size: String {
case small = "sm"
case medium = "md"
}
```
Previously generated schema (incorrect):
```json
{"type": "string", "enum": ["small", "medium"]}
```
Now generates schema (correct):
```json
{"type": "string", "enum": ["sm", "md"]}
```
This ensures generated schemas validate JSON that Swift's Codable can decode.
Backticked Swift identifiers (e.g., case \`default\`) were incorrectly including the backticks in generated JSON schemas, causing schemas to use "\`default\`" instead of "default". **Changes:** - Added String.trimmingBackticks() extension to remove backticks - Updated SchemableEnumCase to strip backticks from case names - Updated SchemaGenerator to strip backticks when matching values - Added comprehensive tests for backticked enum cases **Test Coverage:** - BacktickEnumTests: Macro expansion tests for backticked cases - BacktickEnumIntegrationTests: Schema generation and parsing tests This ensures schemas match Swift's Codable behavior, which also strips backticks from identifiers during encoding/decoding.
…x/string-enum-raw-values
…x/string-enum-raw-values
ajevans99
approved these changes
Nov 5, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes two bugs in enum schema generation that caused incompatibility with Swift's
Codable:Bug 1: String-backed enums using case names instead of raw values
String-backed enums (
enum Size: String { case small = "sm" }) were generating schemas with case names (["small"]) instead of raw values (["sm"]). This broke compatibility withCodable, which uses raw values for encoding/decoding.Example:
{"type": "string", "enum": ["small", "medium"]}❌{"type": "string", "enum": ["sm", "md"]}✅This ensures generated schemas validate JSON that Swift's standard
JSONDecodercan decode.Bug 2: Backticked identifiers including backticks in schemas
Backticked Swift identifiers (e.g.,
case \default`) were incorrectly including the backticks in JSON schemas, generating"`default`"instead of"default"`.Example:
{"type": "string", "enum": ["\default`", "`public`"]}` ❌{"type": "string", "enum": ["default", "public"]}✅Both fixes ensure the generated schemas match Swift's
Codablebehavior.Changes Made
Core Implementation
String.trimmingBackticks()extension to strip backticks from identifiersDocumentation
Tests
"Parental Guidance Suggested 13+"instead of"pg13")Type of Change
Testing
All tests pass:
Test Coverage
Additional Notes
Impact
This is a correctness fix with no breaking changes to the API:
However, if users were relying on the incorrect behavior (schemas with case names for String-backed enums), their schemas will change. This is the correct behavior, as the previous output was incompatible with Codable.
Related Files Changed
Verification Steps
To verify the fix works correctly:
Raw values test:
Backticks test:
Codable compatibility: