Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/AutoRest.Core/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/AutoRest.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
<value>Parameters "subscriptionId" or "api-version" are referenced but not defined in the parameters section of Service Definition</value>
</data>
<data name="UniqueResourcePathsWarning" xml:space="preserve">
<value>More than one resource path is not allowed in a single spec</value>
<value>More than one resource path is not allowed in a single spec (found: {0}).</value>
</data>
<data name="PathCannotBeNullOrEmpty" xml:space="preserve">
<value>path cannot be null or an empty string or a string with white spaces while getting the parent directory</value>
Expand Down
21 changes: 13 additions & 8 deletions src/modeler/AutoRest.Swagger/Validation/UniqueResourcePaths.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using AutoRest.Core.Logging;
using AutoRest.Core.Properties;
using AutoRest.Core.Validation;
Expand All @@ -13,18 +14,22 @@ namespace AutoRest.Swagger.Validation
{
public class UniqueResourcePaths : TypedRule<Dictionary<string, Dictionary<string, Operation>>>
{
// Ignore everything within /es except when we find microsoft.* and capture that group
private readonly Regex apiRegExp = new Regex(@"(?:/[^/]+)+/(?i)microsoft(?-i).([^/]+)(?:/[^/]+)+");
private readonly Regex resPathPattern = new Regex(@"/providers/(?<resPath>[^{/]+)/");

/// <summary>
/// This rule passes if the paths contain reference to exactly one of the namespace resources
/// </summary>
/// <param name="paths"></param>
/// <returns></returns>
public override bool IsValid(Dictionary<string, Dictionary<string, Operation>> paths)
=> paths.Keys.Select(path =>
{ return apiRegExp.Match(path).Success ? apiRegExp.Match(path).Groups?[0]?.Value.ToString().ToLowerInvariant() : null; })
.Where(p=>p!=null).Distinct().Count() <= 1;
public override bool IsValid(Dictionary<string, Dictionary<string, Operation>> paths, RuleContext context, out object[] formatParameters)
{
var resources = paths.Keys
.SelectMany(path => resPathPattern.Matches(path)
.OfType<Match>()
.Select(match => match.Groups["resPath"].Value.ToString()))
.Distinct()
.ToArray();
formatParameters = new [] { string.Join(", ", resources) };
return resources.Length <= 1;
}

/// <summary>
/// The template message for this Rule.
Expand Down