diff --git a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityDefinitionDocumentFilter.cs b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityDefinitionDocumentFilter.cs index 20203e4..16428ec 100644 --- a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityDefinitionDocumentFilter.cs +++ b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityDefinitionDocumentFilter.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using Swashbuckle.AspNetCore.SwaggerGen; namespace Workleap.AspNetCore.Authentication.ClientCredentialsGrant.OpenAPI; @@ -16,7 +16,7 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { var apiPermissions = context.ApiDescriptions.SelectMany(SwaggerUtils.GetRequiredPermissions).ToHashSet(StringComparer.Ordinal); - swaggerDoc.Components.SecuritySchemes.Add( + swaggerDoc.Components?.SecuritySchemes?.Add( ClientCredentialsDefaults.OpenApiSecurityDefinitionId, new OpenApiSecurityScheme { diff --git a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityRequirementOperationFilter.cs b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityRequirementOperationFilter.cs index ec5cbd5..745f304 100644 --- a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityRequirementOperationFilter.cs +++ b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/OpenAPI/SecurityRequirementOperationFilter.cs @@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; using Swashbuckle.AspNetCore.SwaggerGen; namespace Workleap.AspNetCore.Authentication.ClientCredentialsGrant.OpenAPI; @@ -32,30 +32,23 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context) private static void AddAuthenticationAndAuthorizationErrorResponse(OpenApiOperation operation) { - operation.Responses.TryAdd(StatusCodes.Status401Unauthorized.ToString(CultureInfo.InvariantCulture), new OpenApiResponse { Description = ReasonPhrases.GetReasonPhrase(StatusCodes.Status401Unauthorized) }); - operation.Responses.TryAdd(StatusCodes.Status403Forbidden.ToString(CultureInfo.InvariantCulture), new OpenApiResponse { Description = ReasonPhrases.GetReasonPhrase(StatusCodes.Status403Forbidden) }); + operation.Responses?.TryAdd(StatusCodes.Status401Unauthorized.ToString(CultureInfo.InvariantCulture), new OpenApiResponse { Description = ReasonPhrases.GetReasonPhrase(StatusCodes.Status401Unauthorized) }); + operation.Responses?.TryAdd(StatusCodes.Status403Forbidden.ToString(CultureInfo.InvariantCulture), new OpenApiResponse { Description = ReasonPhrases.GetReasonPhrase(StatusCodes.Status403Forbidden) }); } private void AddOperationSecurityReference(OpenApiOperation operation, HashSet permissions) { - var isAlreadyReferencingSecurityDefinition = operation.Security.Any(requirement => requirement.Keys.Any(key => key.Reference?.Id == ClientCredentialsDefaults.OpenApiSecurityDefinitionId)); + var isAlreadyReferencingSecurityDefinition = operation.Security?.Any(requirement => requirement.Keys.Any(key => key.Reference?.Id == ClientCredentialsDefaults.OpenApiSecurityDefinitionId)) ?? false; if (isAlreadyReferencingSecurityDefinition) { return; } - var securityScheme = new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = ClientCredentialsDefaults.OpenApiSecurityDefinitionId, - }, - }; + var securitySchemeRef = new OpenApiSecuritySchemeReference(ClientCredentialsDefaults.OpenApiSecurityDefinitionId); - operation.Security.Add(new OpenApiSecurityRequirement + operation.Security?.Add(new OpenApiSecurityRequirement { - [securityScheme] = this.ExtractScopes(permissions).ToList(), + [securitySchemeRef] = this.ExtractScopes(permissions).ToList(), }); } diff --git a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/Workleap.AspNetCore.Authentication.ClientCredentialsGrant.csproj b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/Workleap.AspNetCore.Authentication.ClientCredentialsGrant.csproj index 551f4de..b90a627 100644 --- a/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/Workleap.AspNetCore.Authentication.ClientCredentialsGrant.csproj +++ b/src/Workleap.AspNetCore.Authentication.ClientCredentialsGrant/Workleap.AspNetCore.Authentication.ClientCredentialsGrant.csproj @@ -14,7 +14,8 @@ - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/tests/WebApi.OpenAPI.SystemTest/SwaggerConfigurationExtensions.cs b/src/tests/WebApi.OpenAPI.SystemTest/SwaggerConfigurationExtensions.cs index 1613932..d51ef48 100644 --- a/src/tests/WebApi.OpenAPI.SystemTest/SwaggerConfigurationExtensions.cs +++ b/src/tests/WebApi.OpenAPI.SystemTest/SwaggerConfigurationExtensions.cs @@ -1,4 +1,4 @@ -using Microsoft.OpenApi.Models; +using Microsoft.OpenApi; namespace WebApi.OpenAPI.SystemTest; diff --git a/src/tests/WebApi.OpenAPI.SystemTest/WebApi.OpenAPI.SystemTest.csproj b/src/tests/WebApi.OpenAPI.SystemTest/WebApi.OpenAPI.SystemTest.csproj index dcc2fe2..af3c0e1 100644 --- a/src/tests/WebApi.OpenAPI.SystemTest/WebApi.OpenAPI.SystemTest.csproj +++ b/src/tests/WebApi.OpenAPI.SystemTest/WebApi.OpenAPI.SystemTest.csproj @@ -3,16 +3,19 @@ net8.0 false false + false + false - - - + + + +