-
Couldn't load subscription status.
- Fork 14
CLOUDP-272001: IPA-112: Disallow usages of project in the api #566
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
234 changes: 234 additions & 0 deletions
234
tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-112-avoid-project-field-names', [ | ||
| { | ||
| name: 'valid schema - no project field names', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| group: { type: 'string' }, | ||
| groupId: { type: 'string' }, | ||
| otherField: { type: 'number' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid schema - with project field name', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| project: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "project" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'project'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid schema - with projects field name', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| projects: { type: 'array' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "projects" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid schema - with projectId field name', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| projectId: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "projectId" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid schema - with different case variants', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| Project: { type: 'string' }, | ||
| PROJECTID: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "Project" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid schema with exception - project field name with exception', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| project: { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-avoid-project-field-names': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'field name containing project substring', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| myProjectDetails: { type: 'object' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "myProjectDetails" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'myProjectDetails'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'exception - field with project substring', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| myProjectDetails: { | ||
| type: 'object', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-avoid-project-field-names': 'Reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'exception - multiple project fields', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| projectId: { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-avoid-project-field-names': 'Reason', | ||
| }, | ||
| }, | ||
| projects: { | ||
| type: 'array', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-avoid-project-field-names': 'Reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'mixed valid, invalid, and exception fields', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| project: { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-avoid-project-field-names': 'Reason', | ||
| }, | ||
| }, | ||
| projectId: { type: 'string' }, | ||
| group: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-avoid-project-field-names', | ||
| message: 'Field name "projectId" should be avoided. Consider using "group" instead.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # IPA-112: Field Names | ||
| # http://go/ipa/112 | ||
|
|
||
| functions: | ||
| - IPA112AvoidProjectFieldNames | ||
|
|
||
| rules: | ||
| xgen-IPA-112-avoid-project-field-names: | ||
| description: | | ||
| Schema field names should avoid using "project", "projects", or "projectId". | ||
|
|
||
| ##### Implementation details | ||
| Rule checks for the following conditions: | ||
| - Searches through all schemas in the API definition | ||
| - Identifies property names that match "project" (case-insensitive) | ||
| - Reports any instances where these field names are used | ||
| - Suggests using "group", "groups", or "groupId" as alternatives | ||
| message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-avoid-project-field-names' | ||
| severity: warn | ||
| given: '$.components.schemas..properties[*]~' | ||
| then: | ||
| function: 'IPA112AvoidProjectFieldNames' | ||
| functionOptions: | ||
| prohibitedFieldNames: | ||
| - name: 'project' | ||
| alternative: ['group'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 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-112-avoid-project-field-names'; | ||
|
|
||
| export default (input, options, { path, documentInventory }) => { | ||
| const oas = documentInventory.resolved; | ||
| const property = resolveObject(oas, path); | ||
|
|
||
| if (hasException(property, RULE_NAME)) { | ||
| collectException(property, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(input, options, path); | ||
| if (errors.length !== 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolationsAndReturnErrors(input, options, path) { | ||
| try { | ||
| const prohibitedFieldNames = options?.prohibitedFieldNames || []; | ||
| const lowerPropertyName = input.toLowerCase(); | ||
|
|
||
| // Check if the property name includes any of the prohibited terms | ||
| for (const prohibitedItem of prohibitedFieldNames) { | ||
| const prohibitedName = prohibitedItem.name || ''; | ||
| const alternative = prohibitedItem.alternative || ''; | ||
|
|
||
| if (lowerPropertyName.includes(prohibitedName.toLowerCase())) { | ||
| return [ | ||
| { | ||
| path, | ||
| message: `Field name "${input}" should be avoided. Consider using ${alternative.map((alt) => `"${alt}"`)} instead.`, | ||
| }, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| return []; | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, path, e); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Q: Is it possible to have exceptions on properties? IIRC when we did the enum rule we had to have the exception on the schema
Uh oh!
There was an error while loading. Please reload this page.
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.
For enum one, we have the exceptions on the property level. Ex:
which is equivalent of
Schemaannotation for the property itself on Java. I kept the same level for exception. Are you asking keeping the exception level for Schema annotation level but for the schema level?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.
Ah thanks for clarifying, I misremembered and thought it was on the schema level higher up. LGTM 👍