Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking if endpoints class is static, if not, creating one. #2002

Merged
merged 1 commit into from Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs
Expand Up @@ -154,21 +154,23 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
//Get class syntax node to add members to the class
var docRoot = docEditor.OriginalRoot as CompilationUnitSyntax;
//create CodeFile just to add usings

var usings = new List<string>();
//add usings for DbContext related actins.
if (!string.IsNullOrEmpty(templateModel.DbContextNamespace))
{
usings.Add(Constants.MicrosoftEntityFrameworkCorePackageName);
usings.Add(templateModel.DbContextNamespace);
}

if (!string.IsNullOrEmpty(templateModel.ContextTypeName))
{
usings.Add(Constants.MicrosoftEntityFrameworkCorePackageName);
}

if (templateModel.OpenAPI)
{
usings.Add("Microsoft.AspNetCore.Http.HttpResults");
usings.Add("Microsoft.AspNetCore.OpenApi");
}

var endpointsCodeFile = new CodeFile { Usings = usings.ToArray()};
var docBuilder = new DocumentBuilder(docEditor, endpointsCodeFile, ConsoleLogger);
var newRoot = docBuilder.AddUsings(new CodeChangeOptions());
Expand All @@ -179,11 +181,31 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints

if (classNode != null && classNode is ClassDeclarationSyntax classDeclaration)
{
SyntaxNode classParentSyntax = null;
//if class is not static, create a new class in the same file
if (!classDeclaration.Modifiers.Any(x => x.Text.Equals(SyntaxFactory.Token(SyntaxKind.StaticKeyword).Text)))
zahalzel marked this conversation as resolved.
Show resolved Hide resolved
{
classParentSyntax = classDeclaration.Parent;
classDeclaration = SyntaxFactory.ClassDeclaration($"{templateModel.ModelType.Name}Endpoints")
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
.NormalizeWhitespace()
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
}
var modifiedClass = classDeclaration.AddMembers(
SyntaxFactory.GlobalStatement(
SyntaxFactory.ParseStatement(membersBlockText))
.WithLeadingTrivia(SyntaxFactory.Tab));
newRoot = newRoot.ReplaceNode(classNode, modifiedClass);
SyntaxFactory.GlobalStatement(SyntaxFactory.ParseStatement(membersBlockText)).WithLeadingTrivia(SyntaxFactory.Tab));

//modify class parent by adding class, classParentSyntax should be null if given class is static.
if (classParentSyntax != null)
{
classParentSyntax = classParentSyntax.InsertNodesAfter(classNode.Parent.ChildNodes().Last(), new List<SyntaxNode>() { modifiedClass });
newRoot = newRoot.ReplaceNode(classNode.Parent, classParentSyntax);
}
//modify given class
else
{
newRoot = newRoot.ReplaceNode(classNode, modifiedClass);
}

docEditor.ReplaceNode(docRoot, newRoot);
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
var classFileTxt = classFileSourceTxt?.ToString();
Expand Down
27 changes: 24 additions & 3 deletions test/Scaffolding/Shared/MSBuildProjectStrings.cs
Expand Up @@ -311,7 +311,7 @@ public static void Main(string[] args)
.WithName(""DeleteCar"");
}
}";
public const string EndpointsEmptyClass = @"namespace MinimalApiTest { class Endpoints { } } ";
public const string EndpointsEmptyClass = @"namespace MinimalApiTest { static class Endpoints { } } ";
public const string MinimalProgramcsFile = @"var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand Down Expand Up @@ -417,8 +417,29 @@ public class Manufacturer
}
}";

// Strings for 3 layered project
public const string WebProjectTxt = @"
public const string CarWithoutNamespaceTxt = @"
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public class Car
{
public string ID { get; set; }
public string Name { get; set; }
public int ManufacturerID { get; set; }
public Manufacturer Manufacturer { get; set; }
[DataType(DataType.MultilineText)]
public string Notes { get; set; }
}

public class Manufacturer
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}";

// Strings for 3 layered project
public const string WebProjectTxt = @"
<Project Sdk=""Microsoft.NET.Sdk.Web"">
<Import Project=""$(MSBuildThisFileDirectory)\TestCodeGeneration.targets"" Condition=""Exists('$(MSBuildThisFileDirectory)\TestCodeGeneration.targets')"" />

Expand Down