diff --git a/.github/workflows/pre-integration.yml b/.github/workflows/pre-integration.yml index 7c2ceb8e6..aa57f46e7 100644 --- a/.github/workflows/pre-integration.yml +++ b/.github/workflows/pre-integration.yml @@ -10,6 +10,7 @@ on: jobs: dotnet5-build: strategy: + fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} @@ -24,8 +25,10 @@ jobs: with: dotnet-version: '5.0.x' - - name: ๐Ÿงน Clean - run: dotnet clean -c Release && dotnet nuget locals all --clear + - name: ๐Ÿ“ Ensure nuget.org added as package source on Windows + if: matrix.os == 'windows-latest' + run: dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org --configfile $env:APPDATA\NuGet\NuGet.Config + continue-on-error: true - name: ๐Ÿ” Restore packages run: dotnet restore @@ -64,6 +67,7 @@ jobs: smoke-tests: strategy: + fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} @@ -85,8 +89,9 @@ jobs: with: dotnet-version: '5.0.x' - - name: ๐Ÿงน Clean - run: dotnet clean -c Release && dotnet nuget locals all --clear + - name: ๐Ÿ“ Ensure nuget.org added as package source + if: matrix.os == 'windows-latest' + run: dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org --configfile $env:APPDATA\NuGet\NuGet.Config - name: ๐Ÿ” Restore packages run: dotnet restore diff --git a/src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs b/src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs index 915c08016..9868c758f 100644 --- a/src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs +++ b/src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs @@ -3,18 +3,35 @@ using System.Linq; using System.Net; using Atc.Rest.ApiGenerator.Models; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.OpenApi.Models; namespace Atc.Rest.ApiGenerator.Factories { public static class ProjectApiFactory { - public static string[] CreateUsingListForEndpoint( + public static UsingDirectiveSyntax[] CreateProjectUsingListForEndpoint( ApiProjectOptions apiProjectOptions, string focusOnSegmentName, + bool hasSharedResponseContract) + { + var result = new List(); + + if (hasSharedResponseContract) + { + result.Add(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}"))); + } + + result.Add(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}"))); + + return result.ToArray(); + } + + public static string[] CreateGeneralUsingListForEndpoint( + ApiProjectOptions apiProjectOptions, List apiOperations, - bool includeRestResults, - bool hasSharedModel) + bool includeRestResults) { if (apiOperations == null) { @@ -34,13 +51,6 @@ public static string[] CreateUsingListForEndpoint( list.Add("Atc.Rest.Results"); } - if (hasSharedModel) - { - list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}"); - } - - list.Add($"{apiProjectOptions.ProjectName}.{NameConstants.Contracts}.{focusOnSegmentName.EnsureFirstCharacterToUpper()}"); - list.Add("Microsoft.AspNetCore.Http"); list.Add("Microsoft.AspNetCore.Mvc"); diff --git a/src/Atc.Rest.ApiGenerator/SyntaxGenerators/Api/SyntaxGeneratorEndpointControllers.cs b/src/Atc.Rest.ApiGenerator/SyntaxGenerators/Api/SyntaxGeneratorEndpointControllers.cs index 4a0cd63fd..2811aea4c 100644 --- a/src/Atc.Rest.ApiGenerator/SyntaxGenerators/Api/SyntaxGeneratorEndpointControllers.cs +++ b/src/Atc.Rest.ApiGenerator/SyntaxGenerators/Api/SyntaxGeneratorEndpointControllers.cs @@ -103,6 +103,13 @@ public bool GenerateCode() } } + // Add the class to the namespace. + @namespace = @namespace.AddUsings( + ProjectApiFactory.CreateProjectUsingListForEndpoint( + ApiProjectOptions, + FocusOnSegmentName, + HasSharedResponseContract())); + // Add the class to the namespace. @namespace = @namespace.AddMembers(classDeclaration); @@ -112,12 +119,10 @@ public bool GenerateCode() .Any(x => x.Identifier.ValueText.Contains($"({Microsoft.OpenApi.Models.NameConstants.Pagination}<", StringComparison.Ordinal)); compilationUnit = compilationUnit.AddUsingStatements( - ProjectApiFactory.CreateUsingListForEndpoint( + ProjectApiFactory.CreateGeneralUsingListForEndpoint( ApiProjectOptions, - FocusOnSegmentName, usedApiOperations, - includeRestResults, - HasSharedResponseContract())); + includeRestResults)); // Add namespace to compilationUnit compilationUnit = compilationUnit.AddMembers(@namespace); diff --git a/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.verified.cs b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.verified.cs new file mode 100644 index 000000000..bcda5aad5 --- /dev/null +++ b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.verified.cs @@ -0,0 +1,49 @@ +using System; +using System.CodeDom.Compiler; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +//------------------------------------------------------------------------------ +// This code was auto-generated by ApiGenerator x.x.x.x. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +//------------------------------------------------------------------------------ +namespace TestProject.AtcTest.Endpoints +{ + using TestProject.AtcTest.Contracts.Tasks; + + /// + /// Endpoint definitions. + /// Area: Tasks. + /// + [ApiController] + [Route("/api/v1/tasks")] + [GeneratedCode("ApiGenerator", "x.x.x.x")] + public class TasksController : ControllerBase + { + /// + /// Description: Get. + /// Operation: Get. + /// Area: Tasks. + /// + [HttpGet] + [ProducesResponseType(typeof(Task), StatusCodes.Status200OK)] + public Task GetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken) + { + if (handler is null) + { + throw new ArgumentNullException(nameof(handler)); + } + + return InvokeGetAsync(handler, cancellationToken); + } + + private static async Task InvokeGetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken) + { + return await handler.ExecuteAsync(cancellationToken); + } + } +} \ No newline at end of file diff --git a/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.yaml b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.yaml new file mode 100644 index 000000000..b60c95fb3 --- /dev/null +++ b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/endpointWithContractWithConflictingName.yaml @@ -0,0 +1,23 @@ +openapi: 3.0.0 +paths: + '/tasks': + get: + summary: Get + description: Get + operationId: get + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Task' +components: + schemas: + Task: + title: Task + description: Task. + type: object + properties: + id: + type: string \ No newline at end of file diff --git a/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/getItem.verified.cs b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/getItem.verified.cs index 9a3d2f498..f43791597 100644 --- a/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/getItem.verified.cs +++ b/test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/EndpointControllers/getItem.verified.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using TestProject.AtcTest.Contracts.Items; //------------------------------------------------------------------------------ // This code was auto-generated by ApiGenerator x.x.x.x. @@ -14,6 +13,8 @@ //------------------------------------------------------------------------------ namespace TestProject.AtcTest.Endpoints { + using TestProject.AtcTest.Contracts.Items; + /// /// Endpoint definitions. /// Area: Items.