@azure/avocado
Advanced tools
Comparing version
# Changelog | ||
## 0.6.4 | ||
- Add rule INCONSISTENT_API_VERSION to validate swagger api version must consistent with its file path. | ||
## 0.6.3 | ||
@@ -4,0 +8,0 @@ |
import * as jsonParser from '@ts-common/json-parser'; | ||
declare type ErrorMessage = 'The example JSON file is not referenced from the swagger file.' | 'The swagger JSON file is not referenced from the readme file.' | 'The `readme.md` is not an AutoRest markdown file.' | 'The JSON file is not found but it is referenced from the readme file.' | 'The JSON file has a circular reference.' | 'The file is not a valid JSON file.' | 'Can not find readme.md in the folder. If no readme.md file, it will block SDK generation.'; | ||
declare type ErrorMessage = 'The example JSON file is not referenced from the swagger file.' | 'The swagger JSON file is not referenced from the readme file.' | 'The `readme.md` is not an AutoRest markdown file.' | 'The JSON file is not found but it is referenced from the readme file.' | 'The JSON file has a circular reference.' | 'The file is not a valid JSON file.' | 'Can not find readme.md in the folder. If no readme.md file, it will block SDK generation.' | 'The API version of the swagger is inconsistent with its file path.'; | ||
export interface IErrorBase { | ||
@@ -42,3 +42,3 @@ readonly level: 'Warning' | 'Error'; | ||
*/ | ||
readonly code: 'NO_JSON_FILE_FOUND' | 'UNREFERENCED_JSON_FILE' | 'CIRCULAR_REFERENCE'; | ||
readonly code: 'NO_JSON_FILE_FOUND' | 'UNREFERENCED_JSON_FILE' | 'CIRCULAR_REFERENCE' | 'INCONSISTENT_API_VERSION'; | ||
/** | ||
@@ -45,0 +45,0 @@ * Error message. |
@@ -53,2 +53,8 @@ "use strict"; | ||
} | ||
case 'INCONSISTENT_API_VERSION': { | ||
return { | ||
code: error.code, | ||
url: error.jsonUrl, | ||
}; | ||
} | ||
} | ||
@@ -124,2 +130,16 @@ }; | ||
}; | ||
const validateSpecificationAPIVersion = (current, document) => it.iterable(function* () { | ||
const info = document.info; | ||
if (info !== undefined) { | ||
if (!current.path.includes(info.version)) { | ||
yield { | ||
code: 'INCONSISTENT_API_VERSION', | ||
level: 'Error', | ||
message: 'The API version of the swagger is inconsistent with its file path.', | ||
jsonUrl: current.path, | ||
readMeUrl: current.readMePath, | ||
}; | ||
} | ||
} | ||
}); | ||
/** | ||
@@ -137,3 +157,3 @@ * Validate each RP folder must have its readme file. | ||
validDirs.some(item => filePath.includes(item) && !ignoredDirs.some(ignoredItem => filePath.toLowerCase().includes(ignoredItem)))) | ||
.map(filepath => path.dirname(filepath)); | ||
.map(filePath => path.dirname(filePath)); | ||
const allJsonSet = new Set(); | ||
@@ -202,2 +222,5 @@ for await (const dir of allJsonDir) { | ||
yield* errors; | ||
if (current.kind === 'SWAGGER' && document !== null) { | ||
yield* validateSpecificationAPIVersion(current, document); | ||
} | ||
// Example file should ignore `$ref` because it's usually meaningless. | ||
@@ -204,0 +227,0 @@ const refFileNames = current.kind === 'SWAGGER' ? getReferencedFileNames(current.path, document) : []; |
{ | ||
"name": "@azure/avocado", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "A validator of OpenAPI configurations", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -75,2 +75,8 @@ # Avocado | ||
### INCONSISTENT_API_VERSION | ||
Level: ERROR | ||
Swagger json file api version must consistent with its file path. Swagger can define [swagger 2.0 basic-structure](https://swagger.io/docs/specification/2-0/basic-structure/) which contains api version. To solve the error, you need modify either your swagger file location or swagger file api version to make both of them consistent. | ||
### CIRCULAR REFERENCE | ||
@@ -77,0 +83,0 @@ |
@@ -14,2 +14,3 @@ // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| 'Can not find readme.md in the folder. If no readme.md file, it will block SDK generation.' | ||
| 'The API version of the swagger is inconsistent with its file path.' | ||
@@ -58,3 +59,3 @@ export interface IErrorBase { | ||
*/ | ||
readonly code: 'NO_JSON_FILE_FOUND' | 'UNREFERENCED_JSON_FILE' | 'CIRCULAR_REFERENCE' | ||
readonly code: 'NO_JSON_FILE_FOUND' | 'UNREFERENCED_JSON_FILE' | 'CIRCULAR_REFERENCE' | 'INCONSISTENT_API_VERSION' | ||
/** | ||
@@ -61,0 +62,0 @@ * Error message. |
@@ -52,2 +52,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. | ||
} | ||
case 'INCONSISTENT_API_VERSION': { | ||
return { | ||
code: error.code, | ||
url: error.jsonUrl, | ||
} | ||
} | ||
} | ||
@@ -163,2 +169,18 @@ } | ||
const validateSpecificationAPIVersion = (current: Specification, document: json.JsonObject): it.IterableEx<err.Error> => | ||
it.iterable<err.Error>(function*() { | ||
const info = document.info as json.JsonObject | undefined | ||
if (info !== undefined) { | ||
if (!current.path.includes(info.version as string)) { | ||
yield { | ||
code: 'INCONSISTENT_API_VERSION', | ||
level: 'Error', | ||
message: 'The API version of the swagger is inconsistent with its file path.', | ||
jsonUrl: current.path, | ||
readMeUrl: current.readMePath, | ||
} | ||
} | ||
} | ||
}) | ||
/** | ||
@@ -183,3 +205,3 @@ * Validate each RP folder must have its readme file. | ||
) | ||
.map(filepath => path.dirname(filepath)) | ||
.map(filePath => path.dirname(filePath)) | ||
@@ -256,2 +278,6 @@ const allJsonSet = new Set<string>() | ||
if (current.kind === 'SWAGGER' && document !== null) { | ||
yield* validateSpecificationAPIVersion(current, document as json.JsonObject) | ||
} | ||
// Example file should ignore `$ref` because it's usually meaningless. | ||
@@ -258,0 +284,0 @@ const refFileNames = current.kind === 'SWAGGER' ? getReferencedFileNames(current.path, document) : [] |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
89822
3.58%1540
3.22%126
5%