Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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-oneOf-must-accompany-oneOf-anyOf-allOf', [
{
name: 'valid schemas',
document: {
components: {
schemas: {
SchemaOneOf: {
discriminator: {
propertyName: 'type',
},
oneOf: [{}],
},
SchemaAnuOf: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit] 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-oneOf-must-accompany-oneOf-anyOf-allOf',
message: "Each discriminator property must be accompanied by a 'oneOf', 'anyOf' or 'allOf' property.",
path: ['components', 'schemas', 'Schema', 'discriminator'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-125-oneOf-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', 'discriminator'],
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-oneOf-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-oneOf-must-accompany-oneOf-anyOf-allOf': 'reason',
},
},
address: {
type: 'string',
},
},
},
},
},
},
errors: [],
},
]);
13 changes: 13 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-125.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
functions:
- IPA125OneOfMustHaveDiscriminator
- IPA125OneOfNoBaseTypes
- IPA125DiscriminatorMustAccompanyOneOfAnyOfAllOf

rules:
xgen-IPA-125-oneOf-must-have-discriminator:
Expand Down Expand Up @@ -63,3 +64,15 @@ rules:
given: '$.components.schemas[*]'
then:
function: 'IPA125OneOfNoBaseTypes'

xgen-IPA-125-oneOf-must-accompany-oneOf-anyOf-allOf:
description: |
Each discriminator property must be accompanied by a `oneOf`, `anyOf` or `allOf` property

##### Implementation details
- Rule checks that a `discriminator` property has a `oneOf`, `anyOf` or `allOf` sibling
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-125-oneOf-must-accompany-oneOf-anyOf-allOf'
severity: warn
given: '$..discriminator'
then:
function: 'IPA125DiscriminatorMustAccompanyOneOfAnyOfAllOf'
8 changes: 8 additions & 0 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,14 @@ Using oneOf with multiple primitive types can lead to ambiguity and validation p
be able to properly determine which type to use in which context. Instead, use more specific
object types with clear discriminators.

#### xgen-IPA-125-oneOf-must-accompany-oneOf-anyOf-allOf

![warn](https://img.shields.io/badge/warning-yellow)
Each discriminator property must be accompanied by a `oneOf`, `anyOf` or `allOf` property

##### Implementation details
- Rule checks that a `discriminator` property has a `oneOf`, `anyOf` or `allOf` sibling



### IPA-126
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 siblings = resolveObject(documentInventory.resolved, path.slice(0, path.length - 1));

const errors = checkViolationsAndReturnErrors(input, path, Object.keys(siblings));
return evaluateAndCollectAdoptionStatus(errors, rule.name, siblings, path);
};

function checkViolationsAndReturnErrors(input, path, siblingKeys) {
Copy link
Collaborator

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?

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, 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 the discriminator

Copy link
Collaborator

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

if (!siblingKeys.includes('oneOf') && !siblingKeys.includes('anyOf') && !siblingKeys.includes('allOf')) {
return [
{
path,
message: ERROR_MESSAGE,
},
];
}
return [];
}
Loading