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
5 changes: 1 addition & 4 deletions .github/workflows/spectral-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,5 @@ jobs:
file_glob: openapi/.raw/v2.yaml
spectral_ruleset: tools/spectral/.spectral.yaml #If updated, need to update in MMS too.
- name: IPA validation action
uses: stoplightio/spectral-action@2ad0b9302e32a77c1caccf474a9b2191a8060d83
with:
file_glob: v2.yaml
spectral_ruleset: tools/spectral/ipa/ipa-spectral.yaml
run: npx spectral lint v2.yaml --ruleset=./tools/spectral/ipa/ipa-spectral.yaml
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️


76,143 changes: 76,143 additions & 0 deletions outputs/v2-dev.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-117-plaintext-response-must-have-example', [
{
name: 'valid responses',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+csv': {
example: 'test',
schema: {},
},
'text/plain': {
schema: {
example: 'test',
type: 'string',
},
},
},
},
// Ignores non-2xx
400: {
content: {
'text/plain': {
schema: {},
},
},
},
},
},
},
// Ignores JSON/YAML
'/resourceTwo': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2024-08-05+json': {
type: 'string',
},
'application/vnd.atlas.2024-08-05+yaml': {
type: 'string',
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid resources',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2022-01-01+csv': {
schema: {},
},
'application/vnd.atlas.2023-01-01+csv': {
schema: {},
},
'text/plain': {
schema: {
type: 'string',
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-plaintext-response-must-have-example',
message: 'For APIs that respond with plain text, for example CSV, API producers must provide an example.',
path: ['paths', '/resource', 'get', 'responses', '200', 'content', 'application/vnd.atlas.2022-01-01+csv'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-plaintext-response-must-have-example',
message: 'For APIs that respond with plain text, for example CSV, API producers must provide an example.',
path: ['paths', '/resource', 'get', 'responses', '200', 'content', 'application/vnd.atlas.2023-01-01+csv'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-plaintext-response-must-have-example',
message: 'For APIs that respond with plain text, for example CSV, API producers must provide an example.',
path: ['paths', '/resource', 'get', 'responses', '200', 'content', 'text/plain'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid resources with exceptions',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2022-01-01+csv': {
schema: {},
'x-xgen-IPA-exception': {
'xgen-IPA-117-plaintext-response-must-have-example': 'reason',
},
},
'application/vnd.atlas.2023-01-01+csv': {
schema: {},
'x-xgen-IPA-exception': {
'xgen-IPA-117-plaintext-response-must-have-example': 'reason',
},
},
'text/plain': {
schema: {
type: 'string',
},
'x-xgen-IPA-exception': {
'xgen-IPA-117-plaintext-response-must-have-example': 'reason',
},
},
},
},
},
},
},
},
},
errors: [],
},
]);
18 changes: 18 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-117.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ functions:
- IPA117DescriptionMustNotUseHtml
- IPA117DescriptionShouldNotUseTables
- IPA117DescriptionShouldNotUseLinks
- IPA117PlaintextResponseMustHaveExample

rules:
xgen-IPA-117-description:
Expand Down Expand Up @@ -157,3 +158,20 @@ rules:
- '$.components.parameters[*]'
then:
function: 'IPA117DescriptionShouldNotUseLinks'
xgen-IPA-117-plaintext-response-must-have-example:
description: |
For APIs that respond with plain text, for example CSV, API producers must provide an example. Some tools are not able to generate examples for such responses

##### Implementation details
- The rule only applies to 2xx responses
- The rule ignores JSON and YAML responses (passed as `allowedTypes`)
- The rule checks for the presence of the example property as a sibling to the `schema` property, or inside the `schema` object
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-plaintext-response-must-have-example'
severity: warn
given:
- '$.paths[*][get,put,post,delete,options,head,patch,trace].responses[*].content'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q: I know this comes a long way but.. Do we need to specify them? Do you remember what is the reason behind specifying HTTP methods?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, if we don't specify them this will also target other things like extension properties as well. For this line specifically the risk is pretty low to have an extension with responses and content, but just as a safe-guard

then:
field: '@key'
function: 'IPA117PlaintextResponseMustHaveExample'
functionOptions:
allowedTypes: ['json', 'yaml']
10 changes: 10 additions & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,16 @@ Rule checks the format of the descriptions for components:
- Schema properties
The rule validates that the description content does not include inline markdown links. The rule ignores HTML `<a>` links - this is covered by `xgen-IPA-117-description-must-not-use-html`.

#### xgen-IPA-117-plaintext-response-must-have-example

![warn](https://img.shields.io/badge/warning-yellow)
For APIs that respond with plain text, for example CSV, API producers must provide an example. Some tools are not able to generate examples for such responses

##### Implementation details
- The rule only applies to 2xx responses
- The rule ignores JSON and YAML responses (passed as `allowedTypes`)
- The rule checks for the presence of the example property as a sibling to the `schema` property, or inside the `schema` object



### IPA-123
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { hasException } from './utils/exceptions.js';
import {
collectAdoption,
collectAndReturnViolation,
collectException,
handleInternalError,
} from './utils/collectionUtils.js';
import { resolveObject } from './utils/componentUtils.js';

const RULE_NAME = 'xgen-IPA-117-plaintext-response-must-have-example';
const ERROR_MESSAGE = 'For APIs that respond with plain text, for example CSV, API producers must provide an example.';

/**
* For APIs that respond with plain text, for example CSV, API producers must provide an example.
*
* @param {object} input - A response media type
* @param {{allowedTypes: string[]}} opts - Allowed type suffixes, if the type ends with one of these, it is ignored
* @param {object} context - The context object containing the path and documentInventory
*/
export default (input, { allowedTypes }, { documentInventory, path }) => {
const responseCode = path[4];

// Ignore non-2xx responses
if (!responseCode.startsWith('2')) {
return;
}

// Ignore allowed types
if (allowedTypes.some((type) => input.endsWith(type))) {
return;
}

const response = resolveObject(documentInventory.resolved, path);

if (hasException(response, RULE_NAME)) {
collectException(response, RULE_NAME, path);
return;
}

const errors = checkViolationsAndReturnErrors(response, path);
if (errors.length !== 0) {
return collectAndReturnViolation(path, RULE_NAME, errors);
}
collectAdoption(path, RULE_NAME);
};

function checkViolationsAndReturnErrors(response, path) {
try {
if (response['example'] || (response['schema'] && response['schema']['example'])) {
return [];
}
return [{ path, message: ERROR_MESSAGE }];
} catch (e) {
handleInternalError(RULE_NAME, path, e);
}
}
Loading