Skip to content

Commit

Permalink
fix: Fixed some responses with byte[].
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jul 14, 2024
1 parent 326fb38 commit e8ced08
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.13.4</Version>
<Version>0.13.5</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
3 changes: 3 additions & 0 deletions src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ .. includedTags.Select(x => PropertyData.Default with
]
: [],
HttpMethod: OperationType.Get,
ContentType: ContentType.String,
Summary: openApiDocument.Info?.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers!.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Settings: settings,
Expand All @@ -215,6 +216,7 @@ .. includedTags.Select(x => PropertyData.Default with
Path: string.Empty,
Properties: ImmutableArray<PropertyData>.Empty,
HttpMethod: OperationType.Get,
ContentType: ContentType.String,
Summary: x.Description?.ClearForXml() ?? string.Empty,
BaseUrlSummary: openApiDocument.Servers!.FirstOrDefault()?.Description?.ClearForXml() ?? string.Empty,
Settings: settings,
Expand Down Expand Up @@ -390,6 +392,7 @@ .. includedTags.Select(x => PropertyData.Default with
Path: string.Empty,
Properties: [],
HttpMethod: OperationType.Get,
ContentType: ContentType.String,
Summary: string.Empty,
BaseUrlSummary: string.Empty,
Settings: settings,
Expand Down
33 changes: 25 additions & 8 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public static string GenerateEndPoint(
var usings = endPoint.Properties.Any(x => x.Type.IsArray && x.ParameterExplode == true)
? "using System.Linq;\n"
: "";
var contentType = endPoint.ContentType switch
{
ContentType.String => "string",
ContentType.Stream => "global::System.IO.Stream",
_ => "byte[]",
};

return $@"{usings}
#nullable enable
Expand Down Expand Up @@ -49,7 +55,7 @@ public partial class {endPoint.ClassName}
partial void Process{endPoint.NotAsyncMethodName}ResponseContent(
global::System.Net.Http.HttpClient httpClient,
global::System.Net.Http.HttpResponseMessage httpResponseMessage,
ref string content);")}
ref {contentType} content);")}
{GenerateMethod(endPoint)}
{GenerateExtensionMethod(endPoint)}
Expand Down Expand Up @@ -91,6 +97,12 @@ public static string GenerateMethod(
var cancellationTokenInsideReadAsync = endPoint.Settings.TargetFramework.StartsWith("net8", StringComparison.OrdinalIgnoreCase)
? "cancellationToken"
: string.Empty;
var contentType = endPoint.ContentType switch
{
ContentType.String => "String",
ContentType.Stream => "Stream",
_ => "ByteArray",
};

return $@"
{endPoint.Summary.ToXmlDocumentationSummary(level: 8)}
Expand Down Expand Up @@ -173,29 +185,34 @@ public static string GenerateMethod(
{(string.IsNullOrWhiteSpace(endPoint.ResponseType.CSharpType) || endPoint.Stream ? @"
response.EnsureSuccessStatusCode();
" : $@"
var __content = await response.Content.ReadAsStringAsync({cancellationTokenInsideReadAsync}).ConfigureAwait(false);
var __content = await response.Content.ReadAs{contentType}Async({cancellationTokenInsideReadAsync}).ConfigureAwait(false);
{(endPoint.ContentType == ContentType.String ? @"
ProcessResponseContent(
client: _httpClient,
response: response,
content: ref __content);
content: ref __content);" : " ")}
Process{endPoint.NotAsyncMethodName}ResponseContent(
httpClient: _httpClient,
httpResponseMessage: response,
content: ref __content);
{(endPoint.ContentType == ContentType.String ? @"
try
{{
{
response.EnsureSuccessStatusCode();
}}
}
catch (global::System.Net.Http.HttpRequestException ex)
{{
{
throw new global::System.InvalidOperationException(__content, ex);
}}
}" : @"
response.EnsureSuccessStatusCode();")}
{(endPoint.ContentType == ContentType.String ? $@"
return
{jsonSerializer.GenerateDeserializeCall(endPoint.ResponseType, endPoint.Settings.JsonSerializerContext)} ??
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");")}
throw new global::System.InvalidOperationException($""Response deserialization failed for \""{{__content}}\"" "");" : @"
return __content;")}")}
{(endPoint.Stream ? $@"
using var stream = await response.Content.ReadAsStreamAsync({cancellationTokenInsideReadAsync}).ConfigureAwait(false);
using var reader = new global::System.IO.StreamReader(stream);
Expand Down
13 changes: 13 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/ContentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OpenApiGenerator.Core.Models;

#pragma warning disable CA1720 // Identifier contains type name

/// <summary>
/// HttpContent type.
/// </summary>
public enum ContentType
{
String,
ByteArray,
Stream,
}
5 changes: 5 additions & 0 deletions src/libs/OpenApiGenerator.Core/Models/EndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public readonly record struct EndPoint(
string Path,
ImmutableArray<PropertyData> Properties,
OperationType HttpMethod,
ContentType ContentType,
string Summary,
string BaseUrlSummary,
Settings Settings,
Expand Down Expand Up @@ -191,6 +192,10 @@ public static EndPoint FromSchema(
Path: preparedPath,
Properties: properties.ToImmutableArray(),
HttpMethod: operation.Key,
ContentType: responses
.Any(x => x.MediaType.Key.Contains("application/octet-stream"))
? ContentType.ByteArray
: ContentType.String,
Summary: operation.Value.GetXmlDocumentationSummary(),
BaseUrlSummary: string.Empty,
Settings: settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ partial void ProcessCreateSpeechResponse(
partial void ProcessCreateSpeechResponseContent(
global::System.Net.Http.HttpClient httpClient,
global::System.Net.Http.HttpResponseMessage httpResponseMessage,
ref string content);
ref byte[] content);

/// <summary>
/// Generates audio from the input text.
Expand Down Expand Up @@ -69,29 +69,17 @@ partial void ProcessCreateSpeechResponseContent(
httpClient: _httpClient,
httpResponseMessage: response);

var __content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var __content = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

ProcessResponseContent(
client: _httpClient,
response: response,
content: ref __content);
ProcessCreateSpeechResponseContent(
httpClient: _httpClient,
httpResponseMessage: response,
content: ref __content);

try
{
response.EnsureSuccessStatusCode();
}
catch (global::System.Net.Http.HttpRequestException ex)
{
throw new global::System.InvalidOperationException(__content, ex);
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]?>(__content, _jsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
response.EnsureSuccessStatusCode();

return __content;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ partial void ProcessCreateSpeechResponse(
partial void ProcessCreateSpeechResponseContent(
global::System.Net.Http.HttpClient httpClient,
global::System.Net.Http.HttpResponseMessage httpResponseMessage,
ref string content);
ref byte[] content);

/// <summary>
/// Generates audio from the input text.
Expand Down Expand Up @@ -69,29 +69,17 @@ partial void ProcessCreateSpeechResponseContent(
httpClient: _httpClient,
httpResponseMessage: response);

var __content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
var __content = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);

ProcessResponseContent(
client: _httpClient,
response: response,
content: ref __content);
ProcessCreateSpeechResponseContent(
httpClient: _httpClient,
httpResponseMessage: response,
content: ref __content);

try
{
response.EnsureSuccessStatusCode();
}
catch (global::System.Net.Http.HttpRequestException ex)
{
throw new global::System.InvalidOperationException(__content, ex);
}

return
global::System.Text.Json.JsonSerializer.Deserialize<byte[]?>(__content, _jsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
response.EnsureSuccessStatusCode();

return __content;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3618,6 +3618,7 @@ Default Value: 1,
}
],
HttpMethod: Post,
ContentType: ByteArray,
Summary: Generates audio from the input text.,
BaseUrlSummary: ,
Settings: {
Expand Down

0 comments on commit e8ced08

Please sign in to comment.