microsoft / microsoft/aspire

Improve / Document creating a parameter with a dynamic default value

Open
#16,890 2 comments 0 reactions 0 assignees View on GitHub
area-app-model aspirifridays
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

On aspirifriday today, we wanted to create a parameter value to represent a JWT signing key - a 32 byte base64 string.

But there was no easy way to provide our own default value generator. There is the (undocumented) `GenerateParameterDefault` class, but that would be very unlikely to generate a valid base64 string.

```cs
var jwtSigningKey = builder.AddParameter(
"jwt-signing-key",
new Base64ParameterDefault(32),
secret: true,
persist: true);

internal sealed class Base64ParameterDefault(int byteLength) : Aspire.Hosting.ApplicationModel.ParameterDefault
{
public int ByteLength { get; } = byteLength > 0
? byteLength
: throw new ArgumentOutOfRangeException(nameof(byteLength));

public override string GetDefaultValue()
{
return Convert.ToBase64String(System.Security.Cryptography.RandomNumberGenerator.GetBytes(ByteLength));
}

public override void WriteToManifest(Aspire.Hosting.Publishing.ManifestPublishingContext context)
{
context.Writer.WriteStartObject("generateBase64");
context.Writer.WriteNumber("byteLength", ByteLength);
context.Writer.WriteEndObject();
}
}
```

### Describe the solution you'd like

I'd like to see an easier way to provide a default value with little more than a generator function, something like the below. As well as some documentaiton on how to do so. (Which would likely also need to cover `GenerateParameterDefault`):

```cs
var jwtSigningKey = builder.AddParameter(
"jwt-signing-key",
() => (string)return CalculateDefaultValue(),
secret: true,
persist: true);
```
### Additional context

https://www.youtube.com/live/RGBK63vn3Rc?si=CRyNwxlB5POnBibs&t=5386

Contributor guide

Open the contributing guide

Research direction

Start with the AddParameter API and the existing GenerateParameterDefault class mentioned in the issue, then review how custom parameter defaults are represented and documented. Done means a simpler generator-based default option is supported and the documentation explains both that option and GenerateParameterDefault.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.