feat(swift): add final classes option#2947
Merged
Merged
Conversation
Member
|
Could you put this behind an option that's default off please? It's a breaking change otherwise. |
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
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
Add an opt-in
--final-classesSwift renderer option. When enabled, it emits every generated Swift reference type as afinal class:--struct-or-class classJSONNull,JSONCodingKey, andJSONAnyCodable helpersThe option defaults to
false, so existing Swift output remains unchanged. Struct output is unchanged in either mode.Related Issue
Closes #1241
Motivation and Context
Final classes communicate that generated models are not intended as subclassing surfaces and can avoid dynamic subclass dispatch. Making the behavior opt-in provides that benefit without introducing a breaking change for users who currently subclass generated types.
The renderer previously had no option for this behavior: it selected only
classorstructfor model declarations and hard-coded bareclassdeclarations for helper types.Previous Behaviour / Output
New Behaviour / Output
With
--final-classes:How Has This Been Tested?
npm run test:unit: 17 files and 130 tests passed with no type errorsFIXTURE=swift npm run test:fixtures: all 241 Swift fixture runs passed with Swift 6.2.3, including the explicitstruct-or-class: class, final-classes: truevariantnpm run buildgit diff --checkclassby default andfinal classwith--final-classesThe repository's requested Node 24.6.0 was not installed in this environment; validation used Node 25.6.1. Implementation and validation were assisted by OpenAI Codex and reviewed against the repository guidance.
Screenshots (if appropriate):
Not applicable; this changes generated source output.