1212using Microsoft . TypeSpec . Generator . Primitives ;
1313using Microsoft . TypeSpec . Generator . Providers ;
1414using Microsoft . TypeSpec . Generator . Statements ;
15- using System ;
1615using System . Collections . Generic ;
1716using static Microsoft . TypeSpec . Generator . Snippets . Snippet ;
1817
@@ -27,6 +26,8 @@ internal class PageableOperationMethodProvider
2726 private readonly MethodProvider _convenienceMethod ;
2827 private readonly bool _isAsync ;
2928 private readonly CSharpType _itemType ;
29+ private readonly CSharpType _actualItemType ;
30+ private ResourceClientProvider ? _itemResourceClient ;
3031 private readonly ResourceOperationKind _methodKind ;
3132 private readonly MethodSignature _signature ;
3233 private readonly MethodBodyStatement [ ] _bodyStatements ;
@@ -48,11 +49,29 @@ public PageableOperationMethodProvider(
4849 _convenienceMethod = convenienceMethod ;
4950 _isAsync = isAsync ;
5051 _itemType = itemType ;
52+ InitializeTypeInfo (
53+ itemType ,
54+ ref _actualItemType ! ,
55+ ref _itemResourceClient
56+ ) ;
5157 _methodKind = methodKind ;
5258 _signature = CreateSignature ( ) ;
5359 _bodyStatements = BuildBodyStatements ( ) ;
5460 }
5561
62+ private static void InitializeTypeInfo (
63+ CSharpType itemType ,
64+ ref CSharpType actualItemType ,
65+ ref ResourceClientProvider ? resourceClient
66+ )
67+ {
68+ actualItemType = itemType ;
69+ if ( ManagementClientGenerator . Instance . OutputLibrary . TryGetResourceClientProvider ( itemType , out resourceClient ) )
70+ {
71+ actualItemType = resourceClient . Type ;
72+ }
73+ }
74+
5675 public static implicit operator MethodProvider ( PageableOperationMethodProvider pageableOperationMethodProvider )
5776 {
5877 return new MethodProvider (
@@ -63,11 +82,9 @@ public static implicit operator MethodProvider(PageableOperationMethodProvider p
6382
6483 protected MethodSignature CreateSignature ( )
6584 {
66- var actualItemType = IsResourceDataType ( _itemType ) ? GetResourceClientProvider ( ) . Type : _itemType ;
67-
6885 var returnType = _isAsync
69- ? new CSharpType ( typeof ( AsyncPageable < > ) , actualItemType )
70- : new CSharpType ( typeof ( Pageable < > ) , actualItemType ) ;
86+ ? new CSharpType ( typeof ( AsyncPageable < > ) , _actualItemType )
87+ : new CSharpType ( typeof ( Pageable < > ) , _actualItemType ) ;
7188 var methodName = _methodKind == ResourceOperationKind . List
7289 ? ( _isAsync ? "GetAllAsync" : "GetAll" )
7390 : _convenienceMethod . Signature . Name ;
@@ -102,9 +119,9 @@ protected MethodBodyStatement[] BuildBodyStatements()
102119 arguments . AddRange ( _contextualPath . PopulateArguments ( This . As < ArmResource > ( ) . Id ( ) , requestMethod . Signature . Parameters , contextVariable , _signature . Parameters ) ) ;
103120
104121 // Handle ResourceData type conversion if needed
105- if ( IsResourceDataType ( _itemType ) )
122+ if ( _itemResourceClient != null )
106123 {
107- statements . Add ( BuildResourceDataConversionStatement ( collectionResultOfT , arguments ) ) ;
124+ statements . Add ( BuildResourceDataConversionStatement ( collectionResultOfT , _itemResourceClient . Type , arguments ) ) ;
108125 }
109126 else
110127 {
@@ -114,21 +131,17 @@ protected MethodBodyStatement[] BuildBodyStatements()
114131 return statements . ToArray ( ) ;
115132 }
116133
117- private MethodBodyStatement BuildResourceDataConversionStatement ( CSharpType sourcePageable , List < ValueExpression > arguments )
134+ private MethodBodyStatement BuildResourceDataConversionStatement ( CSharpType sourcePageable , CSharpType typeOfResource , List < ValueExpression > arguments )
118135 {
119- // Get the resource client provider to access the resource type
120- var resourceClientProvider = GetResourceClientProvider ( ) ;
121- var resourceType = resourceClientProvider . Type ;
122-
123136 // Create PageableWrapper instance to convert from ResourceData to Resource
124137 var pageableWrapperType = _isAsync ? ManagementClientGenerator . Instance . OutputLibrary . AsyncPageableWrapper : ManagementClientGenerator . Instance . OutputLibrary . PageableWrapper ;
125138
126139 // Create the concrete wrapper type with proper generic parameters
127140 // Since pageableWrapperType.Type represents the constructed generic type, we need to use it directly
128- var concreteWrapperType = pageableWrapperType . Type . MakeGenericType ( [ _itemType , resourceType ] ) ;
141+ var concreteWrapperType = pageableWrapperType . Type . MakeGenericType ( [ _itemType , typeOfResource ] ) ;
129142
130143 // Create converter function: data => new ResourceType(Client, data)
131- var converterFunc = CreateConverterFunction ( _itemType , resourceType ) ;
144+ var converterFunc = CreateConverterFunction ( _itemType , typeOfResource ) ;
132145
133146 var wrapperArguments = new List < ValueExpression >
134147 {
@@ -139,30 +152,6 @@ private MethodBodyStatement BuildResourceDataConversionStatement(CSharpType sour
139152 return Return ( New . Instance ( concreteWrapperType , wrapperArguments ) ) ;
140153 }
141154
142- private bool IsResourceDataType ( CSharpType itemType )
143- {
144- try
145- {
146- var resourceClientProvider = GetResourceClientProvider ( ) ;
147- return itemType . Equals ( resourceClientProvider . ResourceData . Type ) ;
148- }
149- catch ( InvalidOperationException )
150- {
151- // If we can't get a ResourceClientProvider, then this is not a ResourceData type
152- return false ;
153- }
154- }
155-
156- private ResourceClientProvider GetResourceClientProvider ( )
157- {
158- return _enclosingType switch
159- {
160- ResourceClientProvider rcp => rcp ,
161- ResourceCollectionClientProvider rccp => rccp . Resource , // Return the Resource property
162- _ => throw new InvalidOperationException ( $ "Expected ResourceClientProvider or ResourceCollectionClientProvider, but got: { _enclosingType . GetType ( ) } ")
163- } ;
164- }
165-
166155 private ValueExpression CreateConverterFunction ( CSharpType fromType , CSharpType toType )
167156 {
168157 // Create a lambda expression: data => new ResourceType(Client, data)
0 commit comments