-
Notifications
You must be signed in to change notification settings - Fork 14
feat(ipa): new IPA rule xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf #893
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
9 commits
Select commit
Hold shift + click to select a range
4436ed7
feat(ipa): new IPA rule xgen-IPA-125-oneOf-must-accompany-oneOf-anyOf…
lovisaberggren e72e2d5
fix: README
lovisaberggren fcf03ca
fix: exception level
lovisaberggren 2851d73
fix: naming
lovisaberggren 0bc337f
fix: ipa docs
lovisaberggren 68f718b
merge conflict
lovisaberggren 6c473ea
fix: format
lovisaberggren e048cf3
fix
lovisaberggren d23d11c
fix: metric path
lovisaberggren 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
128 changes: 128 additions & 0 deletions
128
tools/spectral/ipa/__tests__/IPA125DiscriminatorMustAccompanyOneOfAnyOfAllOf.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,128 @@ | ||
import testRule from './__helpers__/testRule.js'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
testRule('xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', [ | ||
{ | ||
name: 'valid schemas', | ||
document: { | ||
components: { | ||
schemas: { | ||
SchemaOneOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
oneOf: [{}], | ||
}, | ||
SchemaAnyOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
anyOf: [{}], | ||
}, | ||
SchemaAllOf: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
allOf: [{}], | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid schemas', | ||
document: { | ||
components: { | ||
schemas: { | ||
Schema: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
properties: { | ||
type: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
NestedSchema: { | ||
properties: { | ||
name: { | ||
type: 'object', | ||
discriminator: { | ||
propertyName: 'first', | ||
}, | ||
properties: { | ||
first: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
address: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', | ||
message: "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property.", | ||
path: ['components', 'schemas', 'Schema'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf', | ||
message: "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property.", | ||
path: ['components', 'schemas', 'NestedSchema', 'properties', 'name'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'invalid schemas with exceptions', | ||
document: { | ||
components: { | ||
schemas: { | ||
Schema: { | ||
discriminator: { | ||
propertyName: 'type', | ||
}, | ||
properties: { | ||
type: { | ||
type: 'string', | ||
}, | ||
}, | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf': 'reason', | ||
}, | ||
}, | ||
NestedSchema: { | ||
properties: { | ||
name: { | ||
type: 'object', | ||
discriminator: { | ||
propertyName: 'first', | ||
}, | ||
properties: { | ||
first: { | ||
type: 'string', | ||
}, | ||
}, | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-125-discriminator-must-accompany-oneOf-anyOf-allOf': 'reason', | ||
}, | ||
}, | ||
address: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
]); |
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
24 changes: 24 additions & 0 deletions
24
tools/spectral/ipa/rulesets/functions/IPA125DiscriminatorMustAccompanyOneOfAnyOfAllOf.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,24 @@ | ||
import { evaluateAndCollectAdoptionStatus } from './utils/collectionUtils.js'; | ||
import { resolveObject } from './utils/componentUtils.js'; | ||
|
||
const ERROR_MESSAGE = "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property."; | ||
|
||
export default (input, _, { path, documentInventory, rule }) => { | ||
const siblingPath = path.slice(0, path.length - 1); | ||
const siblings = resolveObject(documentInventory.resolved, siblingPath); | ||
|
||
const errors = checkViolationsAndReturnErrors(input, siblingPath, Object.keys(siblings)); | ||
return evaluateAndCollectAdoptionStatus(errors, rule.name, siblings, siblingPath); | ||
}; | ||
|
||
function checkViolationsAndReturnErrors(input, path, siblingKeys) { | ||
if (!siblingKeys.includes('oneOf') && !siblingKeys.includes('anyOf') && !siblingKeys.includes('allOf')) { | ||
return [ | ||
{ | ||
path, | ||
message: ERROR_MESSAGE, | ||
}, | ||
]; | ||
} | ||
return []; | ||
} |
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.
Which Swagger component annotation level are we able to define exceptions? Is it Schema?
Should we update the component level for metrics collection?
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.
Yes, the schema. The exceptions are siblings to the discriminator since discriminators cannot have extensions.
Now the metric path is
['components', 'schemas', 'SchemaName', 'discriminator']
- no strong opinion to have it or remove thediscriminator
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.
I think we can remove the
discriminator
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.
Done!