Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: code linting #293

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions src/abstractions/MultipartBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public bool RemovePart(string partName)
private readonly Dictionary<string, Part> _parts = new Dictionary<string, Part>(StringComparer.OrdinalIgnoreCase);
/// <inheritdoc />
public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() => throw new NotImplementedException();
private const char DoubleQuote = '"';
/// <inheritdoc />
public void Serialize(ISerializationWriter writer)
{
Expand Down Expand Up @@ -123,13 +124,13 @@ public void Serialize(ISerializationWriter writer)
contentDispositionBuilder.Clear();
contentDispositionBuilder.Append("form-data; name=\"");
contentDispositionBuilder.Append(part.Name);
contentDispositionBuilder.Append("\"");
contentDispositionBuilder.Append(DoubleQuote);

if(part.FileName != null)
{
contentDispositionBuilder.Append("; filename=\"");
contentDispositionBuilder.Append(part.FileName);
contentDispositionBuilder.Append("\"");
contentDispositionBuilder.Append(DoubleQuote);
}

writer.WriteStringValue("Content-Disposition", contentDispositionBuilder.ToString());
Expand Down Expand Up @@ -176,12 +177,9 @@ public void Serialize(ISerializationWriter writer)
AddNewLine(writer);
writer.WriteStringValue(string.Empty, $"--{Boundary}--");
}
private void AddNewLine(ISerializationWriter writer)
{
writer.WriteStringValue(string.Empty, string.Empty);
}
private static void AddNewLine(ISerializationWriter writer) => writer.WriteStringValue(string.Empty, string.Empty);

private void WriteSerializedContent(ISerializationWriter writer, ISerializationWriter partWriter)
private static void WriteSerializedContent(ISerializationWriter writer, ISerializationWriter partWriter)
{
using var partContent = partWriter.GetSerializedContent();
if(partContent.CanSeek)
Expand All @@ -191,19 +189,11 @@ private void WriteSerializedContent(ISerializationWriter writer, ISerializationW
writer.WriteByteArrayValue(string.Empty, ms.ToArray());
}

private class Part
private sealed class Part(string name, object content, string contentType, string? fileName)
{
public Part(string name, object content, string contentType, string? fileName)
{
this.Name = name;
this.Content = content;
this.ContentType = contentType;
this.FileName = fileName;
}

public string Name { get; }
public object Content { get; }
public string ContentType { get; }
public string? FileName { get; }
public string Name { get; } = name;
public object Content { get; } = content;
public string ContentType { get; } = contentType;
public string? FileName { get; } = fileName;
}
}
3 changes: 2 additions & 1 deletion src/http/httpClient/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ private async Task<HttpResponseMessage> GetHttpResponseMessage(RequestInformatio
/// The key for the event raised by tracing when an authentication challenge is received
/// </summary>
public const string AuthenticateChallengedEventKey = "com.microsoft.kiota.authenticate_challenge_received";
private static readonly char[] ComaSplitSeparator = [','];

private async Task<HttpResponseMessage> RetryCAEResponseIfRequired(HttpResponseMessage response, RequestInformation requestInfo, CancellationToken cancellationToken, string? claims, Activity? activityForAttributes)
{
Expand All @@ -528,7 +529,7 @@ private async Task<HttpResponseMessage> RetryCAEResponseIfRequired(HttpResponseM

if(authHeader is not null)
{
var authHeaderParameters = authHeader.Parameter?.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var authHeaderParameters = authHeader.Parameter?.Split(ComaSplitSeparator, StringSplitOptions.RemoveEmptyEntries);

string? rawResponseClaims = null;
if(authHeaderParameters != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,30 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
activity?.Dispose();
}
}
private static readonly char[] EntriesSeparator = ['&'];
private static readonly char[] ParameterSeparator = ['='];

internal static string? DecodeUriEncodedString(string? original, char[] charactersToDecode)
{
if(string.IsNullOrEmpty(original) || charactersToDecode == null || charactersToDecode.Length == 0)
// for some reason static analysis is not picking up the fact that string.IsNullOrEmpty is already checking for null
if(original is null || original.Length == 0 || charactersToDecode == null || charactersToDecode.Length == 0)
return original;

var symbolsToReplace = new List<(string, string)>();
foreach(var character in charactersToDecode)
{
var symbol = ($"%{Convert.ToInt32(character):X}", character.ToString());
if(original?.Contains(symbol.Item1) ?? false)
if(original.Contains(symbol.Item1))
{
symbolsToReplace.Add(symbol);
}
}

var encodedParameterValues = new List<string>();
var parts = original?.TrimStart('?').Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
if(parts is null) return original;
var parts = original.TrimStart('?').Split(EntriesSeparator, StringSplitOptions.RemoveEmptyEntries);
foreach(var part in parts)
{
var parameter = part.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[0];
var parameter = part.Split(ParameterSeparator, StringSplitOptions.RemoveEmptyEntries)[0];
if(parameter.Contains("%")) // only pull out params with `%` (encoded)
{
encodedParameterValues.Add(parameter);
Expand All @@ -108,7 +110,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
updatedParameterName = updatedParameterName.Replace(symbolToReplace.Item1, symbolToReplace.Item2);
}
}
original = original?.Replace(parameter, updatedParameterName);
original = original.Replace(parameter, updatedParameterName);
}

return original;
Expand Down
10 changes: 6 additions & 4 deletions src/serialization/form/FormParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ public class FormParseNode : IParseNode
private readonly string RawValue;
private string DecodedValue => Uri.UnescapeDataString(RawValue);
private readonly Dictionary<string, string> Fields;
private static readonly char[] pairDelimiter = ['='];
private static readonly char[] entriesDelimiter = ['&'];
/// <summary>Initializes a new instance of the <see cref="FormParseNode"/> class.</summary>
/// <param name="rawValue">The raw value to parse.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="rawValue"/> is null.</exception>
public FormParseNode(string rawValue)
{
RawValue = rawValue ?? throw new ArgumentNullException(nameof(rawValue));
Fields = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
char[] pairDelimiter = new char[] { '=' };
string[] pairs = rawValue.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
string[] pairs = rawValue.Split(entriesDelimiter, StringSplitOptions.RemoveEmptyEntries);
foreach(string pair in pairs)
{
string[] keyValue = pair.Split(pairDelimiter, StringSplitOptions.RemoveEmptyEntries);
Expand Down Expand Up @@ -89,6 +90,7 @@ private static string SanitizeKey(string key)
private static readonly Type timeSpanType = typeof(TimeSpan?);
private static readonly Type dateType = typeof(Date?);
private static readonly Type timeType = typeof(Time?);
private static readonly char[] ComaSeparator = [','];

/// <summary>
/// Get the collection of primitives of type <typeparam name="T"/>from the form node
Expand All @@ -97,7 +99,7 @@ private static string SanitizeKey(string key)
public IEnumerable<T> GetCollectionOfPrimitiveValues<T>()
{
var genericType = typeof(T);
var primitiveValueCollection = DecodedValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var primitiveValueCollection = DecodedValue.Split(ComaSeparator, StringSplitOptions.RemoveEmptyEntries);
foreach(var collectionValue in primitiveValueCollection)
{
var currentParseNode = new FormParseNode(collectionValue)
Expand Down Expand Up @@ -223,7 +225,7 @@ private void AssignFieldValues<T>(T item) where T : IParsable
IEnumerable<T?> IParseNode.GetCollectionOfEnumValues<T>()
#endif
{
foreach(var v in DecodedValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
foreach(var v in DecodedValue.Split(ComaSeparator, StringSplitOptions.RemoveEmptyEntries))
yield return GetEnumValueInternal<T>(v);
}

Expand Down
Loading