Skip to content

Commit 42a8a28

Browse files
tanushahande2003tanushah
andauthored
Improve JSON handling and HTML encoding in API helpers (#4489)
* Improve JSON handling and HTML encoding in API helpers Refactored WireMockAPI to use System.Text.Json for payload serialization and robust JSON parsing for stub mappings. Enhanced GenAIServiceHelper to HTML-encode error logs and multipart form data, ensuring safer handling of special characters and null values. * Refactor error logging to use exception overload Simplified exception handling by replacing manual error message construction and encoding with a single Reporter.ToLog call that passes the exception object, leveraging built-in logging features. --------- Co-authored-by: tanushah <tanushah@amdocs.com>
1 parent bccf642 commit 42a8a28

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

Ginger/GingerCoreNET/External/WireMock/WireMockAPI.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
using System;
2323
using System.Net.Http;
2424
using System.Text;
25+
using System.Text.Json;
2526
using System.Threading.Tasks;
2627

2728
namespace Amdocs.Ginger.CoreNET.External.WireMock
@@ -99,7 +100,8 @@ public async Task<string> StartRecordingAsync(string targetUrl)
99100

100101
using (HttpClient client = new HttpClient())
101102
{
102-
var content = new StringContent($"{{\"targetBaseUrl\": \"{targetUrl}\"}}", Encoding.UTF8, "application/json");
103+
string json = JsonSerializer.Serialize(new { targetBaseUrl = targetUrl });
104+
var content = new StringContent(json, Encoding.UTF8, "application/json");
103105
HttpResponseMessage response = await client.PostAsync($"{NormalizeUrl(GingerCore.ValueExpression.PasswordCalculation(_baseUrl))}{StartRecordingEndpoint}", content);
104106
response.EnsureSuccessStatusCode();
105107
return await response.Content.ReadAsStringAsync();
@@ -153,7 +155,19 @@ public async Task<string> CreateStubAsync(string stubMapping, string contentType
153155
{
154156
contentType = "application/json";
155157
}
156-
var content = new StringContent(stubMapping, Encoding.UTF8, contentType);
158+
159+
string body;
160+
if (string.IsNullOrEmpty(stubMapping))
161+
{
162+
body = stubMapping;
163+
}
164+
else
165+
{
166+
using JsonDocument doc = JsonDocument.Parse(stubMapping);
167+
body = doc.RootElement.GetRawText();
168+
}
169+
170+
var content = new StringContent(body, Encoding.UTF8, contentType);
157171
HttpResponseMessage response = await client.PostAsync($"{NormalizeUrl(GingerCore.ValueExpression.PasswordCalculation(_baseUrl))}{MappingEndpoint}", content);
158172
response.EnsureSuccessStatusCode();
159173
return await response.Content.ReadAsStringAsync();
@@ -204,7 +218,18 @@ public async Task<string> UpdateStubAsync(string stubId, string stubMapping)
204218

205219
using (HttpClient client = new HttpClient())
206220
{
207-
var content = new StringContent(stubMapping, Encoding.UTF8, "application/json");
221+
string body;
222+
if (string.IsNullOrEmpty(stubMapping))
223+
{
224+
body = stubMapping;
225+
}
226+
else
227+
{
228+
using JsonDocument doc = JsonDocument.Parse(stubMapping);
229+
body = doc.RootElement.GetRawText();
230+
}
231+
232+
var content = new StringContent(body, Encoding.UTF8, "application/json");
208233
HttpResponseMessage response = await client.PutAsync($"{NormalizeUrl(GingerCore.ValueExpression.PasswordCalculation(_baseUrl))}{MappingEndpoint}/{stubId}", content);
209234
response.EnsureSuccessStatusCode();
210235
return await response.Content.ReadAsStringAsync();

Ginger/GingerCoreNET/GenAIServices/GenAIServiceHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
using System;
2323
using System.Collections.Generic;
2424
using System.IdentityModel.Tokens.Jwt;
25+
using System.Net;
2526
using System.Net.Http;
2627
using System.Threading.Tasks;
2728

@@ -104,8 +105,7 @@ private async Task<bool> GetToken()
104105
catch (Exception ex)
105106
{
106107
var error = "Failed to get access token";
107-
Reporter.ToLog(eLogLevel.ERROR, $"{error}, Error :{ex.Message}");
108-
Reporter.ToLog(eLogLevel.ERROR, $"{error}, Error :{ex.Message}, InnerException:{ex.InnerException},StackTrace:{ex.StackTrace}");
108+
Reporter.ToLog(eLogLevel.ERROR, error, ex);
109109
return false;
110110
}
111111
}
@@ -245,11 +245,11 @@ private MultipartFormDataContent PrepareRequestDetailsForChat(string Question)
245245
var content = new MultipartFormDataContent
246246
{
247247
{ new StringContent(Question), "question" },
248-
{ new StringContent(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.Account)), "account" },
249-
{ new StringContent(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.DomainType)), "domainType" },
250-
{ new StringContent(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.TemperatureLevel)), "temperatureVal" },
251-
{ new StringContent(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.MaxTokenValue)), "maxTokensVal" },
252-
{ new StringContent(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.DataPath)), "dataPath" }
248+
{ new StringContent(WebUtility.HtmlEncode(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.Account) ?? string.Empty)), "account" },
249+
{ new StringContent(WebUtility.HtmlEncode(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.DomainType) ?? string.Empty)), "domainType" },
250+
{ new StringContent(WebUtility.HtmlEncode(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.TemperatureLevel) ?? string.Empty)), "temperatureVal" },
251+
{ new StringContent(WebUtility.HtmlEncode(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.MaxTokenValue) ?? string.Empty)), "maxTokensVal" },
252+
{ new StringContent(WebUtility.HtmlEncode(CredentialsCalculation(WorkSpace.Instance.Solution.AskLisaConfiguration.DataPath) ?? string.Empty)), "dataPath" }
253253
};
254254
return content;
255255
}

0 commit comments

Comments
 (0)