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

Security fixes and updates for NotFound response types #208

Merged
merged 5 commits into from
Aug 22, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ public string Generate()
case HttpStatusCode.OK:
AppendAddSuccessResponseForStatusCodeOk(sb, responseModel);
break;
case HttpStatusCode.NotFound:
if (string.IsNullOrEmpty(customErrorResponseModel))
{
sb.AppendLine(8, $"responseBuilder.AddErrorResponse<string?>(HttpStatusCode.{responseModel.StatusCode});");
}
else
{
sb.AppendLine(8, $"responseBuilder.AddErrorResponse<{customErrorResponseModel}>(HttpStatusCode.{responseModel.StatusCode});");
}
break;
case HttpStatusCode.BadRequest:
if (string.IsNullOrEmpty(customErrorResponseModel))
{
Expand Down Expand Up @@ -153,7 +163,6 @@ public string Generate()
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ private void AppendContentWithProblemDetails(
sb.AppendLine();
AppendMethodContentStatusCodeOk(sb, responseModel);
break;
case HttpStatusCode.NotFound:
sb.AppendLine();
sb.AppendLine(4, $"public string? {responseModel.StatusCode.ToNormalizedString()}Content");
sb.AppendLine(8, $"=> Is{responseModel.StatusCode.ToNormalizedString()} && ContentObject is string result");
sb.AppendLine(12, "? result");
sb.AppendLine(12, $": throw new InvalidOperationException(\"Content is not the expected type - please use the Is{responseModel.StatusCode.ToNormalizedString()} property first.\");");
break;
case HttpStatusCode.BadRequest:
sb.AppendLine();
sb.AppendLine(4, $"public ValidationProblemDetails {responseModel.StatusCode.ToNormalizedString()}Content");
Expand Down Expand Up @@ -270,7 +277,6 @@ private void AppendContentWithProblemDetails(
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ private void AppendContentWithProblemDetails(
sb.AppendLine();
AppendMethodContentStatusCodeOk(sb, responseModel);
break;
case HttpStatusCode.NotFound:
sb.AppendLine();
sb.AppendLine(4, $"string? {responseModel.StatusCode.ToNormalizedString()}Content {{ get; }}");
break;
case HttpStatusCode.BadRequest:
sb.AppendLine();
sb.AppendLine(4, $"ValidationProblemDetails {responseModel.StatusCode.ToNormalizedString()}Content {{ get; }}");
Expand Down Expand Up @@ -242,7 +246,6 @@ private void AppendContentWithProblemDetails(
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private static void AppendProducesWithProblemDetails(
break;
case HttpStatusCode.Accepted:
case HttpStatusCode.Created:
case HttpStatusCode.NotFound:
sb.Append(12, $".Produces<string?>(StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})");
break;
case HttpStatusCode.EarlyHints:
Expand Down Expand Up @@ -229,7 +230,6 @@ private static void AppendProducesWithProblemDetails(
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ private void AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(
case HttpStatusCode.Accepted:
case HttpStatusCode.Created:
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? uri = null)");
sb.AppendLine(8, $"=> new(Results.Problem(uri, null, StatusCodes.{item.ResponseModel.StatusCode.ToStatusCodesConstant()}));");
sb.AppendLine(8, $"=> new(TypedResults.{item.ResponseModel.StatusCode}(uri));");
break;
case HttpStatusCode.NotFound:
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? message = null)");
sb.AppendLine(8, $"=> new(TypedResults.{item.ResponseModel.StatusCode}(message));");
break;
case HttpStatusCode.Conflict:
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? message = null)");
sb.AppendLine(8, $"=> new(Results.Problem(message, null, StatusCodes.{item.ResponseModel.StatusCode.ToStatusCodesConstant()}));");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ public void MaintainGlobalUsings(
requiredUsings.Add("Atc.Rest.MinimalApi.Filters.Endpoints");
}

// TODO: Check for any use ??
requiredUsings.Add("Microsoft.AspNetCore.Authorization");

if (operationSchemaMappings.Any(apiOperation => apiOperation.Model.IsShared))
{
requiredUsings.Add($"{projectName}.{ContentGeneratorConstants.Contracts}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private static void AppendProducesWithProblemDetails(
case HttpStatusCode.OK:
AppendProducesForOk(sb, responseModel);
break;
case HttpStatusCode.NotFound:
sb.AppendLine(4, $"[ProducesResponseType(typeof(string), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
break;
case HttpStatusCode.BadRequest:
sb.AppendLine(4, $"[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
break;
Expand Down Expand Up @@ -160,7 +163,6 @@ private static void AppendProducesWithProblemDetails(
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ private void AppendMethodContent(
{
if (useProblemDetailsAsDefaultResponseBody)
{
AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(sb, item, resultName);
AppendMethodContentForOtherStatusCodesThanOkWithProblemDetails(sb, item, resultName);
}
else
{
AppendMethodContentForOtherStatusCodesThenOkWithoutProblemDetails(sb, item, resultName);
AppendMethodContentForOtherStatusCodesThanOkWithoutProblemDetails(sb, item, resultName);
}
}
}
Expand Down Expand Up @@ -146,7 +146,7 @@ private void AppendMethodContentStatusCodeOk(
}
}

private void AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(
private void AppendMethodContentForOtherStatusCodesThanOkWithProblemDetails(
StringBuilder sb,
ContentGeneratorServerResultMethodParameters item,
string resultName)
Expand All @@ -160,6 +160,10 @@ private void AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? uri = null)");
sb.AppendLine(8, $"=> new {resultName}({nameof(Results.ResultFactory)}.{nameof(Results.ResultFactory.CreateContentResult)}({nameof(HttpStatusCode)}.{item.ResponseModel.StatusCode}, uri));");
break;
case HttpStatusCode.NotFound:
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? message = null)");
sb.AppendLine(8, $"=> new {resultName}(new {item.ResponseModel.StatusCode.ToNormalizedString()}ObjectResult(message));");
break;
case HttpStatusCode.BadRequest:
sb.AppendLine(4, $"public static {resultName} {item.ResponseModel.StatusCode.ToNormalizedString()}(string? message = null)");
sb.AppendLine(8, $"=> new {resultName}({nameof(Results.ResultFactory)}.{nameof(Results.ResultFactory.CreateContentResultWithValidationProblemDetails)}({nameof(HttpStatusCode)}.{item.ResponseModel.StatusCode}, message));");
Expand Down Expand Up @@ -190,7 +194,6 @@ private void AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(
case HttpStatusCode.Unauthorized:
case HttpStatusCode.PaymentRequired:
case HttpStatusCode.Forbidden:
case HttpStatusCode.NotFound:
case HttpStatusCode.MethodNotAllowed:
case HttpStatusCode.NotAcceptable:
case HttpStatusCode.ProxyAuthenticationRequired:
Expand Down Expand Up @@ -233,7 +236,7 @@ private void AppendMethodContentForOtherStatusCodesThenOkWithProblemDetails(
}
}

private void AppendMethodContentForOtherStatusCodesThenOkWithoutProblemDetails(
private void AppendMethodContentForOtherStatusCodesThanOkWithoutProblemDetails(
StringBuilder sb,
ContentGeneratorServerResultMethodParameters item,
string resultName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void AppendMethodContentAuthorizationIfNeeded(
var authRoles = authorizationForEndpoint.Roles is null
? null
: string.Join(',', authorizationForEndpoint.Roles);

var authSchemes = authorizationForEndpoint.AuthenticationSchemes is null
? null
: string.Join(',', authorizationForEndpoint.AuthenticationSchemes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public static bool IsUsingRequiredForMicrosoftAspNetCoreAuthorization(
foreach (var openApiPath in openApiDocument.Paths)
{
var isAuthenticationRequired = openApiPath.Value.Extensions.ExtractAuthenticationRequired();
if (isAuthenticationRequired is not null && isAuthenticationRequired.Value)
if (isAuthenticationRequired is not null)
{
return true;
}
Expand All @@ -377,7 +377,7 @@ public static bool IsUsingRequiredForMicrosoftAspNetCoreAuthorization(
}

var isOperationAuthenticationRequired = apiOperationPair.Value.Extensions.ExtractAuthenticationRequired();
if (isOperationAuthenticationRequired is not null && isOperationAuthenticationRequired.Value)
if (isOperationAuthenticationRequired is not null)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static string GetApiGroupName(

if ((authorizationRoles is null || authorizationRoles.Count == 0) &&
(authenticationSchemes is null || authenticationSchemes.Count == 0) &&
authenticationRequiredForPath.HasValueAndFalse())
authenticationRequiredForPath is null)
{
return null;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Atc.Rest.ApiGenerator/Generators/ServerApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public async Task<bool> Generate()
serverApiGeneratorMvc.GenerateEndpoints();

serverApiGeneratorMvc.MaintainApiSpecification(projectOptions.DocumentFile);
serverApiGeneratorMvc.MaintainGlobalUsings(
projectOptions.ApiOptions.Generator.RemoveNamespaceGroupSeparatorInGlobalUsings);
serverApiGeneratorMvc.MaintainGlobalUsings(projectOptions.ApiOptions.Generator.RemoveNamespaceGroupSeparatorInGlobalUsings);
}
else
{
Expand All @@ -94,8 +93,7 @@ public async Task<bool> Generate()
serverApiGeneratorMinimalApi.GenerateEndpoints();

serverApiGeneratorMinimalApi.MaintainApiSpecification(projectOptions.DocumentFile);
serverApiGeneratorMinimalApi.MaintainGlobalUsings(
projectOptions.ApiOptions.Generator.RemoveNamespaceGroupSeparatorInGlobalUsings);
serverApiGeneratorMinimalApi.MaintainGlobalUsings(projectOptions.ApiOptions.Generator.RemoveNamespaceGroupSeparatorInGlobalUsings);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -19,11 +19,7 @@ public interface ISetAccountNameEndpointResult : IEndpointResponse

bool IsBadRequest { get; }

bool IsUnauthorized { get; }

string? OkContent { get; }

string? BadRequestContent { get; }

string? UnauthorizedContent { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -19,11 +19,7 @@ public interface IUpdateAccountNameEndpointResult : IEndpointResponse

bool IsBadRequest { get; }

bool IsUnauthorized { get; }

string? OkContent { get; }

string? BadRequestContent { get; }

string? UnauthorizedContent { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand Down Expand Up @@ -42,7 +42,6 @@ public async Task<SetAccountNameEndpointResult> ExecuteAsync(
var responseBuilder = httpMessageFactory.FromResponse(response);
responseBuilder.AddSuccessResponse<string?>(HttpStatusCode.OK);
responseBuilder.AddErrorResponse<ValidationProblemDetails>(HttpStatusCode.BadRequest);
responseBuilder.AddErrorResponse<string>(HttpStatusCode.Unauthorized);
return await responseBuilder.BuildResponseAsync(x => new SetAccountNameEndpointResult(x), cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -25,9 +25,6 @@ public bool IsOk
public bool IsBadRequest
=> StatusCode == HttpStatusCode.BadRequest;

public bool IsUnauthorized
=> StatusCode == HttpStatusCode.Unauthorized;

public string? OkContent
=> IsOk && ContentObject is string result
? result
Expand All @@ -37,9 +34,4 @@ public string? BadRequestContent
=> IsBadRequest && ContentObject is string result
? result
: throw new InvalidOperationException("Content is not the expected type - please use the IsBadRequest property first.");

public string? UnauthorizedContent
=> IsUnauthorized && ContentObject is string result
? result
: throw new InvalidOperationException("Content is not the expected type - please use the IsUnauthorized property first.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand Down Expand Up @@ -42,7 +42,6 @@ public async Task<UpdateAccountNameEndpointResult> ExecuteAsync(
var responseBuilder = httpMessageFactory.FromResponse(response);
responseBuilder.AddSuccessResponse<string?>(HttpStatusCode.OK);
responseBuilder.AddErrorResponse<ValidationProblemDetails>(HttpStatusCode.BadRequest);
responseBuilder.AddErrorResponse<string>(HttpStatusCode.Unauthorized);
return await responseBuilder.BuildResponseAsync(x => new UpdateAccountNameEndpointResult(x), cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -25,9 +25,6 @@ public bool IsOk
public bool IsBadRequest
=> StatusCode == HttpStatusCode.BadRequest;

public bool IsUnauthorized
=> StatusCode == HttpStatusCode.Unauthorized;

public string? OkContent
=> IsOk && ContentObject is string result
? result
Expand All @@ -37,9 +34,4 @@ public string? BadRequestContent
=> IsBadRequest && ContentObject is string result
? result
: throw new InvalidOperationException("Content is not the expected type - please use the IsBadRequest property first.");

public string? UnauthorizedContent
=> IsUnauthorized && ContentObject is string result
? result
: throw new InvalidOperationException("Content is not the expected type - please use the IsUnauthorized property first.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand Down Expand Up @@ -41,8 +41,7 @@ public async Task<GetAddressesByPostalCodesEndpointResult> ExecuteAsync(
var responseBuilder = httpMessageFactory.FromResponse(response);
responseBuilder.AddSuccessResponse<IEnumerable<Address>>(HttpStatusCode.OK);
responseBuilder.AddErrorResponse<ValidationProblemDetails>(HttpStatusCode.BadRequest);
responseBuilder.AddErrorResponse<string>(HttpStatusCode.Unauthorized);
responseBuilder.AddErrorResponse<string>(HttpStatusCode.NotFound);
responseBuilder.AddErrorResponse<string?>(HttpStatusCode.NotFound);
return await responseBuilder.BuildResponseAsync(x => new GetAddressesByPostalCodesEndpointResult(x), cancellationToken);
}
}
}
Loading
Loading