Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .github/workflows/pre-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
jobs:
dotnet5-build:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
Expand All @@ -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
Expand Down Expand Up @@ -64,6 +67,7 @@ jobs:

smoke-tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
Expand All @@ -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
Expand Down
30 changes: 20 additions & 10 deletions src/Atc.Rest.ApiGenerator/Factories/ProjectApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UsingDirectiveSyntax>();

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<OpenApiOperation> apiOperations,
bool includeRestResults,
bool hasSharedModel)
bool includeRestResults)
{
if (apiOperations == null)
{
Expand All @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Endpoint definitions.
/// Area: Tasks.
/// </summary>
[ApiController]
[Route("/api/v1/tasks")]
[GeneratedCode("ApiGenerator", "x.x.x.x")]
public class TasksController : ControllerBase
{
/// <summary>
/// Description: Get.
/// Operation: Get.
/// Area: Tasks.
/// </summary>
[HttpGet]
[ProducesResponseType(typeof(Task), StatusCodes.Status200OK)]
public Task<ActionResult> GetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken)
{
if (handler is null)
{
throw new ArgumentNullException(nameof(handler));
}

return InvokeGetAsync(handler, cancellationToken);
}

private static async Task<ActionResult> InvokeGetAsync([FromServices] IGetHandler handler, CancellationToken cancellationToken)
{
return await handler.ExecuteAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,6 +13,8 @@
//------------------------------------------------------------------------------
namespace TestProject.AtcTest.Endpoints
{
using TestProject.AtcTest.Contracts.Items;

/// <summary>
/// Endpoint definitions.
/// Area: Items.
Expand Down