Skip to content

Validate resource name - #1691

Merged
James Newton-King (JamesNK) merged 4 commits into
mainfrom
jamesnk/validate-resource-name
Jan 19, 2024
Merged

James Newton-King (JamesNK) merged 4 commits into
mainfrom
jamesnk/validate-resource-name

Conversation

@JamesNK

@JamesNK James Newton-King (JamesNK) commented Jan 18, 2024 •

Copy link
Copy Markdown
Member

Fixes #1399

Uses rules at #1399 (comment)

Microsoft Reviewers: Open in CodeFlow

@JamesNK James Newton-King (JamesNK) added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Jan 18, 2024
/// - Must not contain consecutive hyphens.
/// - Must be between 1 and 64 characters long.
/// </summary>
internal static void ValidateName(string name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not regex?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO verbose + easy to understand is better than 1 line regex + hard to understand

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random question, have we not globalized strings in Aspire.Hosting (or elsewhere in this repo) yet?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're in the dashboard.

@JamesNK
James Newton-King (JamesNK) deleted the jamesnk/validate-resource-name branch January 19, 2024 04:01
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.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a bunch of other code that assumes resource names are case sensitive. Where does the case-insensitivity come from?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a consistent comparer doesn't hurt (well, it's a tiny bit slower comparison when ignoring case, but that will never be impactful)

Drew Noakes (drewnoakes) added a commit to drewnoakes/aspire that referenced this pull request Jan 19, 2024
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.
Drew Noakes (drewnoakes) added a commit that referenced this pull request Jan 19, 2024
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.
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use "." characters in resource names

4 participants