Skip to content

Commit 41d0173

Browse files
Merge pull request #100 from atc-net/feature/99-result-abstract
Result factory methods uses IEnumerable instead of List for "array" type responses
2 parents fbf11f8 + d6b9834 commit 41d0173

File tree

10 files changed

+95
-18
lines changed

10 files changed

+95
-18
lines changed

.github/workflows/pre-integration.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ jobs:
8989
with:
9090
dotnet-version: '5.0.x'
9191

92-
- name: 📐 Ensure nuget.org added as package source
92+
- name: 📐 Ensure nuget.org added as package source on Windows
9393
if: matrix.os == 'windows-latest'
9494
run: dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org --configfile $env:APPDATA\NuGet\NuGet.Config
95+
continue-on-error: true
9596

9697
- name: 🔁 Restore packages
9798
run: dotnet restore

docs/CodeDoc/Atc.Rest.ApiGenerator/Atc.Rest.ApiGenerator.Helpers.md

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public static class GenerateAtcCodingRulesHelper
5858
### Static Fields
5959

6060

61+
#### FileNameDirectoryBuildProps
62+
63+
```csharp
64+
string FileNameDirectoryBuildProps
65+
```
6166
#### FileNameEditorConfig
6267

6368
```csharp

docs/CodeDoc/Atc.Rest.ApiGenerator/IndexExtended.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
- HasSharedResponseContract(OpenApiDocument document, List<ApiOperationSchemaMap> operationSchemaMappings, string focusOnSegmentName)
125125
- [GenerateAtcCodingRulesHelper](Atc.Rest.ApiGenerator.Helpers.md#generateatccodingruleshelper)
126126
- Static Fields
127+
- string FileNameDirectoryBuildProps
127128
- string FileNameEditorConfig
128129
- Static Methods
129130
- Generate(string outputSlnPath, DirectoryInfo outputSrcPath, DirectoryInfo outputTestPath)

src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public static string[] CreateUsingListForContractResult(
204204

205205
if (responses.HasSchemaTypeOfArray())
206206
{
207+
list.Add("System.Linq");
207208
list.Add("System.Collections.Generic");
208209
}
209210

src/Atc.Rest.ApiGenerator/NameConstants.cs

+2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ internal static class NameConstants
2828
public const string Handler = "Handler";
2929

3030
public const string ClientRequestParameters = "RequestParameters";
31+
32+
public const string AbstractCollectionTypeName = "IEnumerable";
3133
}
3234
}

src/Atc.Rest.ApiGenerator/SyntaxGenerators/Api/SyntaxGeneratorContractResult.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ private List<MemberDeclarationSyntax> CreateMethods(string className)
245245
{
246246
methodDeclaration = CreateTypeRequestObjectResult(className, httpStatusCode.ToNormalizedString(), schema.GetDataType(), "response", isList, isPagination);
247247
}
248+
else if (schema != null && string.Equals(schema.Type, OpenApiDataTypeConstants.Array, StringComparison.Ordinal))
249+
{
250+
methodDeclaration = CreateTypeRequestObjectResult(
251+
className,
252+
httpStatusCode.ToNormalizedString(),
253+
schema.Items.GetDataType(),
254+
"response",
255+
isList,
256+
isPagination);
257+
}
248258
else
249259
{
250260
methodDeclaration = CreateTypeRequestWithMessageAllowNull(className, httpStatusCode, nameof(OkObjectResult));
@@ -558,9 +568,13 @@ private static MethodDeclarationSyntax CreateTypeRequestObjectResult(
558568
bool asGenericPagination = false)
559569
{
560570
string? genericListTypeName = null;
571+
string objectResultParameter = asGenericList
572+
? parameterName + $" ?? Enumerable.Empty<{parameterTypeName}>()"
573+
: parameterName;
574+
561575
if (asGenericList)
562576
{
563-
genericListTypeName = Microsoft.OpenApi.Models.NameConstants.List;
577+
genericListTypeName = NameConstants.AbstractCollectionTypeName;
564578
}
565579
else if (asGenericPagination)
566580
{
@@ -585,7 +599,7 @@ private static MethodDeclarationSyntax CreateTypeRequestObjectResult(
585599
SyntaxFactory.SingletonSeparatedList(
586600
SyntaxFactory.Argument(
587601
SyntaxObjectCreationExpressionFactory.Create(methodName + nameof(ObjectResult))
588-
.WithArgumentList(SyntaxArgumentListFactory.CreateWithOneItem(parameterName))))))))
602+
.WithArgumentList(SyntaxArgumentListFactory.CreateWithOneItem(objectResultParameter))))))))
589603
.WithSemicolonToken(SyntaxTokenFactory.Semicolon());
590604
}
591605

@@ -669,10 +683,10 @@ private static ConversionOperatorDeclarationSyntax CreateImplicitOperator(
669683
SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(className)))
670684
.WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword(true))
671685
.WithOperatorKeyword(SyntaxTokenFactory.OperatorKeyword())
672-
.AddParameterListParameters(SyntaxParameterFactory.Create(typeName, "x", genericListTypeName))
686+
.AddParameterListParameters(SyntaxParameterFactory.Create(typeName, "response", genericListTypeName))
673687
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
674688
SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(httpStatus)))
675-
.AddArgumentListArguments(SyntaxArgumentFactory.Create("x")))
689+
.AddArgumentListArguments(SyntaxArgumentFactory.Create("response")))
676690
.WithArrowToken(SyntaxTokenFactory.EqualsGreaterThan()))
677691
.WithSemicolonToken(SyntaxTokenFactory.Semicolon());
678692
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.CodeDom.Compiler;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Atc.Rest.Results;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
//------------------------------------------------------------------------------
8+
// This code was auto-generated by ApiGenerator x.x.x.x.
9+
//
10+
// Changes to this file may cause incorrect behavior and will be lost if
11+
// the code is regenerated.
12+
//------------------------------------------------------------------------------
13+
namespace TestProject.AtcTest.Contracts.Test
14+
{
15+
/// <summary>
16+
/// Results for operation request.
17+
/// Description: Get.
18+
/// Operation: Get.
19+
/// Area: Test.
20+
/// </summary>
21+
[GeneratedCode("ApiGenerator", "x.x.x.x")]
22+
public class GetResult : ResultBase
23+
{
24+
private GetResult(ActionResult result) : base(result) { }
25+
26+
/// <summary>
27+
/// 200 - Ok response.
28+
/// </summary>
29+
public static GetResult Ok(IEnumerable<string> response) => new GetResult(new OkObjectResult(response ?? Enumerable.Empty<string>()));
30+
31+
/// <summary>
32+
/// Performs an implicit conversion from GetResult to ActionResult.
33+
/// </summary>
34+
public static implicit operator GetResult(List<string> response) => Ok(response);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
openapi: 3.0.0
2+
paths:
3+
/:
4+
get:
5+
summary: Get
6+
description: Get
7+
operationId: get
8+
tags: []
9+
responses:
10+
'200':
11+
description: OK
12+
content:
13+
application/json:
14+
schema:
15+
type: array
16+
items:
17+
type: string

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/ContractResult/getOkResponse.verified.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ namespace TestProject.AtcTest.Contracts.Test
1212
{
1313
/// <summary>
1414
/// Results for operation request.
15-
/// Description: Get all users.
16-
/// Operation: GetUsers.
15+
/// Description: Get.
16+
/// Operation: Get.
1717
/// Area: Test.
1818
/// </summary>
1919
[GeneratedCode("ApiGenerator", "x.x.x.x")]
20-
public class GetUsersResult : ResultBase
20+
public class GetResult : ResultBase
2121
{
22-
private GetUsersResult(ActionResult result) : base(result) { }
22+
private GetResult(ActionResult result) : base(result) { }
2323

2424
/// <summary>
2525
/// 200 - Ok response.
2626
/// </summary>
27-
public static GetUsersResult Ok(string? message = null) => new GetUsersResult(new OkObjectResult(message));
27+
public static GetResult Ok(string? message = null) => new GetResult(new OkObjectResult(message));
2828

2929
/// <summary>
30-
/// Performs an implicit conversion from GetUsersResult to ActionResult.
30+
/// Performs an implicit conversion from GetResult to ActionResult.
3131
/// </summary>
32-
public static implicit operator GetUsersResult(string x) => Ok(x);
32+
public static implicit operator GetResult(string response) => Ok(response);
3333
}
3434
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
openapi: 3.0.0
22
paths:
3-
/users:
3+
/:
44
get:
5-
summary: Get all users
6-
description: Get all users
7-
operationId: getUsers
5+
summary: Get
6+
description: Get
7+
operationId: get
88
tags: []
99
responses:
1010
'200':
1111
description: OK
1212
content:
13-
application/json:
13+
text/plain:
1414
schema:
15-
$ref: '#/components/schemas/Users'
15+
type: string

0 commit comments

Comments
 (0)