@@ -583,6 +583,135 @@ private static string GenerateSuccessResponseReturnWithoutBody(
583583 headers: { GetSuccessResponseHeadersExpression ( endPoint ) } );" ;
584584 }
585585
586+ private static bool IsJsonMediaType ( string mediaType )
587+ {
588+ return ! string . IsNullOrWhiteSpace ( mediaType ) &&
589+ mediaType . Contains ( "json" , StringComparison . OrdinalIgnoreCase ) ;
590+ }
591+
592+ private static bool ShouldUseSystemNetHttpJsonForRequest ( EndPoint endPoint )
593+ {
594+ return endPoint . Settings . GenerateMethodsUsingSystemNetHttpJson &&
595+ endPoint . Settings . UsesSystemTextJson ( ) &&
596+ IsJsonMediaType ( endPoint . RequestMediaType ) &&
597+ ! endPoint . IsMultipartFormData &&
598+ ! endPoint . RequestType . IsBinary &&
599+ ! endPoint . RequestType . IsBase64 ;
600+ }
601+
602+ private static bool ShouldUseSystemNetHttpJsonForSuccessResponse ( EndPoint endPoint )
603+ {
604+ return endPoint . Settings . GenerateMethodsUsingSystemNetHttpJson &&
605+ endPoint . Settings . UsesSystemTextJson ( ) &&
606+ endPoint . ContentType == ContentType . String &&
607+ endPoint . SuccessResponse . Type . CSharpTypeWithoutNullability is not "string" &&
608+ IsJsonMediaType ( endPoint . SuccessResponse . MimeType ) &&
609+ ! endPoint . SuccessResponse . Type . UsesGeneratedJsonHelpers ;
610+ }
611+
612+ private static string GenerateSystemNetHttpJsonRequestData (
613+ EndPoint endPoint )
614+ {
615+ if ( endPoint . Settings . HasJsonSerializerContext ( ) )
616+ {
617+ return $@ "
618+ var __httpRequestContent = global::{ endPoint . Settings . Namespace } .AutoSdkPolyfills.CreateJsonContent(
619+ inputValue: request,
620+ inputType: request.GetType(),
621+ mediaType: ""{ endPoint . RequestMediaType } "",
622+ jsonSerializerContext: JsonSerializerContext);
623+ __httpRequest.Content = __httpRequestContent;
624+ " . RemoveBlankLinesWhereOnlyWhitespaces ( ) ;
625+ }
626+
627+ return $@ "
628+ var __httpRequestContent = global::{ endPoint . Settings . Namespace } .AutoSdkPolyfills.CreateJsonContent(
629+ inputValue: request,
630+ mediaType: ""{ endPoint . RequestMediaType } "",
631+ jsonSerializerOptions: JsonSerializerOptions);
632+ __httpRequest.Content = __httpRequestContent;
633+ " . RemoveBlankLinesWhereOnlyWhitespaces ( ) ;
634+ }
635+
636+ private static string GenerateSystemNetHttpJsonReadCall (
637+ EndPoint endPoint )
638+ {
639+ var type = endPoint . SuccessResponse . Type . CSharpTypeWithNullabilityForValueTypes ;
640+
641+ if ( endPoint . Settings . HasJsonSerializerContext ( ) )
642+ {
643+ return $ "await global::{ endPoint . Settings . Namespace } .AutoSdkPolyfills.ReadFromJsonAsync<{ type } >(__response.Content, JsonSerializerContext, cancellationToken).ConfigureAwait(false)";
644+ }
645+
646+ return $ "await global::{ endPoint . Settings . Namespace } .AutoSdkPolyfills.ReadFromJsonAsync<{ type } >(__response.Content, JsonSerializerOptions, cancellationToken).ConfigureAwait(false)";
647+ }
648+
649+ private static string GenerateUnbufferedSuccessResponseHandling (
650+ EndPoint endPoint ,
651+ bool wrapSuccessResponse )
652+ {
653+ var jsonSerializer = endPoint . Settings . JsonSerializerType . GetSerializer ( ) ;
654+
655+ if ( string . IsNullOrWhiteSpace ( endPoint . SuccessResponse . Type . CSharpType ) )
656+ {
657+ return GenerateSuccessResponseReturnWithoutBody ( endPoint , wrapSuccessResponse ) ;
658+ }
659+
660+ if ( endPoint . ContentType == ContentType . String &&
661+ endPoint . SuccessResponse . Type . CSharpTypeWithoutNullability is not "string" )
662+ {
663+ if ( ShouldUseSystemNetHttpJsonForSuccessResponse ( endPoint ) )
664+ {
665+ var readCall = GenerateSystemNetHttpJsonReadCall ( endPoint ) ;
666+
667+ return wrapSuccessResponse
668+ ? $@ "var __value = { readCall } ??
669+ throw new global::System.InvalidOperationException(""Response deserialization failed."");
670+ { GenerateSuccessResponseReturn ( endPoint , "__value" , wrapSuccessResponse ) } "
671+ : $@ "return
672+ { readCall } ??
673+ throw new global::System.InvalidOperationException(""Response deserialization failed."");" ;
674+ }
675+
676+ return wrapSuccessResponse
677+ ? $@ "using var __content = await __response.Content.ReadAsStreamAsync(
678+ #if NET5_0_OR_GREATER
679+ cancellationToken
680+ #endif
681+ ).ConfigureAwait(false);
682+
683+ var __value = { jsonSerializer . GenerateDeserializeFromStreamCall ( "__content" , endPoint . SuccessResponse . Type , endPoint . Settings . JsonSerializerContext ) } ??
684+ throw new global::System.InvalidOperationException(""Response deserialization failed."");
685+ { GenerateSuccessResponseReturn ( endPoint , "__value" , wrapSuccessResponse ) } "
686+ : $@ "using var __content = await __response.Content.ReadAsStreamAsync(
687+ #if NET5_0_OR_GREATER
688+ cancellationToken
689+ #endif
690+ ).ConfigureAwait(false);
691+
692+ return
693+ { jsonSerializer . GenerateDeserializeFromStreamCall ( "__content" , endPoint . SuccessResponse . Type , endPoint . Settings . JsonSerializerContext ) } ??
694+ throw new global::System.InvalidOperationException(""Response deserialization failed."");" ;
695+ }
696+
697+ return $@ "{ endPoint . ContentType switch
698+ {
699+ ContentType . Stream => "using " ,
700+ _ => string . Empty ,
701+ } } var __content = await __response.Content.ReadAs{ endPoint . ContentType switch
702+ {
703+ ContentType . String => "String" ,
704+ ContentType . Stream => "Stream" ,
705+ _ => "ByteArray" ,
706+ } } Async(
707+ #if NET5_0_OR_GREATER
708+ cancellationToken
709+ #endif
710+ ).ConfigureAwait(false);
711+
712+ { GenerateSuccessResponseReturn ( endPoint , "__content" , wrapSuccessResponse ) } " ;
713+ }
714+
586715 public static string GenerateResponse (
587716 EndPoint endPoint ,
588717 bool wrapSuccessResponse = false )
@@ -897,34 +1026,7 @@ public static string GenerateResponse(
8971026 try
8981027 {{
8991028 __response.EnsureSuccessStatusCode();
900-
901- { endPoint . ContentType switch
902- {
903- ContentType . String when endPoint . SuccessResponse . Type . CSharpTypeWithoutNullability is not "string" => "using " ,
904- ContentType . Stream => "using " ,
905- _ => string . Empty ,
906- } } var __content = await __response.Content.ReadAs{ endPoint . ContentType switch
907- {
908- ContentType . String when endPoint . SuccessResponse . Type . CSharpTypeWithoutNullability is "string" => "String" ,
909- ContentType . String => "Stream" ,
910- ContentType . Stream => "Stream" ,
911- _ => "ByteArray" ,
912- } } Async(
913- #if NET5_0_OR_GREATER
914- cancellationToken
915- #endif
916- ).ConfigureAwait(false);
917-
918- { ( string . IsNullOrWhiteSpace ( endPoint . SuccessResponse . Type . CSharpType )
919- ? GenerateSuccessResponseReturnWithoutBody ( endPoint , wrapSuccessResponse )
920- : endPoint is { ContentType : ContentType . String , SuccessResponse . Type . CSharpTypeWithoutNullability : not "string" } ? wrapSuccessResponse ? $@ "
921- var __value = { jsonSerializer . GenerateDeserializeFromStreamCall ( "__content" , endPoint . SuccessResponse . Type , endPoint . Settings . JsonSerializerContext ) } ??
922- throw new global::System.InvalidOperationException(""Response deserialization failed."");
923- { GenerateSuccessResponseReturn ( endPoint , "__value" , wrapSuccessResponse ) } " : $@ "
924- return
925- { jsonSerializer . GenerateDeserializeFromStreamCall ( "__content" , endPoint . SuccessResponse . Type , endPoint . Settings . JsonSerializerContext ) } ??
926- throw new global::System.InvalidOperationException(""Response deserialization failed."");" : $@ "
927- { GenerateSuccessResponseReturn ( endPoint , "__content" , wrapSuccessResponse ) } " ) }
1029+ { GenerateUnbufferedSuccessResponseHandling ( endPoint , wrapSuccessResponse ) }
9281030 }}
9291031 catch (global::System.Exception __ex)
9301032 {{
@@ -1034,6 +1136,11 @@ public static string GenerateRequestData(
10341136 " . RemoveBlankLinesWhereOnlyWhitespaces ( ) ;
10351137 }
10361138
1139+ if ( ShouldUseSystemNetHttpJsonForRequest ( endPoint ) )
1140+ {
1141+ return GenerateSystemNetHttpJsonRequestData ( endPoint ) ;
1142+ }
1143+
10371144 var requestContent = endPoint . RequestType . IsBase64
10381145 ? "global::System.Convert.ToBase64String(request)"
10391146 : jsonSerializer . GenerateSerializeCall ( endPoint . RequestType , endPoint . Settings . JsonSerializerContext ) ;
0 commit comments