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
18 changes: 18 additions & 0 deletions 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.

6 changes: 6 additions & 0 deletions src/core/AutoRest.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,10 @@
<data name="LongRunningResponseNotValid" xml:space="preserve">
<value>An operation with x-ms-long-running-operation extension must have a valid terminal success status code. 200 or 201 for Put/Patch. 200, 201 or 204 for Post. 200 or 204 or both for Delete.</value>
</data>
<data name="InvalidMutabilityValues" xml:space="preserve">
<value>Valid values for an x-ms-mutability extension are 'create', 'read' and 'update'. Applied extension contains invalid value(s): '{0}'.</value>
</data>
<data name="InvalidMutabilityValueForReadOnly" xml:space="preserve">
<value>When property is modeled as "readOnly": true then x-ms-mutability extension can only have "read" value. When property is modeled as "readOnly": false then applying x-ms-mutability extension with only "read" value is not allowed. Extension contains invalid values: '{0}'.</value>
</data>
</root>
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"
}
}
}
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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ public void LongRunningResponseForDeleteValidation()
var messages = ValidateSwagger(Path.Combine("Swagger", "Validation", "long-running-invalid-response-delete.json"));
messages.AssertOnlyValidationMessage(typeof(LongRunningResponseValidation));
}

[Fact]
public void MutabilityNotModeledValidation()
{
var messages = ValidateSwagger(Path.Combine("Swagger", "Validation", "mutability-invalid-values.json"));
messages.AssertOnlyValidationMessage(typeof(MutabilityValidValuesRule), 2);
}

[Fact]
public void MutabilityNotModeledWithReadOnlyValidation()
{
var messages = ValidateSwagger(Path.Combine("Swagger", "Validation", "mutability-invalid-values-for-readonly.json"));
messages.AssertOnlyValidationMessage(typeof(MutabilityWithReadOnlyRule), 2);
}
}

#region Positive tests
Expand Down
2 changes: 2 additions & 0 deletions src/modeler/AutoRest.Swagger/Model/SwaggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public SwaggerBase()
[CollectionRule(typeof(NextLinkPropertyMustExist))]
[CollectionRule(typeof(PageableRequires200Response))]
[CollectionRule(typeof(LongRunningResponseValidation))]
[CollectionRule(typeof(MutabilityValidValuesRule))]
Copy link
Copy Markdown
Contributor

@fearthecowboy fearthecowboy Jan 11, 2017

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:

// x-ms-mutability extension
[CollectionRule(typeof(MutabilityValidValuesRule))]
public IEnumerable<string> XmsMutability =>  ((Newtonsoft.Json.Linq.JArray)Extensions["x-ms-mutability"] )?.ToObject<string[]>();

[CollectionRule(typeof(MutabilityWithReadOnlyRule))]
public Dictionary<string, object> Extensions { get; set; }

/// <summary>
Expand Down
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; }
}
}
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;
}
}
}
Loading