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

Add pageable rule #161

Merged
merged 6 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
134 changes: 134 additions & 0 deletions docs/pageable-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# PageableOperations

## Description

This rule appears when you define a get operation, and its response schema has a property that is an array. The operation might be pageable.

## How to fix

Add an `x-ms-pageable` extension to the operation.

## Effect on generated code

### Before

#### Spec

```json5
"paths": {
"/plants": {
"get": {
"operationId": "Stuff_List",
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/StuffList"
}
}
}
}
}
}
"definitions": {
"StuffList": {
"type": "object",
"properties": {
"value": {
"type": "array",
"items": {
"$ref": "#/definitions/Stuff"
}
},
"nextLink": {
"type": "string",
}
}
},
"Stuff": {
"type": "object",
"properties": {
"name": {
"type": "string",
},
}
}
}
```

#### Generated code

```csharp
public static ProductResult GetMultiplePages(this IPagingOperations operations, string clientRequestId = default(string), PagingGetMultiplePagesOptions pagingGetMultiplePagesOptions = default(PagingGetMultiplePagesOptions))
{
return operations.GetMultiplePagesAsync(clientRequestId, pagingGetMultiplePagesOptions).GetAwaiter().GetResult();
}
```

### After

#### Spec

```json5
"paths": {
"/plants": {
"get": {
"operationId": "Stuff_List",
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/StuffList"
}
}
}
"x-ms-pageable": {
"nextLinkName": "nextLink"
}
}
}
}
"definitions": {
"StuffList": {
"type": "object",
"properties": {
"value": {
"type": "array",
"items": {
"$ref": "#/definitions/Stuff"
}
},
"nextLink": {
"type": "string",
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Let's be diligent, mark this as readonly

}
},
"Stuff": {
"type": "object",
"properties": {
"name": {
"type": "string",
},
}
}
}
```

#### Generated code

```csharp
public static IPage<Product> GetMultiplePages(this IPagingOperations operations, string clientRequestId = default(string), PagingGetMultiplePagesOptions pagingGetMultiplePagesOptions = default(PagingGetMultiplePagesOptions))
{
return operations.GetMultiplePagesAsync(clientRequestId, pagingGetMultiplePagesOptions).GetAwaiter().GetResult();
}
public static IPage<Product> GetMultiplePagesNext(this IPagingOperations operations, string nextPageLink, string clientRequestId = default(string), PagingGetMultiplePagesOptions pagingGetMultiplePagesOptions = default(PagingGetMultiplePagesOptions))
{
return operations.GetMultiplePagesNextAsync(nextPageLink, clientRequestId, pagingGetMultiplePagesOptions).GetAwaiter().GetResult();
}
}
```
1 change: 1 addition & 0 deletions src/typescript/azure-openapi-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Message } from "../jsonrpc/types";
import { rules, OpenApiTypes, MergeStates } from "./rule";

// register rules
require("./rules/PageableOperation");
require("./rules/DescriptionMustNotBeNodeName");
require("./rules/ControlCharactersAreNotAllowed");
require("./rules/ArraySchemaMustHaveItems");
Expand Down
39 changes: 39 additions & 0 deletions src/typescript/azure-openapi-validator/rules/PageableOperation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { MergeStates, OpenApiTypes, rules } from '../rule';
Copy link
Contributor

Choose a reason for hiding this comment

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

If I remember correctly, we decided to flag this only if a model had less than 3 properties with one being the value array. There might be models which have a value array and a whole lot of other properties, should we flag this rule for such cases too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had not heard the 3 properties thing, but it makes sense

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great if you can talk to one of the PMs regarding this before going through with the implementation

export const PageableOperation: string = "PageableOperation";

const jp = require('jsonpath');

rules.push({
id: "R2029",
name: PageableOperation,
severity: "warning",
category: "SDKViolation",
mergeState: MergeStates.composed,
openapiType: OpenApiTypes.arm | OpenApiTypes.dataplane,
appliesTo_JsonQuery: "$.paths[?(@.get)]",
run: function* (doc, node, path) {
const operations = Object.keys(node);
const getKey = operations.find(key => {
return key.toLowerCase() === 'get';
});

const schemaRef = node[getKey]['responses']['200']['schema']['$ref'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Most operations should have a 200 response but what happens here if they don't?

const schemaPath: string[] = (<string>schemaRef).split('/');
const schemaProperties = doc.definitions[schemaPath[2]].properties;
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if the model is defined inline?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can run with this assumption since there are linter rules in place to flag inline definitions
On the other hand though, moving this to a utility method could be helpful


const valueKey = Object.keys(schemaProperties).find(key => {
return key.toLowerCase() === 'value';
Copy link
Member

Choose a reason for hiding this comment

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

Is the key - 'value' a mandatory field? Please confirm if an array could be defined without this key

});

if (valueKey != undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

Will this be true if the valueKey is null? If so, I am fine

if (schemaProperties[valueKey].type === 'array') {
yield { message: `Based on the response model schema, operation '${node[getKey].operationId}' might be pageable. Consider adding the x-ms-pageable swagger extension.`, location: path.concat(getKey) };
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: let's not call it swagger 😄


}
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ import {
collectTestMessagesFromValidator
} from './utilities/tests-helper';
import { DescriptionMustNotBeNodeName } from '../rules/DescriptionMustNotBeNodeName';
import { PageableOperation } from '../rules/PageableOperation';

@suite class CompositeAzureTests {
@test async "description should not be property name"() {
const fileName: string = 'DescriptionSameAsPropertyName.json';
const messages: Message[] = await collectTestMessagesFromValidator(fileName, OpenApiTypes.arm, MergeStates.composed);
assertValidationRuleCount(messages, DescriptionMustNotBeNodeName, 2);
}

@test async "operations returning a model including an array might be pageable"() {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a positive test case too?

const fileName: string = 'PageableOperation.json';
const messages: Message[] = await collectTestMessagesFromValidator(fileName, OpenApiTypes.arm, MergeStates.composed);
assertValidationRuleCount(messages, PageableOperation, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"swagger": "2.0",
"info": {
"title": "LinterTests",
"description": "Use this swagger to test the linter to detect response types with arrays as possible pageable operations",
"version": "2016-10-10"
},
"host": "management.azure.com",
"paths": {
"/plants": {
"get": {
"description": "Get a list of plants.",
"operationId": "Plants_List",
"parameters": [],
"responses": {
"200": {
"description": "OK.",
"schema": {
"$ref": "#/definitions/PlantsList"
}
}
}
}
}
},
"definitions": {
"PlantsList": {
"type": "object",
"description": "Response to a Pageable operation. This one lists plants",
"properties": {
"value": {
"type": "array",
"description": "List of plants",
"items": {
"$ref": "#/definitions/Plant"
}
},
"nextLink": {
"type": "string",
"description": "Link to next page of results"
}
}
},
"Plant": {
"type": "object",
"description": "A plant",
"properties": {
"name": {
"type": "string",
"description": "The plant's name"
},
"edible": {
"type": "boolean",
"description": "Is the plant edible?"
}
}
}
},
"parameters": {}
}