-
Notifications
You must be signed in to change notification settings - Fork 931
Refactor user secrets to DI-based factory with thread-safe synchronization #12732
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
Merged
joperezr
merged 4 commits into
release/13.0
from
copilot/update-aspire-to-latest-version
Nov 6, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace Aspire.Hosting.Pipelines.Internal; | ||
|
|
||
| /// <summary> | ||
| /// Provides utility methods for flattening and unflattening JSON objects using colon-separated keys. | ||
| /// </summary> | ||
| internal static class JsonFlattener | ||
| { | ||
| /// <summary> | ||
| /// Flattens a JsonObject using colon-separated keys for configuration compatibility. | ||
| /// Handles both nested objects and arrays with indexed keys. | ||
| /// </summary> | ||
| /// <param name="source">The source JsonObject to flatten.</param> | ||
| /// <returns>A flattened JsonObject.</returns> | ||
| public static JsonObject FlattenJsonObject(JsonObject source) | ||
| { | ||
| var result = new JsonObject(); | ||
| FlattenJsonObjectRecursive(source, string.Empty, result); | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unflattens a JsonObject that uses colon-separated keys back into a nested structure. | ||
| /// Handles both nested objects and arrays with indexed keys. | ||
| /// </summary> | ||
| /// <param name="source">The flattened JsonObject to unflatten.</param> | ||
| /// <returns>An unflattened JsonObject with nested structure.</returns> | ||
| public static JsonObject UnflattenJsonObject(JsonObject source) | ||
| { | ||
| var result = new JsonObject(); | ||
|
|
||
| foreach (var kvp in source) | ||
| { | ||
| var keys = kvp.Key.Split(':'); | ||
| var current = result; | ||
|
|
||
| for (var i = 0; i < keys.Length - 1; i++) | ||
| { | ||
| var key = keys[i]; | ||
| if (!current.TryGetPropertyValue(key, out var existing) || existing is not JsonObject) | ||
| { | ||
| var newObject = new JsonObject(); | ||
| current[key] = newObject; | ||
| current = newObject; | ||
| } | ||
| else | ||
| { | ||
| current = existing.AsObject(); | ||
| } | ||
| } | ||
|
|
||
| current[keys[^1]] = kvp.Value?.DeepClone(); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static void FlattenJsonObjectRecursive(JsonObject source, string prefix, JsonObject result) | ||
| { | ||
| foreach (var kvp in source) | ||
| { | ||
| var key = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}:{kvp.Key}"; | ||
|
|
||
| if (kvp.Value is JsonObject nestedObject) | ||
| { | ||
| FlattenJsonObjectRecursive(nestedObject, key, result); | ||
| } | ||
| else if (kvp.Value is JsonArray array) | ||
| { | ||
| for (var i = 0; i < array.Count; i++) | ||
| { | ||
| var arrayKey = $"{key}:{i}"; | ||
| if (array[i] is JsonObject arrayObject) | ||
| { | ||
| FlattenJsonObjectRecursive(arrayObject, arrayKey, result); | ||
| } | ||
| else | ||
| { | ||
| result[arrayKey] = array[i]?.DeepClone(); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| result[key] = kvp.Value?.DeepClone(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Nodes; | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| namespace Aspire.Hosting.UserSecrets; | ||
|
|
||
| /// <summary> | ||
| /// Defines an interface for managing user secrets with support for read and write operations. | ||
| /// </summary> | ||
| internal interface IUserSecretsManager | ||
| { | ||
| /// <summary> | ||
| /// Gets the path to the user secrets file. | ||
| /// </summary> | ||
| string FilePath { get; } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to set a user secret value synchronously. | ||
| /// </summary> | ||
| /// <param name="name">The name of the secret.</param> | ||
| /// <param name="value">The value of the secret.</param> | ||
| /// <returns>True if the secret was set successfully; otherwise, false.</returns> | ||
| bool TrySetSecret(string name, string value); | ||
|
|
||
| /// <summary> | ||
| /// Gets a secret value if it exists in configuration, or sets it using the value generator if it doesn't. | ||
| /// </summary> | ||
| /// <param name="configuration">The configuration manager to check and update.</param> | ||
| /// <param name="name">The name of the secret.</param> | ||
| /// <param name="valueGenerator">Function to generate the value if it doesn't exist.</param> | ||
| void GetOrSetSecret(IConfigurationManager configuration, string name, Func<string> valueGenerator); | ||
|
|
||
| /// <summary> | ||
| /// Saves state to user secrets asynchronously (for deployment state manager). | ||
| /// If multiple callers save state concurrently, the last write wins. | ||
| /// </summary> | ||
| /// <param name="state">The state to save as a JSON object.</param> | ||
| /// <param name="cancellationToken">Cancellation token.</param> | ||
| Task SaveStateAsync(JsonObject state, CancellationToken cancellationToken = default); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The new constructor parameter
factoryis documented in the param tag, but the class-level summary on line 12-17 should be updated to mention that multiple constructors exist and explain when to use the factory parameter overload.