-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add minimal option to webapi template #36068
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
Conversation
- Add "minimal" option to webapi project template - Factor Program.cs into multiple files and update template manifest to exclude/rename dependent on selected options - Updated controller and minimal versions to set endpoint/route name when EnableOpenAPI is true - Configure webapi template minimal option for VS display as "Use controllers"
Here's how the var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
} And here it is with using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.Resource;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
var scopeRequiredByApi = app.Configuration["AzureAd:Scopes"];
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", (HttpContext httpContext) =>
{
httpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.RequireAuthorization();
app.Run();
class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
} |
Should WeatherForecast be in another file? |
@davidfowl that's up to us. I moved it into |
Made the template baseline test more resilient by ensuring that all template arg options without values are added to the project key rather than a specific few. Args that have a value are still not added to the key. Keys are all tracked now to ensure uniqueness & an exception is thrown if they aren't. Renamed a few things for better clarity and easy of debugging too.
|
||
text += AuthenticationOptionRegex.Match(arguments) | ||
.Groups.TryGetValue("auth", out var auth) ? auth.Value : ""; | ||
private string CreateProjectKey(string arguments) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minor question I had was to confirm that we don't care about ordering variation... since "dotnet new blazorwasm -ho --auth Individual" won't be considered the same as "dotnet new blazorwasm --auth Individual -ho", probably not an issue since our tests hopefully are always consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question as this routine would now produce a unique key. I think the "worst" that would happen in that case is we end up with a duplicate test, rather than the worst case now, being tests that should be unique, aren't, and the tests fail with somewhat obscure errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a sort to make it a non-issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var argumentsArray = arguments | ||
.Split(" --", int.MaxValue, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) | ||
.SelectMany(e => e.Split(" -", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)) | ||
.ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this any different than the following?
var argumentsArray = arguments | |
.Split(" --", int.MaxValue, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) | |
.SelectMany(e => e.Split(" -", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)) | |
.ToArray(); | |
var argumentsArray = arguments | |
.Split(new[] { " --", " -" }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries) | |
.ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried that first and it didn't seem to produce what I wanted, but it may be that my intent changed during refactoring. I can try again although I found the meaning of passing a string array as slightly ambiguous WRT to pass ordering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My basic testing shows it gives the same result "new templatename -minimal -au SingleOrg --another-option OptionValue". I nerd sniped @BrennanConroy who found this in the docs.
To avoid ambiguous results when strings in separator have characters in common, the Split method proceeds from the beginning to the end of the value of the instance, and matches the first element in separator that is equal to a delimiter in the instance. The order in which substrings are encountered in the instance takes precedence over the order of elements in separator.
So it seems it should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it locally and it does work. I'll roll it into the change to make WeatherForecast
a record too.
- Change WeatherForecast to a record - Simplify method in test
This looks really good @DamianEdwards! Thanks for getting this done so quickly |
@dougbu can you merge this please? |
* Add minimal option to webapi template - Add "minimal" option to webapi project template - Factor Program.cs into multiple files and update template manifest to exclude/rename dependent on selected options - Updated controller and minimal versions to set endpoint/route name when EnableOpenAPI is true - Configure webapi template minimal option for VS display as "Use controllers" * Update template baselines & fix casing of option description * Fix template baseline tests issue * Update template baseline test to be more resilient Made the template baseline test more resilient by ensuring that all template arg options without values are added to the project key rather than a specific few. Args that have a value are still not added to the key. Keys are all tracked now to ensure uniqueness & an exception is thrown if they aren't. Renamed a few things for better clarity and easy of debugging too. * Make template baseline test project key disregard ordering * Update based on feedback - Change WeatherForecast to a record - Simplify method in test
- Add "minimal" option to webapi project template - Factor Program.cs into multiple files and update template manifest to exclude/rename dependent on selected options - Updated controller and minimal versions to set endpoint/route name when EnableOpenAPI is true - Configure webapi template minimal option for VS display as "Use controllers"
Fixes #35996