Skip to content

Commit

Permalink
Merge branch 'CiscoDevNet:main' into DT_update-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolb committed Aug 26, 2024
2 parents 6ad153b + 7bdad66 commit 28959c9
Show file tree
Hide file tree
Showing 4 changed files with 543 additions and 0 deletions.
2 changes: 2 additions & 0 deletions documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export default {
'$.paths.*.*.responses.links.*',
'$.*.securitySchemes.*',
'$.securityDefinitions.*',
'$.components.schemas.[*].properties.*',
'$.components.schemas.*',
],
'then': {
'function': ensureField,
Expand Down
131 changes: 131 additions & 0 deletions test/description-for-every-attribute.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Copyright 2022 Cisco Systems, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import fsPromises from 'fs/promises';
import path from 'path';
import { prepLinter } from '../util/testUtils';
import ruleset from '../documentation';
const ruleName = 'description-for-every-attribute';
const resPath = path.join(__dirname, `resources/${ ruleName }`);

describe(ruleName, () => {
let spectral;

beforeAll(() => {
spectral = prepLinter(ruleset, ruleName);
});
test('should throw an error if missing description', async () => {
const spec = await fsPromises.readFile(`${ resPath }/negative.json`);
const res = await spectral.run(spec.toString());

expect(res).toEqual([
{
code: ruleName,
message: 'For every attribute that is present in the OAS document, if a description is proposed as optional to complement that attribute, then yes it must be present; description is missing in the object',
path: [
'paths',
'/test',
'get',
'parameters',
'0',
],
range: {
end: {
character: 29,
line: 36,
},
start: {
character: 11,
line: 29,
},
},
severity: 0,
},
{
code: ruleName,
message: 'For every attribute that is present in the OAS document, if a description is proposed as optional to complement that attribute, then yes it must be present; description is missing in the object',
path: [
'paths',
'/test',
'get',
'responses',
'200',
],
range: {
end: {
character: 32,
line: 72,
},
start: {
character: 16,
line: 41,
},
},
severity: 0,
},
{
code: ruleName,
message: 'For every attribute that is present in the OAS document, if a description is proposed as optional to complement that attribute, then yes it must be present; description is missing in the object',
path: [
'components',
'schemas',
'Pet',
],
range: {
end: {
character: 28,
line: 160,
},
start: {
character: 12,
line: 143,
},
},
severity: 0,
},
{
code: ruleName,
message: 'For every attribute that is present in the OAS document, if a description is proposed as optional to complement that attribute, then yes it must be present; description is missing in the object',
path: [
'components',
'schemas',
'Error',
'properties',
'message',
],
range: {
end: {
character: 28,
line: 184,
},
start: {
character: 20,
line: 183,
},
},
severity: 0,
},
]);
});
test('should pass with provided description', async () => {
const spec = await fsPromises.readFile(`${ resPath }/positive.json`);
const res = await spectral.run(spec.toString());

expect(res).toEqual([]);
});
});
203 changes: 203 additions & 0 deletions test/resources/description-for-every-attribute/negative.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
{
"openapi": "3.0.3",
"info": {
"title": "Sample API",
"description": "This is a sample API.",
"version": "1.0",
"contact": {
"name": "Dan Hayduk"
}
},
"servers": [
{
"url": "http://api.example.com"
}
],
"tags": [
{
"name": "Sample",
"description": "This is a sample tag."
}
],
"paths": {
"/test": {
"get": {
"operationId": "getTestData",
"tags": [
"Sample"
],
"parameters": [
{
"name": "limit",
"in": "query",
"required": false,
"schema": {
"type": "integer",
"format": "int32",
"example": 1
}
}
],
"responses": {
"200": {
"headers": {
"X-RateLimit-Limit": {
"schema": {
"type": "integer",
"example": 1
}
}
},
"content": {
"application/json": {
"schema": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"example": {
"id": 1,
"name": "tom",
"tag": "man"
}
}
}
}
},
"500": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"patch": {
"description": "Modify some test data.",
"operationId": "patchTestData",
"tags": [
"Sample"
],
"requestBody": {
"description": "descllla",
"content": {
"application/merge-patch+json": {
"schema": {
"type": "string",
"example": "s"
}
},
"application/json": {
"schema": {
"type": "string",
"example": "s"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "string",
"example": "s"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}

},

"components": {
"schemas": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"description": "desc",
"type": "integer",
"format": "int64"
},
"name": {
"description": "desc",
"type": "string"
},
"tag": {
"description": "desc",
"type": "string"
}
}
},
"Pets": {
"description": "desc",
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
},
"Error": {
"description": "desc",
"required": [
"code",
"message"
],
"properties": {
"code": {
"description": "desc",
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
},
"example": {
"code": 2,
"message": "m"
}
}
},
"securitySchemes": {
"api_key": {
"description": "desc",
"in": "header",
"name": "api_key",
"type": "apiKey"
}
}
}
}
Loading

0 comments on commit 28959c9

Please sign in to comment.