-
Couldn't load subscription status.
- Fork 14
CLOUDP-306573: IPA rule - plaintext repsonse has example #628
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: [], | ||
| }, | ||
| ]); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ functions: | |
| - IPA117DescriptionMustNotUseHtml | ||
| - IPA117DescriptionShouldNotUseTables | ||
| - IPA117DescriptionShouldNotUseLinks | ||
| - IPA117PlaintextResponseMustHaveExample | ||
|
|
||
| rules: | ||
| xgen-IPA-117-description: | ||
|
|
@@ -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' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'] | ||
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️