Skip to content

Commit

Permalink
Merge pull request #192 from atc-net/feature/minor-bugfixes
Browse files Browse the repository at this point in the history
Feature/minor bugfixes
  • Loading branch information
davidkallesen authored Jun 27, 2024
2 parents 941ffa6 + 4b9b04a commit 8e76ed9
Show file tree
Hide file tree
Showing 37 changed files with 207 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,28 @@ private void AppendMethodContentStatusCodeOk(
return;
}

var dataType = responseModel.GetQualifiedDataType();

if (responseModel.CollectionDataType is null)
{
sb.AppendLine(4, $"public {responseModel.DataType} OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is {responseModel.DataType} result");
sb.AppendLine(4, $"public {dataType} OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is {dataType} result");
sb.AppendLine(12, "? result");
sb.AppendLine(12, ": throw new InvalidOperationException(\"Content is not the expected type - please use the IsOk property first.\");");
return;
}

if (responseModel.CollectionDataType == NameConstants.List)
{
sb.AppendLine(4, $"public IEnumerable<{responseModel.DataType}> OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is IEnumerable<{responseModel.DataType}> result");
sb.AppendLine(4, $"public IEnumerable<{dataType}> OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is IEnumerable<{dataType}> result");
sb.AppendLine(12, "? result");
sb.AppendLine(12, ": throw new InvalidOperationException(\"Content is not the expected type - please use the IsOk property first.\");");
}
else
{
sb.AppendLine(4, $"public {responseModel.CollectionDataType}<{responseModel.DataType}> OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is {responseModel.CollectionDataType}<{responseModel.DataType}> result");
sb.AppendLine(4, $"public {responseModel.CollectionDataType}<{dataType}> OkContent");
sb.AppendLine(8, $"=> IsOk && ContentObject is {responseModel.CollectionDataType}<{dataType}> result");
sb.AppendLine(12, "? result");
sb.AppendLine(12, ": throw new InvalidOperationException(\"Content is not the expected type - please use the IsOk property first.\");");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,21 @@ private void AppendMethodContentStatusCodeOk(
return;
}

var dataType = responseModel.GetQualifiedDataType();

if (responseModel.CollectionDataType is null)
{
sb.AppendLine(4, $"{responseModel.DataType} OkContent {{ get; }}");
sb.AppendLine(4, $"{dataType} OkContent {{ get; }}");
return;
}

if (responseModel.CollectionDataType == NameConstants.List)
{
sb.AppendLine(4, $"IEnumerable<{responseModel.DataType}> OkContent {{ get; }}");
sb.AppendLine(4, $"IEnumerable<{dataType}> OkContent {{ get; }}");
}
else
{
sb.AppendLine(4, $"{responseModel.CollectionDataType}<{responseModel.DataType}> OkContent {{ get; }}");
sb.AppendLine(4, $"{responseModel.CollectionDataType}<{dataType}> OkContent {{ get; }}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ public void MaintainGlobalUsings(
requiredUsings.Add("Atc.Rest.Results");
}

if (operationSchemaMappings.Any(apiOperation => apiOperation.Model.IsEnum ||
apiOperation.Model.IsShared))
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 @@ -2,6 +2,26 @@ namespace Atc.Rest.ApiGenerator.Contracts.Extensions;

public static class ApiOperationResponseModelExtensions
{
public static string? GetQualifiedDataType(
this ApiOperationResponseModel responseModel)
{
ArgumentNullException.ThrowIfNull(responseModel);

if (string.IsNullOrEmpty(responseModel.DataType))
{
return null;
}

var dataType = responseModel.DataType;
if (responseModel.Namespace is not null &&
responseModel.Namespace.EndsWith(responseModel.DataType, StringComparison.Ordinal))
{
dataType = $"{responseModel.Namespace}.{dataType}";
}

return dataType;
}

public static IEnumerable<ApiOperationResponseModel> AppendUnauthorizedIfNeeded(
this IEnumerable<ApiOperationResponseModel> responseModels,
ApiAuthorizeModel? authorization)
Expand All @@ -24,7 +44,8 @@ public static IEnumerable<ApiOperationResponseModel> AppendUnauthorizedIfNeeded(
MediaType: null,
CollectionDataType: null,
DataType: null,
Description: null));
Description: null,
Namespace: null));
}

return models;
Expand Down Expand Up @@ -56,7 +77,8 @@ authorization is
MediaType: null,
CollectionDataType: null,
DataType: null,
Description: null));
Description: null,
Namespace: null));
}

return models;
Expand All @@ -79,7 +101,8 @@ public static IEnumerable<ApiOperationResponseModel> AppendBadRequestIfNeeded(
MediaType: null,
CollectionDataType: null,
DataType: null,
Description: null));
Description: null,
Namespace: null));
}

return models;
Expand All @@ -102,7 +125,8 @@ public static IEnumerable<ApiOperationResponseModel> AppendBadRequestIfNeeded(
MediaType: null,
CollectionDataType: null,
DataType: null,
Description: null));
Description: null,
Namespace: null));
}

return models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public record ApiOperationResponseModel(
string? MediaType,
string? CollectionDataType,
string? DataType,
string? Description);
string? Description,
string? Namespace);
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,19 @@ private static void AppendProducesForOk(
}
else
{
var dataType = responseModel.GetQualifiedDataType();

if (string.IsNullOrEmpty(responseModel.CollectionDataType))
{
sb.Append(12, $".Produces<{responseModel.DataType}>()");
sb.Append(12, $".Produces<{dataType}>()");
}
else
{
sb.AppendLine(
4,
responseModel.CollectionDataType == "List"
? $".Produces<IEnumerable<{responseModel.DataType}>>()"
: $".Produces<{responseModel.CollectionDataType}<{responseModel.DataType}>>()");
? $".Produces<IEnumerable<{dataType}>>()"
: $".Produces<{responseModel.CollectionDataType}<{dataType}>>()");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,12 @@ public void MaintainGlobalUsings(
// TODO: Check for any use ??
requiredUsings.Add("Microsoft.AspNetCore.Authorization");

var apiGroupNames = openApiDocument.GetApiGroupNames();

// TODO: Check for any use ??
requiredUsings.Add($"{projectName}.{ContentGeneratorConstants.Contracts}");
if (operationSchemaMappings.Any(apiOperation => apiOperation.Model.IsShared))
{
requiredUsings.Add($"{projectName}.{ContentGeneratorConstants.Contracts}");
}

var apiGroupNames = openApiDocument.GetApiGroupNames();
requiredUsings.AddRange(apiGroupNames.Select(x => $"{projectName}.{ContentGeneratorConstants.Contracts}.{x}"));

GlobalUsingsHelper.CreateOrUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public ServerGenerator(
ArgumentNullException.ThrowIfNull(projectName);
ArgumentNullException.ThrowIfNull(rootPath);
ArgumentNullException.ThrowIfNull(srcPath);
ArgumentNullException.ThrowIfNull(testPath);

logger = loggerFactory.CreateLogger<ServerGenerator>();
this.projectName = projectName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,19 @@ private static void AppendProducesForOk(
}
else
{
var dataType = responseModel.GetQualifiedDataType();

if (string.IsNullOrEmpty(responseModel.CollectionDataType))
{
sb.AppendLine(4, $"[ProducesResponseType(typeof({responseModel.DataType}), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
sb.AppendLine(4, $"[ProducesResponseType(typeof({dataType}), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
}
else
{
sb.AppendLine(
4,
responseModel.CollectionDataType == "List"
? $"[ProducesResponseType(typeof(IEnumerable<{responseModel.DataType}>), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]"
: $"[ProducesResponseType(typeof({responseModel.CollectionDataType}<{responseModel.DataType}>), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
? $"[ProducesResponseType(typeof(IEnumerable<{dataType}>), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]"
: $"[ProducesResponseType(typeof({responseModel.CollectionDataType}<{dataType}>), StatusCodes.{responseModel.StatusCode.ToStatusCodesConstant()})]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ContentGeneratorServerResultParameters Create(

// Methods
var methodParameters = new List<ContentGeneratorServerResultMethodParameters>();
var responseModels = openApiOperation.ExtractApiOperationResponseModels().ToList();
var responseModels = openApiOperation.ExtractApiOperationResponseModels(@namespace).ToList();

foreach (var responseModel in responseModels.OrderBy(x => x.StatusCode))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public ServerGenerator(
ArgumentNullException.ThrowIfNull(projectName);
ArgumentNullException.ThrowIfNull(rootPath);
ArgumentNullException.ThrowIfNull(srcPath);
ArgumentNullException.ThrowIfNull(testPath);

logger = loggerFactory.CreateLogger<ServerGenerator>();
this.projectName = projectName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public static ContentGeneratorClientEndpointResultInterfaceParameters Create(
AppendParameters(parameters, openApiOperation.Parameters);
AppendParametersFromBody(parameters, openApiOperation.RequestBody);

var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var operationName = openApiOperation.GetOperationName();
var endpointAuthorization = openApiOperation.ExtractApiOperationAuthorization(openApiPath);
var responseModels = openApiOperation.ExtractApiOperationResponseModels().ToList();
var responseModels = openApiOperation.ExtractApiOperationResponseModels(modelNamespace).ToList();
var hasParameterType = openApiPath.HasParameters() || openApiOperation.HasParametersOrRequestBody();

return new ContentGeneratorClientEndpointResultInterfaceParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public static ContentGeneratorClientEndpointResultParameters Create(
AppendParameters(parameters, openApiOperation.Parameters);
AppendParametersFromBody(parameters, openApiOperation.RequestBody);

var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var operationName = openApiOperation.GetOperationName();
var endpointAuthorization = openApiOperation.ExtractApiOperationAuthorization(openApiPath);
var responseModels = openApiOperation.ExtractApiOperationResponseModels().ToList();
var responseModels = openApiOperation.ExtractApiOperationResponseModels(modelNamespace).ToList();
var hasParameterType = openApiPath.HasParameters() || openApiOperation.HasParametersOrRequestBody();

if (hasParameterType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static ContentGeneratorServerEndpointParameters Create(
string endpointSuffixName,
OpenApiDocument openApiDocument)
{
var modelNamespace = $"{projectName}.{ContentGeneratorConstants.Contracts}.{apiGroupName}";
var methodParameters = new List<ContentGeneratorServerEndpointMethodParameters>();

ApiAuthorizeModel? controllerAuthorization = null;
Expand All @@ -23,7 +24,7 @@ public static ContentGeneratorServerEndpointParameters Create(
var operationName = apiOperation.Value.GetOperationName();
var endpointAuthorization = apiOperation.Value.ExtractApiOperationAuthorization(apiPathData);
var responseModels = apiOperation.Value
.ExtractApiOperationResponseModels()
.ExtractApiOperationResponseModels(modelNamespace)
.AdjustNamespacesIfNeeded(operationSchemaMappings);

methodParameters.Add(new ContentGeneratorServerEndpointMethodParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public async Task<Version> GetAtcApiGeneratorVersion()
("Asp.Versioning.Http", PackageDefaultVersions["Asp.Versioning.Http"]),
("Atc", atcVersion),
("Atc.Rest.Extended", atcVersion),
("Atc.Rest.MinimalApi", "1.0.81"),
("Microsoft.NETCore.Platforms", PackageDefaultVersions["Microsoft.NETCore.Platforms"]),
("Swashbuckle.AspNetCore", PackageDefaultVersions["Swashbuckle.AspNetCore"]),
};
Expand Down
Loading

0 comments on commit 8e76ed9

Please sign in to comment.