@@ -22,7 +22,7 @@ public void RegisterHelper(ScriptObject scriptObject)
2222 {
2323 scriptObject . Import ( nameof ( GetInterfaceName ) , new Func < JsonSchema , string > ( GetInterfaceName ) ) ;
2424 scriptObject . Import ( nameof ( GetOperationId ) , new Func < OpenApiOperation , string , string > ( GetOperationId ) ) ;
25- scriptObject . Import ( nameof ( GetActionName ) , new Func < OpenApiOperationDescription , string , string , string > ( GetActionName ) ) ;
25+ scriptObject . Import ( nameof ( GetActionName ) , new Func < OpenApiOperation , string , string , string > ( GetActionName ) ) ;
2626 scriptObject . Import ( nameof ( GetDotNetName ) , new Func < string , string , string > ( GetDotNetName ) ) ;
2727 scriptObject . Import ( nameof ( GetDotNetNameOpenApiParameter ) , new Func < OpenApiParameter , string , string > ( GetDotNetNameOpenApiParameter ) ) ;
2828 }
@@ -164,59 +164,28 @@ public static string GetOperationId(OpenApiOperation watchOperation, string suff
164164 return methodName ;
165165 }
166166
167- public static string GetActionName ( OpenApiOperationDescription apiOperation , string resource , string suffix )
167+ public static string GetActionName ( OpenApiOperation apiOperation , string resource , string suffix )
168168 {
169- var actionType = apiOperation . Operation ? . ExtensionData ? [ "x-kubernetes-action" ] as string ;
170-
171- if ( string . IsNullOrEmpty ( actionType ) )
172- {
173- return $ "{ apiOperation . Method . ToPascalCase ( ) } { suffix } ";
174- }
175-
176- var resourceNamespace = ParsePathSegmentAfterParameter ( apiOperation . Path , "namespace" ) . ToPascalCase ( ) ;
177- var resourceName = ParsePathSegmentAfterParameter ( apiOperation . Path , "name" ) . ToPascalCase ( ) ;
178- var actionMappings = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase )
169+ var operationId = apiOperation . OperationId . ToPascalCase ( ) ;
170+ var replacements = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase )
179171 {
180- { "get" , "Get" } ,
181- { "list" , "List" } ,
182- { "put" , "Put" } ,
183- { "patch" , "Patch" } ,
184- { "post" , "Post" } ,
185- { "delete" , "Delete" } ,
186- { "deletecollection" , "DeleteCollection" } ,
187- { "watch" , "Watch" } ,
188- { "watchlist" , "WatchList" } ,
189- { "proxy" , "Proxy" } ,
172+ { "Replace" , "Put" } ,
173+ { "Read" , "Get" } ,
174+ { "Create" , "Post" } ,
190175 } ;
191176
192- if ( actionMappings . TryGetValue ( actionType , out var actionPrefix ) )
193- {
194- return Regex . Replace ( $ "{ actionPrefix } { resourceNamespace } { resourceName } { suffix } ", resource , string . Empty ,
195- RegexOptions . IgnoreCase ) ;
196- }
197-
198- if ( string . Equals ( "connect" , actionType , StringComparison . OrdinalIgnoreCase ) )
177+ foreach ( var replacement in replacements )
199178 {
200- return Regex . Replace ( $ "Connect{ apiOperation . Method } { resourceNamespace } { resourceName } { suffix } ", resource ,
201- string . Empty ,
202- RegexOptions . IgnoreCase ) ;
179+ operationId = Regex . Replace ( operationId , replacement . Key , replacement . Value , RegexOptions . IgnoreCase ) ;
203180 }
204181
205- return $ "{ actionType . ToPascalCase ( ) } { suffix } ";
206- }
207-
208- private static string ParsePathSegmentAfterParameter ( string path , string variableName = "namespace" )
209- {
210- var pattern = $@ "/\{{{variableName}\}}/([^/]+)/?";
211-
212- var match = Regex . Match ( path , pattern ) ;
213-
214- if ( match . Success && match . Groups . Count > 1 )
215- {
216- return match . Groups [ 1 ] . Value ;
217- }
182+ var resources = new [ ] { resource , "ForAllNamespaces" , "Namespaced" } ;
183+ var pattern = string . Join ( "|" , Array . ConvertAll ( resources , Regex . Escape ) ) ;
184+ var actionName = pattern . Length > 0
185+ ? Regex . Replace ( operationId , pattern , string . Empty , RegexOptions . IgnoreCase )
186+ : operationId ;
218187
219- return string . Empty ;
188+ return $ " { actionName } { suffix } " ;
220189 }
221190 }
222191}
0 commit comments