-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation rule to check if operationId nouns conflict with model nam…
…es (#2150)
- Loading branch information
Showing
11 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
57 changes: 57 additions & 0 deletions
57
...utoRest.Swagger.Tests/Resource/Swagger/Validation/operationid-noun-conflicting-model.json
This file contains 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,57 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"title": "Microsoft Azure Redis Cache Management API", | ||
"description": "Some cool documentation.", | ||
"version": "2014-04-01-preview" | ||
}, | ||
"host": "management.azure.com", | ||
"schemes": [ | ||
"https" | ||
], | ||
"basePath": "/", | ||
"produces": [ "application/json" ], | ||
"consumes": [ "application/json" ], | ||
"paths": { | ||
"/foo": { | ||
"get": { | ||
"operationId": "Model_Get", | ||
"description": "gets a model", | ||
"parameters": [ | ||
], | ||
"responses": { | ||
"200": { | ||
"schema": { | ||
"$ref": "#/definitions/Model" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"Model": { | ||
"properties": { | ||
"prop1": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"parameters": { | ||
"SubscriptionIdParamterer": { | ||
"name": "subscriptionId", | ||
"in": "path", | ||
"description": "Subscription ID.", | ||
"required": true, | ||
"type": "string" | ||
}, | ||
"ApiVersionParameter": { | ||
"name": "apiVersion", | ||
"in": "path", | ||
"description": "API ID.", | ||
"required": true, | ||
"type": "string" | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
72 changes: 72 additions & 0 deletions
72
src/modeler/AutoRest.Swagger/Validation/OperationIdNounConflictingModelNames.cs
This file contains 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,72 @@ | ||
// 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 System.Linq; | ||
using System.Text.RegularExpressions; | ||
using AutoRest.Core.Logging; | ||
using AutoRest.Core.Properties; | ||
using AutoRest.Swagger.Model; | ||
using AutoRest.Swagger.Validation.Core; | ||
|
||
namespace AutoRest.Swagger.Validation | ||
{ | ||
public class OperationIdNounConflictingModelNames : TypedRule<string> | ||
{ | ||
private readonly Regex NounVerbPattern = new Regex("^(?<noun>\\w+)?_(\\w+)$"); | ||
|
||
/// <summary> | ||
/// Id of the Rule. | ||
/// </summary> | ||
public override string Id => "M2063"; | ||
|
||
/// <summary> | ||
/// Violation category of the Rule. | ||
/// </summary> | ||
public override ValidationCategory ValidationCategory => ValidationCategory.SDKViolation; | ||
|
||
/// <summary> | ||
/// Check if the noun part of an operationId (Noun_Verb) conflicts with any model names provided in the spec | ||
/// </summary> | ||
/// <param name="entity">The operation id to test</param> | ||
/// <param name="formatParameters">The noun to be put in the failure message</param> | ||
/// <returns></returns> | ||
public override bool IsValid(string entity, RuleContext context, out object[] formatParameters) | ||
{ | ||
var match = NounVerbPattern.Match(entity); | ||
formatParameters = new object[] { }; | ||
|
||
// if operationId does not match the pattern, return true and let some other validation handle it | ||
if (!match.Success) | ||
{ | ||
return true; | ||
} | ||
|
||
// Get the noun and verb parts of the operation id | ||
var noun = match.Groups["noun"].Value; | ||
|
||
var serviceDefinition = (ServiceDefinition)context.Root; | ||
if (serviceDefinition.Definitions.Keys.Any(key => key.ToLower().Equals(noun.ToLower()))) | ||
{ | ||
formatParameters = new object[] { noun, noun }; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// The template message for this Rule. | ||
/// </summary> | ||
/// <remarks> | ||
/// This may contain placeholders '{0}' for parameterized messages. | ||
/// </remarks> | ||
public override string MessageTemplate => Resources.OperationIdNounConflictingModelNamesMessage; | ||
|
||
/// <summary> | ||
/// The severity of this message (ie, debug/info/warning/error/fatal, etc) | ||
/// </summary> | ||
public override Category Severity => Category.Warning; | ||
|
||
} | ||
} |