Skip to content

Commit

Permalink
- Refactor constsnts
Browse files Browse the repository at this point in the history
- Use TryGet to avoid reference resolution errors
  • Loading branch information
Evans Aboge (from Dev Box) committed Jan 20, 2025
1 parent 673e08c commit 759a7ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,10 @@ private void AddOperationSecurityRequirementToDOM(OpenApiOperation operation, Co
{
foreach (var scheme in securityRequirement.Keys)
{
var securityScheme = securitySchemes[scheme.Reference.Id];
AddSecurity(codeClass, securityScheme);
if (securitySchemes.TryGetValue(scheme.Reference.Id, out var securityScheme))
{
AddSecurity(codeClass, securityScheme);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Kiota.Builder/Settings/VsCodeFileManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static class VsCodeSettingsManager
public static async Task UpdateFileAsync(string fileUpdate, string fileUpdatePath, string fileUpdateKey, CancellationToken cancellationToken)
{
ArgumentException.ThrowIfNullOrEmpty(fileUpdate);
ArgumentException.ThrowIfNullOrEmpty(fileUpdatePath);
ArgumentException.ThrowIfNullOrEmpty(fileUpdateKey);
Dictionary<string, object> settings = [];

// Read existing settings or create new if file doesn't exist
Expand Down
17 changes: 9 additions & 8 deletions src/Kiota.Builder/Writers/HTTP/CodeClassDeclarationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ private static class Constants
internal const string BearerAuth = "bearerAuth";
internal const string HttpVersion = "HTTP/1.1";
internal const string LocalHostUrl = "http://localhost/";

internal static Dictionary<string, string> SchemeTypeMapping = new()
{
{ SecuritySchemeType.ApiKey.ToString().ToLowerInvariant(), ApiKeyAuth },
{ SecuritySchemeType.Http.ToString().ToLowerInvariant(), BearerAuth },
{ SecuritySchemeType.OAuth2.ToString().ToLowerInvariant(), BearerAuth },
{ SecuritySchemeType.OpenIdConnect.ToString().ToLowerInvariant(), BearerAuth }
};
}

protected override void WriteTypeDeclaration(ClassDeclaration codeElement, LanguageWriter writer)
Expand Down Expand Up @@ -226,14 +234,7 @@ private static void WriteHttpMethods(

if (authenticationMethod != null && Enum.TryParse(typeof(SecuritySchemeType), authenticationMethod.Type.Name, true, out var _))
{
var schemeTypeMapping = new Dictionary<string, string>
{
{ SecuritySchemeType.ApiKey.ToString().ToLowerInvariant(), Constants.ApiKeyAuth },
{ SecuritySchemeType.Http.ToString().ToLowerInvariant(), Constants.BearerAuth },
{ SecuritySchemeType.OAuth2.ToString().ToLowerInvariant(), Constants.BearerAuth },
{ SecuritySchemeType.OpenIdConnect.ToString().ToLowerInvariant(), Constants.BearerAuth }
};
var schemeType = schemeTypeMapping[authenticationMethod.Type.Name.ToLowerInvariant()];
var schemeType = Constants.SchemeTypeMapping[authenticationMethod.Type.Name.ToLowerInvariant()];
writer.WriteLine($"Authorization: {{{{{schemeType}}}}}");
}

Expand Down

0 comments on commit 759a7ad

Please sign in to comment.