Skip to content
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

Adding new rule XmsParameterLocation: Check Global parameters for this extension #149

Merged
merged 6 commits into from
Mar 14, 2018
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
15 changes: 15 additions & 0 deletions docs/x-ms-parameter-location.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# XmsParameterLocation
## Description
An Open API Document has a section for parameters. Any parameter that is defined in this (global) parameters section will be treated as client parameters(by autorest). So, the service teams must be absolutely sure that this is the expectation (i.e. defining them as client properties) before defining the parameters in this section.

90% scenario is that subscriptionId and api-version are parameters that should be defined in the global parameters section.

However, one can define a parameter that is being referenced in multiple operations (example: resourceGroupName) in the global parameters section and apply the extension "x-ms-parameter-location": "method". This will then not be a client property.

So, when you define a parameter in the global parameters section, apply the extension "x-ms-parameter-location": "method" so this will not be treated as a client property. But, if you actually want the parameter to be client properties then apply the extension "x-ms-parameter-location": "client".

## How to fix
Apply "x-ms-parameter-location": "method"/"x-ms-parameter-location": "client" on the global parameters (based on your requirements)

## Reference
Refer [Link](https://github.com/Azure/autorest/blob/master/docs/extensions/readme.md#x-ms-parameter-location) for further details.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenAPI.Validator.Tests
{

[Collection("Validation Tests")]
public partial class OpenAPIModelerValidationTests
{
Expand Down Expand Up @@ -98,6 +98,13 @@ public void AnonymousParameterSchemaValidation()
Assert.Equal(messages.Count(), 1);
}

[Fact]
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a good idea to add a positive test case as well.

public void XmsParameterLocationValidation()
{
var messages = GetValidationMessagesForRule<XmsParameterLocation>("xms-parameter-location.json");
Assert.Equal(messages.Count(), 1);
}

[Fact]
public void OperationParametersValidation()
{
Expand Down Expand Up @@ -638,7 +645,7 @@ public void LocationPropertyWithoutXmsMutability()
var messages = GetValidationMessagesForRule<LocationMustHaveXmsMutability>("location-without-xms-mutability.json");
Assert.Equal(messages.Count(), 1);
}

[Fact]
public void LocationPropertyWithIncorrectXmsMutability()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
"description": "foo",
"operationId": "Operations_List",
"x-ms-examples": {
"Get operations": {

}
"Get operations": {}
},
"parameters": [
{
Expand Down Expand Up @@ -75,9 +73,7 @@
"pets"
],
"x-ms-examples": {
"The pet we get": {

}
"The pet we get": {}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Ideally json keys should not have spaces as such, consider renaming this to thePetWeGet

},
"parameters": [
{
Expand Down Expand Up @@ -118,9 +114,7 @@
"pets"
],
"x-ms-examples": {
"The pet we post": {

}
"The pet we post": {}
},
"responses": {
"201": {
Expand All @@ -144,9 +138,7 @@
"pets"
],
"x-ms-examples": {
"The pet we get": {

}
"The pet we get": {}
},
"parameters": [
{
Expand Down Expand Up @@ -177,9 +169,7 @@
"get": {
"operationId": "Foos_Get",
"x-ms-examples": {
"The foo we get": {

}
"The foo we get": {}
},
"description": "foo",
"responses": {
Expand All @@ -192,9 +182,7 @@
"operationId": "Foos_Post",
"description": "foo",
"x-ms-examples": {
"The foo we post": {

}
"The foo we post": {}
},
"parameters": [
{
Expand Down Expand Up @@ -224,9 +212,7 @@
"operationId": "Foos_PostOverload",
"description": "foo",
"x-ms-examples": {
"The foo we post": {

}
"The foo we post": {}
},
"parameters": [
{
Expand Down Expand Up @@ -255,7 +241,8 @@
"in": "query",
"name": "FooQueryParam",
"description": "Query parameter for Foo operation",
"type": "string"
"type": "string",
"x-ms-parameter-location": "method"
},
"SubscriptionIdParameter": {
"name": "subscriptionId",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"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": {
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Cache/Redis/{name}": {
"delete": {
"tags": [
"Redis"
],
"operationId": "Redis_Delete",
"description": "Deletes a Redis cache.",
"x-ms-long-running-operation": true,
"parameters": [
{
"name": "resourceGroupName",
"in": "path",
"required": true,
"type": "string",
"description": "The name of the resource group."
},
{
"name": "name",
"in": "path",
"required": true,
"type": "string",
"description": "The name of the Redis cache."
},
{
"$ref": "#/parameters/ApiVersionParameter"
},
{
"$ref": "#/parameters/SubscriptionIdParamterer"
}
],
"responses": {
"200": {
"description": ""
},
"204": {
"description": ""
}
}
}
}
},
"definitions": {
"Sku": {
"description": "The SKU (pricing tier) of the CDN profile.",
"type": "object",
"properties": {
"name": {
"$ref": "#/definitions/SkuName",
"description": "The name."
}
}
},
"SkuName": {
"type": "string",
"enum": [
"NotSpecified",
"Free",
"Shared",
"Basic",
"Standard",
"Premium"
],
"x-ms-enum": {
"name": "SkuName",
"modelAsString": false
},
"description": "The sku name."
},
"Resource": {
"description": "The Resource model definition.",
"properties": {
"id": {
"readOnly": true,
"type": "string",
"x-ms-client-name": "id2",
"description": "Resource Id"
},
"name": {
"readOnly": true,
"type": "string",
"description": "Resource name"
},
"type": {
"readOnly": true,
"type": "string",
"description": "Resource type"
},
"location": {
"type": "string",
"description": "Resource location"
},
"tags": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "Resource tags"
}
},
"required": [
"location"
],
"x-ms-azure-resource": true
}
},
"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"
},
"CatalogNameParameter": {
"name": "catalogName",
"in": "path",
"required": true,
"type": "string",
"description": "The name of the data catlog in the specified subscription and resource group."
}
}
}
1 change: 1 addition & 0 deletions src/dotnet/OpenAPI.Validator/Model/ServiceDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public ServiceDefinition()
/// </summary>
[Rule(typeof(ParameterNotDefinedInGlobalParameters))]
[CollectionRule(typeof(AnonymousBodyParameter))]
[CollectionRule(typeof(XmsParameterLocation))]
public Dictionary<string, SwaggerParameter> Parameters { get; set; }

/// <summary>
Expand Down
Loading