Skip to content

Commit

Permalink
Handle null objects in validation helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
peombwa committed Dec 5, 2023
1 parent fdc2a7e commit 1626b7e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/lib/Helpers/ValidationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ internal static void ValidateBaseUrl(string parameterName, string? baseUrl)
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessage.BaseUrlIsNotValid, nameof(baseUrl)), parameterName);
}

internal static void ThrowIfNull(object? obj, string? paramName = null)
{
if (obj == null)
throw new ArgumentNullException(paramName);
}

private static readonly Regex s_emailRegex = new(@"^[^@\s]+@[^@\s]+$", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
private static void ValidateEmail(string parameterName, string value)
{
if (!s_emailRegex.IsMatch(value))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ErrorMessage.FieldIsNotValid, parameterName), parameterName);
}

public static void ThrowIfNull<T>(T obj, string paramName)
{
if (obj == null)
throw new ArgumentNullException(paramName);
}
}
}
6 changes: 4 additions & 2 deletions src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public static async Task<OpenAIPluginManifest> ToOpenAIPluginManifestAsync(this
}
else
{
var result = await ParsingHelpers.ParseOpenApiAsync(new Uri(apiDependency!.ApiDescriptionUrl), false, cancellationToken).ConfigureAwait(false);
return apiManifestDocument.ToOpenAIPluginManifest(result.OpenApiDocument, logoUrl, legalInfoUrl, openApiPath ?? apiDependency.ApiDescriptionUrl!);
var result = await ParsingHelpers.ParseOpenApiAsync(new Uri(apiDependency?.ApiDescriptionUrl), false, cancellationToken).ConfigureAwait(false);

Check warning on line 35 in src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Change this expression which always evaluates to the same result. (https://rules.sonarsource.com/csharp/RSPEC-2589)

Check warning on line 35 in src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Change this expression which always evaluates to the same result. (https://rules.sonarsource.com/csharp/RSPEC-2589)
if (string.IsNullOrWhiteSpace(openApiPath))
openApiPath = apiDependency?.ApiDescriptionUrl;

Check warning on line 37 in src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Change this expression which always evaluates to the same result. (https://rules.sonarsource.com/csharp/RSPEC-2589)

Check warning on line 37 in src/lib/TypeExtensions/ApiManifestDocumentExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Change this expression which always evaluates to the same result. (https://rules.sonarsource.com/csharp/RSPEC-2589)
return apiManifestDocument.ToOpenAIPluginManifest(result.OpenApiDocument, logoUrl, legalInfoUrl, openApiPath!);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/apimanifest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Company>Microsoft</Company>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<LangVersion>10</LangVersion>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SignAssembly>True</SignAssembly>
Expand Down

0 comments on commit 1626b7e

Please sign in to comment.