1- using Microsoft . OpenApi . Models ;
1+ using Microsoft . OpenApi ;
22
33namespace RestClient . Net . OpenApiGenerator ;
44
@@ -26,10 +26,15 @@ string basePath
2626
2727 foreach ( var path in document . Paths )
2828 {
29+ if ( path . Value ? . Operations == null )
30+ {
31+ continue ;
32+ }
33+
2934 foreach ( var operation in path . Value . Operations )
3035 {
3136 var responseType = GetResponseType ( operation . Value ) ;
32- var isDelete = operation . Key == OperationType . Delete ;
37+ var isDelete = operation . Key == HttpMethod . Delete ;
3338 var resultResponseType = isDelete ? "Unit" : responseType ;
3439 _ = responseTypes . Add ( resultResponseType ) ;
3540
@@ -123,7 +128,7 @@ private static async Task<string> DeserializeError(
123128
124129 private static ( string Method , List < string > PrivateFunctions ) GenerateMethod (
125130 string path ,
126- OperationType operationType ,
131+ HttpMethod operationType ,
127132 OpenApiOperation operation ,
128133 string basePath
129134 )
@@ -148,7 +153,7 @@ string basePath
148153
149154#pragma warning disable CA1502 // Avoid excessive complexity - code generator method is inherently complex
150155 private static ( string Method , List < string > PrivateFunctions ) GenerateHttpMethod (
151- OperationType operationType ,
156+ HttpMethod operationType ,
152157 string methodName ,
153158 string path ,
154159 List < ( string Name , string Type , bool IsPath ) > parameters ,
@@ -159,8 +164,10 @@ string summary
159164#pragma warning restore CA1502
160165 {
161166 var hasBody =
162- operationType is OperationType . Post or OperationType . Put or OperationType . Patch ;
163- var isDelete = operationType == OperationType . Delete ;
167+ operationType == HttpMethod . Post
168+ || operationType == HttpMethod . Put
169+ || operationType == HttpMethod . Patch ;
170+ var isDelete = operationType == HttpMethod . Delete ;
164171 var hasPathParams = parameters . Any ( p => p . IsPath ) ;
165172 var queryParams = parameters . Where ( p => ! p . IsPath ) . ToList ( ) ;
166173 var hasQueryParams = queryParams . Count > 0 ;
@@ -175,7 +182,12 @@ string summary
175182 ? "?" + string . Join ( "&" , queryParams . Select ( q => $ "{ q . Name } ={{{q.Name}}}") )
176183 : string . Empty ;
177184
178- var verb = operationType . ToString ( ) ;
185+ var verb = operationType == HttpMethod . Get ? "Get"
186+ : operationType == HttpMethod . Post ? "Post"
187+ : operationType == HttpMethod . Put ? "Put"
188+ : operationType == HttpMethod . Delete ? "Delete"
189+ : operationType == HttpMethod . Patch ? "Patch"
190+ : operationType . Method ;
179191 var createMethod = $ "Create{ verb } ";
180192 var delegateType = $ "{ verb } Async";
181193
@@ -369,7 +381,7 @@ string summary
369381
370382 private static string GetMethodName (
371383 OpenApiOperation operation ,
372- OperationType operationType ,
384+ HttpMethod operationType ,
373385 string path
374386 )
375387 {
@@ -382,15 +394,15 @@ string path
382394 path . Split ( '/' ) . LastOrDefault ( p => ! string . IsNullOrEmpty ( p ) && ! p . StartsWith ( '{' ) )
383395 ?? "Resource" ;
384396
385- return operationType switch
386- {
387- OperationType . Get => $ "Get { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
388- OperationType . Post => $ "Create { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
389- OperationType . Put => $ "Update { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
390- OperationType . Delete => $ "Delete { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
391- OperationType . Patch => $ "Patch { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
392- _ => $ " { operationType } { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ,
393- } ;
397+ var methodName =
398+ operationType == HttpMethod . Get ? "Get"
399+ : operationType == HttpMethod . Post ? "Create"
400+ : operationType == HttpMethod . Put ? "Update"
401+ : operationType == HttpMethod . Delete ? "Delete"
402+ : operationType == HttpMethod . Patch ? "Patch"
403+ : operationType . Method ;
404+
405+ return $ " { methodName } { CodeGenerationHelpers . ToPascalCase ( pathPart ) } " ;
394406 }
395407
396408 private static List < ( string Name , string Type , bool IsPath ) > GetParameters (
@@ -399,8 +411,18 @@ OpenApiOperation operation
399411 {
400412 var parameters = new List < ( string Name , string Type , bool IsPath ) > ( ) ;
401413
414+ if ( operation . Parameters == null )
415+ {
416+ return parameters ;
417+ }
418+
402419 foreach ( var param in operation . Parameters )
403420 {
421+ if ( param . Name == null )
422+ {
423+ continue ;
424+ }
425+
404426 var isPath = param . In == ParameterLocation . Path ;
405427 var type = ModelGenerator . MapOpenApiType ( param . Schema ) ;
406428 parameters . Add ( ( param . Name , type , isPath ) ) ;
@@ -409,11 +431,18 @@ OpenApiOperation operation
409431 return parameters ;
410432 }
411433
412- private static string ? GetRequestBodyType ( OpenApiOperation operation ) =>
413- operation . RequestBody == null ? null
414- : operation . RequestBody . Content . FirstOrDefault ( ) . Value ? . Schema ? . Reference != null
415- ? operation . RequestBody . Content . FirstOrDefault ( ) . Value . Schema . Reference . Id
416- : "object" ;
434+ private static string ? GetRequestBodyType ( OpenApiOperation operation )
435+ {
436+ if ( operation . RequestBody ? . Content == null )
437+ {
438+ return null ;
439+ }
440+
441+ var firstContent = operation . RequestBody . Content . FirstOrDefault ( ) ;
442+ return firstContent . Value ? . Schema is OpenApiSchemaReference schemaRef
443+ ? schemaRef . Reference . Id ?? "object"
444+ : "object" ;
445+ }
417446
418447 private static string GenerateTypeAliasesFile ( HashSet < string > responseTypes , string @namespace )
419448 {
@@ -488,20 +517,22 @@ _ when responseType.StartsWith("List<", StringComparison.Ordinal) =>
488517
489518 private static string GetResponseType ( OpenApiOperation operation )
490519 {
491- var successResponse = operation . Responses . FirstOrDefault ( r =>
520+ var successResponse = operation . Responses ? . FirstOrDefault ( r =>
492521 r . Key . StartsWith ( '2' ) || r . Key == "default"
493522 ) ;
494523
495- if ( successResponse . Value == null )
524+ if ( successResponse ? . Value ? . Content == null )
496525 {
497526 return "object" ;
498527 }
499528
500- var content = successResponse . Value . Content . FirstOrDefault ( ) ;
501- return content . Value ? . Schema ? . Reference != null ? content . Value . Schema . Reference . Id
502- : content . Value ? . Schema ? . Type == "array"
503- && content . Value . Schema . Items ? . Reference != null
504- ? $ "List<{ content . Value . Schema . Items . Reference . Id } >"
529+ var content = successResponse . Value . Value . Content . FirstOrDefault ( ) ;
530+ return content . Value ? . Schema is OpenApiSchemaReference schemaRef
531+ ? schemaRef . Reference . Id ?? "object"
532+ : content . Value ? . Schema is OpenApiSchema schema
533+ && schema . Type == JsonSchemaType . Array
534+ && schema . Items is OpenApiSchemaReference itemsRef
535+ ? $ "List<{ itemsRef . Reference . Id ?? "object" } >"
505536 : ModelGenerator . MapOpenApiType ( content . Value ? . Schema ) ;
506537 }
507538}
0 commit comments