Repository navigation
Validate resource name - #1691
Conversation
| /// - Must not contain consecutive hyphens. | ||
| /// - Must be between 1 and 64 characters long. | ||
| /// </summary> | ||
| internal static void ValidateName(string name) |
There was a problem hiding this comment.
IMO verbose + easy to understand is better than 1 line regex + hard to understand
There was a problem hiding this comment.
The battle rages on 😉
|
|
||
| if (name.Length > 64) | ||
| { | ||
| throw new ArgumentException($"Resource name '{name}' is invalid. Name must be between 1 and 64 characters long.", nameof(name)); |
There was a problem hiding this comment.
Random question, have we not globalized strings in Aspire.Hosting (or elsewhere in this repo) yet?
There was a problem hiding this comment.
They're in the dashboard.
| if (Resources.FirstOrDefault(r => string.Equals(r.Name, resource.Name, StringComparison.OrdinalIgnoreCase)) is { } existingResource) | ||
| { | ||
| throw new DistributedApplicationException($"Cannot add resource of type '{resource.GetType()}' with name '{resource.Name}' because resource of type '{existingResource.GetType()}' with that name already exists."); | ||
| throw new DistributedApplicationException($"Cannot add resource of type '{resource.GetType()}' with name '{resource.Name}' because resource of type '{existingResource.GetType()}' with that name already exists. Resource names are case-insensitive."); |
There was a problem hiding this comment.
We have a bunch of other code that assumes resource names are case sensitive. Where does the case-insensitivity come from?
There was a problem hiding this comment.
Oh, I see this in Damian Edwards (@DamianEdwards)'s comment on the linked issue. Is that case-insensitivity modelled throughout the whole stack? There's a bunch of code in the dashboard that assumes StringComparison.Ordinal for resource names.
There was a problem hiding this comment.
Resource names are really only case-insensitive when building the model. A case-insensitive check is used to prevent duplicates ("TEST" and "test"). Only one is allowed and will make it into the dashboard.
If there is only "TEST" in the dashboard then it doesn't matter whether case is ignored at that point or not.
There was a problem hiding this comment.
But a consistent comparer doesn't hurt (well, it's a tiny bit slower comparison when ignoring case, but that will never be impactful)
In microsoft#1691 the semantics around comparisons of resource names changed to make them case-insensitive. This change needs to be applied in some other places, for consistency. We have a `StringComparers` class which denotes the comparison style for different types of values. This change extends the use of that type, and changes the value for `ResourceName` from `Ordinal` to `OrdinalIgnoreCase`. It also moves that internal type into the `Aspire` namespace so that it's picked up by both projects that link it (`Aspire.Hosting` and `Aspire.Dashboard`) without needing to choose a namespace from either one, which then wouldn't make sense in the other.
In #1691 the semantics around comparisons of resource names changed to make them case-insensitive. This change needs to be applied in some other places, for consistency. We have a `StringComparers` class which denotes the comparison style for different types of values. This change extends the use of that type, and changes the value for `ResourceName` from `Ordinal` to `OrdinalIgnoreCase`. It also moves that internal type into the `Aspire` namespace so that it's picked up by both projects that link it (`Aspire.Hosting` and `Aspire.Dashboard`) without needing to choose a namespace from either one, which then wouldn't make sense in the other.
Fixes #1399
Uses rules at #1399 (comment)
Microsoft Reviewers: Open in CodeFlow