Skip to content

Commit fbf11f8

Browse files
Merge pull request #103 from atc-net/feature/102-name-conflicts
Move project usings inside namespace. Closes #102
2 parents 181eccb + 9169230 commit fbf11f8

File tree

6 files changed

+112
-19
lines changed

6 files changed

+112
-19
lines changed

.github/workflows/pre-integration.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
jobs:
1111
dotnet5-build:
1212
strategy:
13+
fail-fast: false
1314
matrix:
1415
os: [ubuntu-latest, macos-latest, windows-latest]
1516
runs-on: ${{ matrix.os }}
@@ -24,8 +25,10 @@ jobs:
2425
with:
2526
dotnet-version: '5.0.x'
2627

27-
- name: 🧹 Clean
28-
run: dotnet clean -c Release && dotnet nuget locals all --clear
28+
- name: 📐 Ensure nuget.org added as package source on Windows
29+
if: matrix.os == 'windows-latest'
30+
run: dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org --configfile $env:APPDATA\NuGet\NuGet.Config
31+
continue-on-error: true
2932

3033
- name: 🔁 Restore packages
3134
run: dotnet restore
@@ -64,6 +67,7 @@ jobs:
6467

6568
smoke-tests:
6669
strategy:
70+
fail-fast: false
6771
matrix:
6872
os: [ubuntu-latest, macos-latest, windows-latest]
6973
runs-on: ${{ matrix.os }}
@@ -85,8 +89,9 @@ jobs:
8589
with:
8690
dotnet-version: '5.0.x'
8791

88-
- name: 🧹 Clean
89-
run: dotnet clean -c Release && dotnet nuget locals all --clear
92+
- name: 📐 Ensure nuget.org added as package source
93+
if: matrix.os == 'windows-latest'
94+
run: dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org --configfile $env:APPDATA\NuGet\NuGet.Config
9095

9196
- name: 🔁 Restore packages
9297
run: dotnet restore

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

+20-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,35 @@
33
using System.Linq;
44
using System.Net;
55
using Atc.Rest.ApiGenerator.Models;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
68
using Microsoft.OpenApi.Models;
79

810
namespace Atc.Rest.ApiGenerator.Factories
911
{
1012
public static class ProjectApiFactory
1113
{
12-
public static string[] CreateUsingListForEndpoint(
14+
public static UsingDirectiveSyntax[] CreateProjectUsingListForEndpoint(
1315
ApiProjectOptions apiProjectOptions,
1416
string focusOnSegmentName,
17+
bool hasSharedResponseContract)
18+
{
19+
var result = new List<UsingDirectiveSyntax>();
20+
21+
if (hasSharedResponseContract)
22+
{
23+
result.Add(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}")));
24+
}
25+
26+
result.Add(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}")));
27+
28+
return result.ToArray();
29+
}
30+
31+
public static string[] CreateGeneralUsingListForEndpoint(
32+
ApiProjectOptions apiProjectOptions,
1533
List<OpenApiOperation> apiOperations,
16-
bool includeRestResults,
17-
bool hasSharedModel)
34+
bool includeRestResults)
1835
{
1936
if (apiOperations == null)
2037
{
@@ -34,13 +51,6 @@ public static string[] CreateUsingListForEndpoint(
3451
list.Add("Atc.Rest.Results");
3552
}
3653

37-
if (hasSharedModel)
38-
{
39-
list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}");
40-
}
41-
42-
list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}");
43-
4454
list.Add("Microsoft.AspNetCore.Http");
4555
list.Add("Microsoft.AspNetCore.Mvc");
4656

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ public bool GenerateCode()
103103
}
104104
}
105105

106+
// Add the class to the namespace.
107+
@namespace = @namespace.AddUsings(
108+
ProjectApiFactory.CreateProjectUsingListForEndpoint(
109+
ApiProjectOptions,
110+
FocusOnSegmentName,
111+
HasSharedResponseContract()));
112+
106113
// Add the class to the namespace.
107114
@namespace = @namespace.AddMembers(classDeclaration);
108115

@@ -112,12 +119,10 @@ public bool GenerateCode()
112119
.Any(x => x.Identifier.ValueText.Contains($"({Microsoft.OpenApi.Models.NameConstants.Pagination}<", StringComparison.Ordinal));
113120

114121
compilationUnit = compilationUnit.AddUsingStatements(
115-
ProjectApiFactory.CreateUsingListForEndpoint(
122+
ProjectApiFactory.CreateGeneralUsingListForEndpoint(
116123
ApiProjectOptions,
117-
FocusOnSegmentName,
118124
usedApiOperations,
119-
includeRestResults,
120-
HasSharedResponseContract()));
125+
includeRestResults));
121126

122127
// Add namespace to compilationUnit
123128
compilationUnit = compilationUnit.AddMembers(@namespace);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.CodeDom.Compiler;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
8+
//------------------------------------------------------------------------------
9+
// This code was auto-generated by ApiGenerator x.x.x.x.
10+
//
11+
// Changes to this file may cause incorrect behavior and will be lost if
12+
// the code is regenerated.
13+
//------------------------------------------------------------------------------
14+
namespace TestProject.AtcTest.Endpoints
15+
{
16+
using TestProject.AtcTest.Contracts.Tasks;
17+
18+
/// <summary>
19+
/// Endpoint definitions.
20+
/// Area: Tasks.
21+
/// </summary>
22+
[ApiController]
23+
[Route("/api/v1/tasks")]
24+
[GeneratedCode("ApiGenerator", "x.x.x.x")]
25+
public class TasksController : ControllerBase
26+
{
27+
/// <summary>
28+
/// Description: Get.
29+
/// Operation: Get.
30+
/// Area: Tasks.
31+
/// </summary>
32+
[HttpGet]
33+
[ProducesResponseType(typeof(Task), StatusCodes.Status200OK)]
34+
public Task<ActionResult> GetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken)
35+
{
36+
if (handler is null)
37+
{
38+
throw new ArgumentNullException(nameof(handler));
39+
}
40+
41+
return InvokeGetAsync(handler, cancellationToken);
42+
}
43+
44+
private static async Task<ActionResult> InvokeGetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken)
45+
{
46+
return await handler.ExecuteAsync(cancellationToken);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
openapi: 3.0.0
2+
paths:
3+
'/tasks':
4+
get:
5+
summary: Get
6+
description: Get
7+
operationId: get
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '#/components/schemas/Task'
15+
components:
16+
schemas:
17+
Task:
18+
title: Task
19+
description: Task.
20+
type: object
21+
properties:
22+
id:
23+
type: string

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/getItem.verified.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Mvc;
7-
using TestProject.AtcTest.Contracts.Items;
87

98
//------------------------------------------------------------------------------
109
// This code was auto-generated by ApiGenerator x.x.x.x.
@@ -14,6 +13,8 @@
1413
//------------------------------------------------------------------------------
1514
namespace TestProject.AtcTest.Endpoints
1615
{
16+
using TestProject.AtcTest.Contracts.Items;
17+
1718
/// <summary>
1819
/// Endpoint definitions.
1920
/// Area: Items.

0 commit comments

Comments
 (0)