Skip to content

Commit

Permalink
feat: Added ability to override JsonSerializerContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 14, 2024
1 parent d76a664 commit cf4d948
Show file tree
Hide file tree
Showing 4,284 changed files with 64,008 additions and 10,075 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public string GenerateConverterAttribute(string type)

public string GenerateSerializeCall(TypeData type, string jsonSerializerContext)
{
return "global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions)";
return "global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions)";
}

public string GenerateDeserializeCall(TypeData type, string jsonSerializerContext)
{
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type.CSharpTypeWithNullability}>(__content, _jsonSerializerOptions)";
return $"global::Newtonsoft.Json.JsonConvert.DeserializeObject<{type.CSharpTypeWithNullability}>(__content, JsonSerializerOptions)";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ public static string GetContextType(TypeData typeData, bool makeNullableRootIfVa
public string GenerateSerializeCall(TypeData type, string jsonSerializerContext)
{
return string.IsNullOrWhiteSpace(jsonSerializerContext)
? "global::System.Text.Json.JsonSerializer.Serialize(request, _jsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Serialize(request, global::{jsonSerializerContext}.Default.{GetContextType(type, makeNullableRootIfValueType: true)})";
? "global::System.Text.Json.JsonSerializer.Serialize(request, JsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Serialize(request, request.GetType(), JsonSerializerContext)";
}

public string GenerateDeserializeCall(TypeData type, string jsonSerializerContext)
{
return string.IsNullOrWhiteSpace(jsonSerializerContext)
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type.CSharpTypeWithNullability}>(__content, _jsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, global::{jsonSerializerContext}.Default.{GetContextType(type, makeNullableRootIfValueType: true)})";
? $"global::System.Text.Json.JsonSerializer.Deserialize<{type.CSharpTypeWithNullability}>(__content, JsonSerializerOptions)"
: $"global::System.Text.Json.JsonSerializer.Deserialize(__content, typeof({type.CSharpTypeWithNullabilityForValueTypes}), JsonSerializerContext) as {type.CSharpTypeWithNullabilityForValueTypes}";
}
}
21 changes: 14 additions & 7 deletions src/libs/AutoSDK/Sources/Sources.Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ public sealed partial class {endPoint.ClassName} : global::System.IDisposable
public const string BaseUrl = ""{endPoint.BaseUrl}"";
private readonly global::System.Net.Http.HttpClient _httpClient;
{string.Empty.ToXmlDocumentationSummary(level: 8)}
{(hasOptions ? $@"
private readonly {serializer.GetOptionsType()} _jsonSerializerOptions;" : " ")}
public {serializer.GetOptionsType()} JsonSerializerOptions {{ get; set; }}{(
endPoint.Id == "MainConstructor"
? $" = {serializer.CreateDefaultSettings(endPoint.Converters)};"
: $" = new {serializer.GetOptionsType()}();")}" : $@"
public global::System.Text.Json.Serialization.JsonSerializerContext JsonSerializerContext {{ get; set; }} = global::{endPoint.Settings.JsonSerializerContext}.Default;")}
{(endPoint.Properties.Length != 0 ? "\n" + endPoint.Properties.Select(x => $@"
{x.Summary.ToXmlDocumentationSummary(level: 8)}
public {x.Type.CSharpType} {x.Name} => new {x.Type.CSharpType}(_httpClient{(hasOptions ? ", jsonSerializerOptions: _jsonSerializerOptions" : "")});
public {x.Type.CSharpType} {x.Name} => new {x.Type.CSharpType}(_httpClient)
{{
{(hasOptions
? "JsonSerializerOptions = JsonSerializerOptions,"
: "JsonSerializerContext = JsonSerializerContext,")}
}};
").Inject() : " ")}
/// <summary>
Expand All @@ -42,14 +53,10 @@ public sealed partial class {endPoint.ClassName} : global::System.IDisposable
/// <param name=""jsonSerializerOptions""></param>" : " ")}
public {endPoint.ClassName}(
global::System.Net.Http.HttpClient? httpClient = null,
global::System.Uri? baseUri = null{(hasOptions ? $@",
{serializer.GetOptionsType()}? jsonSerializerOptions = null" : " ")}
)
global::System.Uri? baseUri = null)
{{
_httpClient = httpClient ?? new global::System.Net.Http.HttpClient();
_httpClient.BaseAddress ??= baseUri ?? new global::System.Uri(BaseUrl);
{(hasOptions ? $@"
_jsonSerializerOptions = _jsonSerializerOptions ?? {(endPoint.Id == "MainConstructor" ? serializer.CreateDefaultSettings(endPoint.Converters) : $"new {serializer.GetOptionsType()}()")};" : " ")}
Initialized(_httpClient);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ partial void ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponseConte
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ConvertDocumentFileStudioV1ChatFilesConvertPostResponse?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ConvertDocumentFileStudioV1ChatFilesConvertPostResponse?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ partial void ProcessV1AnswerResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/studio/v1/answer", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions);
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: __httpRequestContentBody,
encoding: global::System.Text.Encoding.UTF8,
Expand Down Expand Up @@ -91,7 +91,7 @@ partial void ProcessV1AnswerResponseContent(
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1AnswerResponse?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1AnswerResponse?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ partial void ProcessV1ConversationalRagResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/studio/v1/conversational-rag", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions);
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: __httpRequestContentBody,
encoding: global::System.Text.Encoding.UTF8,
Expand Down Expand Up @@ -91,7 +91,7 @@ partial void ProcessV1ConversationalRagResponseContent(
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ConversationalRagResult?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ConversationalRagResult?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ partial void ProcessV1EmbedResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + "/studio/v1/embed", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions);
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: __httpRequestContentBody,
encoding: global::System.Text.Encoding.UTF8,
Expand Down Expand Up @@ -91,7 +91,7 @@ partial void ProcessV1EmbedResponseContent(
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1EmbedResponse?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1EmbedResponse?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,108 @@ public sealed partial class Api : global::System.IDisposable
public const string BaseUrl = "https://api.ai21.com/";

private readonly global::System.Net.Http.HttpClient _httpClient;
private readonly global::Newtonsoft.Json.JsonSerializerSettings _jsonSerializerOptions;

/// <summary>
///
/// </summary>
public global::Newtonsoft.Json.JsonSerializerSettings JsonSerializerOptions { get; set; } = new global::Newtonsoft.Json.JsonSerializerSettings();


/// <summary>
///
/// </summary>
public JambaCompleteClient JambaComplete => new JambaCompleteClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public JambaCompleteClient JambaComplete => new JambaCompleteClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public CompletionClient Completion => new CompletionClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public CompletionClient Completion => new CompletionClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public CustomModelsClient CustomModels => new CustomModelsClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public CustomModelsClient CustomModels => new CustomModelsClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public DatasetsClient Datasets => new DatasetsClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public DatasetsClient Datasets => new DatasetsClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public ParaphraseClient Paraphrase => new ParaphraseClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public ParaphraseClient Paraphrase => new ParaphraseClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public SummarizeClient Summarize => new SummarizeClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public SummarizeClient Summarize => new SummarizeClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public SegmentationClient Segmentation => new SegmentationClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public SegmentationClient Segmentation => new SegmentationClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public GrammaticalErrorCorrectionsClient GrammaticalErrorCorrections => new GrammaticalErrorCorrectionsClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public GrammaticalErrorCorrectionsClient GrammaticalErrorCorrections => new GrammaticalErrorCorrectionsClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public LibraryManagementClient LibraryManagement => new LibraryManagementClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public LibraryManagementClient LibraryManagement => new LibraryManagementClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public RAGEngineClient RAGEngine => new RAGEngineClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public RAGEngineClient RAGEngine => new RAGEngineClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public ChatClient Chat => new ChatClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public ChatClient Chat => new ChatClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
///
/// </summary>
public TokenizeClient Tokenize => new TokenizeClient(_httpClient, jsonSerializerOptions: _jsonSerializerOptions);
public TokenizeClient Tokenize => new TokenizeClient(_httpClient)
{
JsonSerializerOptions = JsonSerializerOptions,
};

/// <summary>
/// Creates a new instance of the Api.
Expand All @@ -90,13 +130,10 @@ public sealed partial class Api : global::System.IDisposable
/// <param name="jsonSerializerOptions"></param>
public Api(
global::System.Net.Http.HttpClient? httpClient = null,
global::System.Uri? baseUri = null,
global::Newtonsoft.Json.JsonSerializerSettings? jsonSerializerOptions = null
)
global::System.Uri? baseUri = null)
{
_httpClient = httpClient ?? new global::System.Net.Http.HttpClient();
_httpClient.BaseAddress ??= baseUri ?? new global::System.Uri(BaseUrl);
_jsonSerializerOptions = _jsonSerializerOptions ?? new global::Newtonsoft.Json.JsonSerializerSettings();

Initialized(_httpClient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ partial void ProcessV1J2UltraChatResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + $"/studio/v1/j2-ultra/chat?model={model}", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions);
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: __httpRequestContentBody,
encoding: global::System.Text.Encoding.UTF8,
Expand Down Expand Up @@ -99,7 +99,7 @@ partial void ProcessV1J2UltraChatResponseContent(
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ChatResponse?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.ChatResponse?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public sealed partial class ChatClient : global::System.IDisposable
public const string BaseUrl = "https://api.ai21.com/";

private readonly global::System.Net.Http.HttpClient _httpClient;
private readonly global::Newtonsoft.Json.JsonSerializerSettings _jsonSerializerOptions;

/// <summary>
///
/// </summary>
public global::Newtonsoft.Json.JsonSerializerSettings JsonSerializerOptions { get; set; } = new global::Newtonsoft.Json.JsonSerializerSettings();


/// <summary>
Expand All @@ -29,13 +33,10 @@ public sealed partial class ChatClient : global::System.IDisposable
/// <param name="jsonSerializerOptions"></param>
public ChatClient(
global::System.Net.Http.HttpClient? httpClient = null,
global::System.Uri? baseUri = null,
global::Newtonsoft.Json.JsonSerializerSettings? jsonSerializerOptions = null
)
global::System.Uri? baseUri = null)
{
_httpClient = httpClient ?? new global::System.Net.Http.HttpClient();
_httpClient.BaseAddress ??= baseUri ?? new global::System.Uri(BaseUrl);
_jsonSerializerOptions = _jsonSerializerOptions ?? new global::Newtonsoft.Json.JsonSerializerSettings();

Initialized(_httpClient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ partial void ProcessV1J2GrandeCompleteResponseContent(
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
method: global::System.Net.Http.HttpMethod.Post,
requestUri: new global::System.Uri(_httpClient.BaseAddress?.AbsoluteUri.TrimEnd('/') + $"/studio/v1/j2-grande/complete?model={model}", global::System.UriKind.RelativeOrAbsolute));
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, _jsonSerializerOptions);
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
var __httpRequestContent = new global::System.Net.Http.StringContent(
content: __httpRequestContentBody,
encoding: global::System.Text.Encoding.UTF8,
Expand Down Expand Up @@ -99,7 +99,7 @@ partial void ProcessV1J2GrandeCompleteResponseContent(
}

return
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1J2GrandeCompleteResponse?>(__content, _jsonSerializerOptions) ??
global::Newtonsoft.Json.JsonConvert.DeserializeObject<global::G.V1J2GrandeCompleteResponse?>(__content, JsonSerializerOptions) ??
throw new global::System.InvalidOperationException($"Response deserialization failed for \"{__content}\" ");
}

Expand Down
Loading

0 comments on commit cf4d948

Please sign in to comment.