-
Notifications
You must be signed in to change notification settings - Fork 737
[Linter Rules] M2008 for x-ms-mutability extension #1712
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
Merged
Changes from all commits
Commits
Show all changes
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
97 changes: 97 additions & 0 deletions
97
...ler/AutoRest.Swagger.Tests/Swagger/Validation/mutability-invalid-values-for-readonly.json
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,97 @@ | ||
| { | ||
| "swagger": "2.0", | ||
| "info": { | ||
| "title": "Mutability extension used with invalid values when modeled with readonly.", | ||
| "description": "Some documentation.", | ||
| "version": "2014-04-01-preview" | ||
| }, | ||
| "host": "management.azure.com", | ||
| "schemes": [ | ||
| "https" | ||
| ], | ||
| "basePath": "/", | ||
| "produces": [ | ||
| "application/json" | ||
| ], | ||
| "consumes": [ | ||
| "application/json" | ||
| ], | ||
| "paths": { | ||
| "/foo": { | ||
| "post": { | ||
| "operationId": "Noun_Verb", | ||
| "summary": "Foo path", | ||
| "description": "Foo operation", | ||
| "parameters": [ | ||
| { | ||
| "name": "foo1", | ||
| "description": "Name of the domain.", | ||
| "type": "string" | ||
| } | ||
| ], | ||
| "responses": { | ||
| "default": { | ||
| "description": "Unexpected error" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "definitions": { | ||
| "Resource": { | ||
| "description": "The Resource Model definition.", | ||
| "properties": { | ||
| "id": { | ||
| "readOnly": true, | ||
| "type": "string", | ||
| "description": "Resource Id", | ||
| "x-ms-mutability": [ "read" ] | ||
| }, | ||
| "name": { | ||
| "readOnly": true, | ||
| "type": "string", | ||
| "description": "Resource name", | ||
| "x-ms-mutability": [ "create", "read" ] | ||
| }, | ||
| "type": { | ||
| "type": "string", | ||
| "description": "Resource type", | ||
| "x-ms-mutability": [ "read" ] | ||
| }, | ||
| "location": { | ||
| "type": "string", | ||
| "description": "Resource location", | ||
| "x-ms-mutability": [ "create", "read" ] | ||
| }, | ||
| "tags": { | ||
| "type": "object", | ||
| "additionalProperties": { | ||
| "type": "string" | ||
| }, | ||
| "description": "Resource tags", | ||
| "x-ms-mutability": [ "create", "read", "update" ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "location" | ||
| ], | ||
| "x-ms-azure-resource": true | ||
| } | ||
| }, | ||
| "parameters": { | ||
| "SubscriptionIdParameter": { | ||
| "name": "subscriptionId", | ||
| "in": "path", | ||
| "required": true, | ||
| "type": "string", | ||
| "description": "test subscription id" | ||
| }, | ||
| "ApiVersion": { | ||
| "name": "api-version", | ||
| "in": "path", | ||
| "required": true, | ||
| "type": "string", | ||
| "description": "test api version" | ||
| } | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/modeler/AutoRest.Swagger.Tests/Swagger/Validation/mutability-invalid-values.json
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,86 @@ | ||
| { | ||
| "swagger": "2.0", | ||
| "info": { | ||
| "title": "Mutability extension used with invalid values.", | ||
| "description": "Some documentation.", | ||
| "version": "2014-04-01-preview" | ||
| }, | ||
| "host": "management.azure.com", | ||
| "schemes": [ | ||
| "https" | ||
| ], | ||
| "basePath": "/", | ||
| "produces": [ | ||
| "application/json" | ||
| ], | ||
| "consumes": [ | ||
| "application/json" | ||
| ], | ||
| "paths": { | ||
| "/foo": { | ||
| "post": { | ||
| "operationId": "Noun_Verb", | ||
| "summary": "Foo path", | ||
| "description": "Foo operation", | ||
| "parameters": [ | ||
| { | ||
| "name": "foo1", | ||
| "description": "Name of the domain.", | ||
| "type": "string" | ||
| } | ||
| ], | ||
| "responses": { | ||
| "default": { | ||
| "description": "Unexpected error" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "definitions": { | ||
| "Resource": { | ||
| "description": "The Resource Model definition.", | ||
| "properties": { | ||
| "id": { | ||
| "readOnly": true, | ||
| "type": "string", | ||
| "description": "Resource Id", | ||
| "x-ms-mutability": [ "readWrite" ] | ||
| }, | ||
| "name": { | ||
| "type": "string", | ||
| "description": "Resource name", | ||
| "x-ms-mutability": [ "read", "create", "update", "delete" ] | ||
| }, | ||
| "tags": { | ||
| "type": "object", | ||
| "additionalProperties": { | ||
| "type": "string" | ||
| }, | ||
| "description": "Resource tags", | ||
| "x-ms-mutability": [ "create", "read", "update" ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "location" | ||
| ], | ||
| "x-ms-azure-resource": true | ||
| } | ||
| }, | ||
| "parameters": { | ||
| "SubscriptionIdParameter": { | ||
| "name": "subscriptionId", | ||
| "in": "path", | ||
| "required": true, | ||
| "type": "string", | ||
| "description": "test subscription id" | ||
| }, | ||
| "ApiVersion": { | ||
| "name": "api-version", | ||
| "in": "path", | ||
| "required": true, | ||
| "type": "string", | ||
| "description": "test api version" | ||
| } | ||
| } | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
src/modeler/AutoRest.Swagger/Validation/Extensions/MutabilityExtensionRule.cs
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,33 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using AutoRest.Core.Logging; | ||
| using AutoRest.Core.Validation; | ||
| using AutoRest.Swagger.Model; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace AutoRest.Swagger.Validation | ||
| { | ||
| public abstract class MutabilityExtensionRule : ExtensionRule | ||
| { | ||
| protected override string ExtensionName => "x-ms-mutability"; | ||
|
|
||
| protected static Schema Get200ResponseSchema(RuleContext context) | ||
| { | ||
| OperationResponse response = context?.GetFirstAncestor<Operation>()?.Responses?.GetValueOrNull("200"); | ||
| if (response == null) | ||
| { | ||
| return null; | ||
| } | ||
| var resolver = new SchemaResolver(context?.GetServiceDefinition()); | ||
| return resolver.Unwrap(response.Schema); | ||
| } | ||
|
|
||
| public abstract override string MessageTemplate { get; } | ||
|
|
||
| /// <summary> | ||
| /// The severity of this message (ie, debug/info/warning/error/fatal, etc) | ||
| /// </summary> | ||
| public abstract override Category Severity { get; } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/modeler/AutoRest.Swagger/Validation/Extensions/MutabilityValidValuesRule.cs
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,58 @@ | ||
| // 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 AutoRest.Core.Logging; | ||
| using AutoRest.Core.Properties; | ||
| using AutoRest.Core.Validation; | ||
|
|
||
| namespace AutoRest.Swagger.Validation | ||
| { | ||
| public class MutabilityValidValuesRule : MutabilityExtensionRule | ||
| { | ||
| /// <summary> | ||
| /// Array of valid values for x-ms-mutability extension. | ||
| /// </summary> | ||
| protected readonly string[] ValidValues = { "create", "read", "update" }; | ||
|
|
||
| /// <summary> | ||
| /// An x-ms-mutability extension passes this rule if it has only valid possible values. | ||
| /// </summary> | ||
| /// <param name="mutable">mutability extension object.</param> | ||
| /// <param name="context">Rule context.</param> | ||
| /// <param name="formatParameters">array of invalid parameters to be returned.</param> | ||
| /// <returns><c>true</c> if all values for x-ms-mutability are valid, otherwise <c>false</c>.</returns> | ||
| /// <remarks>This rule corresponds to M2006.</remarks> | ||
| public override bool IsValid(object mutable, RuleContext context, out object[] formatParameters) => ValidateMutabilityValues(mutable, context, out formatParameters); | ||
|
|
||
| /// <summary> | ||
| /// The template message for this Rule. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This may contain placeholders '{0}' for parameterized messages. | ||
| /// </remarks> | ||
| public override string MessageTemplate => Resources.InvalidMutabilityValues; | ||
|
|
||
| /// <summary> | ||
| /// The severity of this message (ie, debug/info/warning/error/fatal, etc) | ||
| /// </summary> | ||
| public override Category Severity => Category.Warning; | ||
|
|
||
| /// <summary> | ||
| /// Verify that mutability values are valid. | ||
| /// </summary> | ||
| /// <param name="mutable">mutability extension object.</param> | ||
| /// <param name="context">Rule context.</param> | ||
| /// <param name="formatParameters">array of invalid parameters to be returned.</param> | ||
| /// <returns><c>true</c> if all values for x-ms-mutability are valid, otherwise <c>false</c>.</returns> | ||
| private bool ValidateMutabilityValues(object mutable, RuleContext context, out object[] formatParameters) | ||
| { | ||
| string[] values = ((Newtonsoft.Json.Linq.JArray)mutable).ToObject<string[]>(); | ||
| string[] invalidValues = values.Except(ValidValues, StringComparer.OrdinalIgnoreCase).ToArray(); | ||
| formatParameters = invalidValues.Length == 0 ? new object[0] : new string[] { String.Join(",", invalidValues) }; | ||
|
|
||
| return invalidValues.Length == 0; | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
[comment, no action required] We are surely going to hell... we're putting a ton of rules on the extensions collection. We're really going to have to think this thru better in AutoRest 2.0 //cc @olydis
I wonder if we shouldn't be making properties for known extensions, and attaching the rules to the individual properties?
ie: