feat: Add CodeGenNamespace assembly attribute for enum namespace customization#10124
Open
radhgupta wants to merge 1 commit intomicrosoft:mainfrom
Open
feat: Add CodeGenNamespace assembly attribute for enum namespace customization#10124radhgupta wants to merge 1 commit intomicrosoft:mainfrom
radhgupta wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…omization
Add a new [assembly: CodeGenNamespace] attribute that allows changing the
namespace of generated types (especially fixed enums) without redefining
all members in custom code.
Usage:
[assembly: CodeGenNamespace("OriginalEnum", "NewNamespace.Models")]
This complements the existing @clientNamespace TCGC decorator for scenarios
where users cannot modify the TypeSpec spec or need C#-specific overrides.
Fixes microsoft#9086
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
commit: |
Contributor
|
No changes needing a change description found. |
Contributor
|
I'm not sure it is worth adding this just for enums when we can already customize in client.tsp. |
Member
Author
I see, should we mark the issue done then?? |
Contributor
I think we can close it for now as not fixing. |
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.
Problem
Fixes #9086
Users need to change the namespace of fixed C#
enumtypes, but since enums can't bepartial, they must redefine ALL enum members in custom code via[CodeGenType]. This prevents them from automatically catching spec updates — any new enum values added to the spec require manual custom code updates.Solution
Add a new
[assembly: CodeGenNamespace]attribute that changes the namespace of a generated type without requiring a replacement type definition:The generator still emits the full enum with all members — just in the new namespace. No custom enum definition needed, no member duplication.
Note on
@clientNamespaceAs Josh noted on the issue, the
@clientNamespaceTCGC decorator can also be used at the TypeSpec level to change namespaces. TheInputEnumType.Namespaceproperty already flows from TCGC → emitter → generator, so@clientNamespaceshould work for this scenario.The
[CodeGenNamespace]attribute complements@clientNamespacefor scenarios where:[CodeGenType],[CodeGenMember])Changes
Core implementation (5 files modified/added):
CodeGenNamespaceAttributeDefinition.cs— New TypeProvider that generates theCodeGenNamespaceAttributeclass (AttributeTargets.Assembly,AllowMultiple = true)SourceInputModel.cs— Scans assembly-levelCodeGenNamespaceattributes from custom code, builds atypeName → namespaceoverride mapTypeProvider.cs— AddedGetResolvedNamespace()with priority chain: CustomCodeView → CodeGenNamespace override → default namespaceCodeModelGenerator.cs— Registered new attribute; added safeGetNamespaceOverride()static accessor for initialization-time safetyCodeGenAttributes.cs— Added constant for the new attribute nameTests (12 test cases across 3 files):
[CodeGenType]takes precedence when both attributes presentGenerated:
CodeGenNamespaceAttribute.csinGenerated/Internal/(auto-generated viaGenerate.ps1)Test Results
All 2,660 tests pass across 5 test projects (0 failures).