Skip to content

Commit

Permalink
minimal api changes (#3067)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery authored Nov 5, 2024
1 parent 6649e87 commit 0f6872d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ private void ValidateOpenApiDependencies(IEnumerable<DependencyDescription> pack
{
var openApiDependencies = new HashSet<string>()
{
"Swashbuckle.AspNetCore",
"Microsoft.AspNetCore.OpenApi"
};

Expand Down
14 changes: 3 additions & 11 deletions src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/minimalApiChanges.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,16 @@
"CodeChanges": [
{
"InsertAfter": "WebApplication.CreateBuilder.Services.AddControllers();",
"InsertBefore": [ "WebApplication.CreateBuilder.Services.AddSwaggerGen();", "var app = builder.Build();" ],
"Options": [ "OpenApi" ],
"Block": "WebApplication.CreateBuilder.Services.AddEndpointsApiExplorer()",
"LeadingTrivia": {
"Newline": true
}
},
{
"InsertAfter": "WebApplication.CreateBuilder.Services.AddEndpointsApiExplorer();",
"InsertBefore": [ "var app = builder.Build();" ],
"Options": [ "OpenApi" ],
"Block": "WebApplication.CreateBuilder.Services.AddSwaggerGen()",
"Block": "WebApplication.CreateBuilder.Services.AddOpenApi()",
"LeadingTrivia": {
"Newline": true
}
},
{
"Block": "if (app.Environment.IsDevelopment())\r\n{\r\n app.UseSwagger();\r\n app.UseSwaggerUI();\r\n}",
"CheckBlock": "if (!app.Environment.IsDevelopment())",
"Block": "if (app.Environment.IsDevelopment())\r\n{\r\n app.MapOpenApi();\r\n}",
"Options": [ "OpenApi" ],
"InsertAfter": "var app = WebApplication.CreateBuilder.Build();",
"InsertBefore": [ "app.UseHttpsRedirection();" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ public static class @endPointsClassName
})
@{
string builderExtensions = $".WithName(\"{@getAllModels}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelList}>(StatusCodes.Status200OK)";
}

@:@builderExtensions;
}

Expand All @@ -89,15 +80,6 @@ public static class @endPointsClassName
})
@{
builderExtensions = $".WithName(\"{@getModelById}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}
@:@builderExtensions;
}

Expand Down Expand Up @@ -139,16 +121,6 @@ public static class @endPointsClassName
}
@{
builderExtensions = $".WithName(\"{@updateModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status204NoContent)";
}

@:@builderExtensions;
}

Expand All @@ -160,14 +132,6 @@ public static class @endPointsClassName
})
@{
builderExtensions = $".WithName(\"{@createModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status201Created)";
}
@:@builderExtensions;
}

Expand Down Expand Up @@ -198,15 +162,6 @@ public static class @endPointsClassName
}
@{
builderExtensions = $".WithName(\"{@deleteModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}
@:@builderExtensions;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@
})
@{
string builderExtensions = $".WithName(\"{@getAllModels}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelList}>(StatusCodes.Status200OK)";
}

@:@builderExtensions;
}
Expand All @@ -78,15 +70,6 @@
})
@{
builderExtensions = $".WithName(\"{@getModelById}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}
@:@builderExtensions;
}

Expand Down Expand Up @@ -128,16 +111,6 @@
}
@{
builderExtensions = $".WithName(\"{@updateModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status204NoContent)";
}

@:@builderExtensions;
}

Expand All @@ -149,14 +122,6 @@
})
@{
builderExtensions = $".WithName(\"{@createModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status201Created)";
}
@:@builderExtensions;
}

Expand Down Expand Up @@ -187,15 +152,6 @@
}
@{
builderExtensions = $".WithName(\"{@deleteModel}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{@modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}
@:@builderExtensions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@
]
}
}
},
{
"FileName": "Program.cs",
"Usings": [],
"Options": [
"OpenApi"
],
"Methods": {
"Global": {
"CodeChanges": [
{
"InsertAfter": "WebApplication.CreateBuilder.Services.AddControllers();",
"InsertBefore": [ "var app = builder.Build();" ],
"Block": "WebApplication.CreateBuilder.Services.AddOpenApi()",
"LeadingTrivia": {
"Newline": true
}
},
{
"CheckBlock": "if (!app.Environment.IsDevelopment())",
"Block": "if (app.Environment.IsDevelopment())\r\n{\r\n app.MapOpenApi();\r\n}",
"Options": [ "OpenApi" ],
"InsertAfter": "var app = WebApplication.CreateBuilder.Build();",
"InsertBefore": [ "app.UseHttpsRedirection();" ],
"LeadingTrivia": {
"Newline": true
}
}
]
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ public override async Task<bool> ExecuteAsync(ScaffolderContext context, Cancell
{
scaffoldingModel.ProjectInfo.CodeChangeOptions =
[
scaffoldingModel.DbContextInfo.EfScenario ? "EfScenario" : string.Empty
scaffoldingModel.DbContextInfo.EfScenario ? "EfScenario" : string.Empty,
settings.OpenApi ? "OpenApi" : string.Empty
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ public virtual string TransformText()
this.Write(";\r\n })");

string builderExtensions = $".WithName(\"{getAllModels}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{modelList}>(StatusCodes.Status200OK)";
}

this.Write("\r\n ");
this.Write(this.ToStringHelper.ToStringWithCulture(builderExtensions));
Expand All @@ -130,15 +122,6 @@ public virtual string TransformText()
this.Write(";\r\n })");

builderExtensions = $".WithName(\"{getModelById}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{Model.ModelInfo.ModelTypeName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n .Produces(StatusCodes.Status404NotFound)";
}

this.Write("\r\n ");
this.Write(this.ToStringHelper.ToStringWithCulture(builderExtensions));
Expand Down Expand Up @@ -214,15 +197,6 @@ public virtual string TransformText()
}

builderExtensions = $".WithName(\"{updateModel}\")";
if (Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status204NoContent)";
}


this.Write(" ");
Expand All @@ -240,14 +214,6 @@ public virtual string TransformText()
this.Write(";\r\n })\r\n ");

builderExtensions = $".WithName(\"{createModel}\")";
if(Model.OpenAPI)
{
builderExtensions+= $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{Model.ModelInfo.ModelTypeName}>(StatusCodes.Status201Created)";
}

this.Write(this.ToStringHelper.ToStringWithCulture(builderExtensions));
this.Write(";\r\n\r\n ");
Expand Down Expand Up @@ -304,15 +270,6 @@ public virtual string TransformText()
}

builderExtensions = $".WithName(\"{deleteModel}\")";
if (Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}

this.Write(this.ToStringHelper.ToStringWithCulture(builderExtensions));
this.Write(";\r\n }\r\n}\r\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ public static class <#= Model.EndpointsClassName #>
return await db.<#= modelToList #>;
})<#
string builderExtensions = $".WithName(\"{getAllModels}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{modelList}>(StatusCodes.Status200OK)";
}
#>

<#= builderExtensions #>;
Expand All @@ -90,15 +82,6 @@ public static class <#= Model.EndpointsClassName #>
: <#= resultsNotFound #>;
})<#
builderExtensions = $".WithName(\"{getModelById}\")";
if(Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if(!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{Model.ModelInfo.ModelTypeName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n .Produces(StatusCodes.Status404NotFound)";
}
#>

<#= builderExtensions #>;
Expand Down Expand Up @@ -149,15 +132,6 @@ public static class <#= Model.EndpointsClassName #>
}

builderExtensions = $".WithName(\"{updateModel}\")";
if (Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status204NoContent)";
}

#>
<#= builderExtensions #>;
Expand All @@ -170,14 +144,6 @@ public static class <#= Model.EndpointsClassName #>
})
<#
builderExtensions = $".WithName(\"{createModel}\")";
if(Model.OpenAPI)
{
builderExtensions+= $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n .Produces<{Model.ModelInfo.ModelTypeName}>(StatusCodes.Status201Created)";
}
#>
<#= builderExtensions #>;

Expand Down Expand Up @@ -213,15 +179,6 @@ group.MapDelete("/{id}", async <#= typedTaskOkNotFound #> (<#= primaryKeyShortTy
}

builderExtensions = $".WithName(\"{deleteModel}\")";
if (Model.OpenAPI)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.WithOpenApi()";
}
if (!Model.UseTypedResults)
{
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces<{modelName}>(StatusCodes.Status200OK)";
builderExtensions += $"\r\n{builderExtensionSpaces}.Produces(StatusCodes.Status404NotFound)";
}
#>
<#=builderExtensions #>;
}
Expand Down

0 comments on commit 0f6872d

Please sign in to comment.