-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-306580: IPA 125 - Each oneOf property must be accompanied with discriminator #572
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
4 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
185 changes: 185 additions & 0 deletions
185
tools/spectral/ipa/__tests__/IPA125OneOfMustHaveDiscriminator.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,185 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| const componentSchemas = { | ||
| schemas: { | ||
| Dog: { | ||
| type: 'object', | ||
| properties: { | ||
| breed: { type: 'string' }, | ||
| age: { type: 'integer' }, | ||
| }, | ||
| }, | ||
| Cat: { | ||
| type: 'object', | ||
| properties: { | ||
| color: { type: 'string' }, | ||
| livesLeft: { type: 'integer' }, | ||
| }, | ||
| }, | ||
| Bird: { | ||
| type: 'object', | ||
| properties: { | ||
| species: { type: 'string' }, | ||
| wingspan: { type: 'number' }, | ||
| }, | ||
| }, | ||
| Fish: { | ||
| type: 'object', | ||
| properties: { | ||
| species: { type: 'string' }, | ||
| waterType: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| testRule('xgen-IPA-125-oneOf-must-have-discriminator', [ | ||
| { | ||
| name: 'valid oneOf with discriminator and matching mapping', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| discriminator: { | ||
| propertyName: 'type', | ||
| mapping: { | ||
| dog: '#/components/schemas/Dog', | ||
| cat: '#/components/schemas/Cat', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid oneOf with discriminator but mismatched mapping', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| discriminator: { | ||
| propertyName: 'type', | ||
| mapping: { | ||
| dog: '#/components/schemas/Dog', | ||
| bird: '#/components/schemas/Bird', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-125-oneOf-must-have-discriminator', | ||
| message: | ||
| 'The discriminator mapping must match the oneOf references. Unmatched Discriminator mappings with oneOf references: #/components/schemas/Bird', | ||
| path: ['schemas', 'Animal'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid oneOf without discriminator', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-125-oneOf-must-have-discriminator', | ||
| message: 'The schema has oneOf but no discriminator property.', | ||
| path: ['schemas', 'Animal'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid oneOf with non-object discriminator', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| discriminator: "I'm a string, not an object!", | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-125-oneOf-must-have-discriminator', | ||
| message: 'Discriminator property is not an object.', | ||
| path: ['schemas', 'Animal'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid oneOf with discriminator but no propertyName', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| discriminator: { | ||
| mapping: { | ||
| dog: '#/components/schemas/Dog', | ||
| cat: '#/components/schemas/Cat', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-125-oneOf-must-have-discriminator', | ||
| message: 'Discriminator has no propertyName defined.', | ||
| path: ['schemas', 'Animal'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid oneOf with discriminator but no mapping', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| discriminator: { | ||
| propertyName: 'type', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-125-oneOf-must-have-discriminator', | ||
| message: 'Discriminator must have a mapping object.', | ||
| path: ['schemas', 'Animal'], | ||
| severity: DiagnosticSeverity.Error, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'oneOf with discriminator exemption', | ||
| document: { | ||
| components: componentSchemas, | ||
| schemas: { | ||
| Animal: { | ||
| oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }], | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-125-oneOf-must-have-discriminator': 'reason for exemption', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # IPA-125: Single Type in Request and Response | ||
| # http://go/ipa/125 | ||
|
|
||
| functions: | ||
| - IPA125OneOfMustHaveDiscriminator | ||
|
|
||
| rules: | ||
| xgen-IPA-125-oneOf-must-have-discriminator: | ||
| description: | | ||
| Each oneOf property must include a discriminator property to define the exact type. | ||
|
|
||
| ##### Implementation details | ||
| Rule checks for the following conditions: | ||
| - Applies only to schemas with `oneOf` containing references | ||
| - Ensures a `discriminator` property is present with a valid `propertyName` | ||
| - Validates that `discriminator.mapping` contains exactly the same number of entries as `oneOf` references | ||
| - Validates that each `discriminator.mapping` value matches a reference in the `oneOf` array | ||
| - Ignores `oneOf` definitions with inline schemas | ||
|
|
||
| ##### Matching Logic | ||
| - The `discriminator.mapping` must have the same number of entries as there are references in the `oneOf` array | ||
| - Each value in the `discriminator.mapping` must match one of the `$ref` values in the `oneOf` array | ||
| - Each `$ref` in the `oneOf` array must have a corresponding entry in the `discriminator.mapping` | ||
| - Example: | ||
| ```yaml | ||
| oneOf: | ||
| - $ref: '#/components/schemas/Dog' | ||
| - $ref: '#/components/schemas/Cat' | ||
| discriminator: | ||
| propertyName: type | ||
| mapping: | ||
| dog: '#/components/schemas/Dog' | ||
| cat: '#/components/schemas/Cat' | ||
| ``` | ||
| This is valid because there are exactly 2 mappings for 2 oneOf references, and all values match. | ||
|
|
||
| message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-125-oneOf-must-have-discriminator' | ||
| severity: error | ||
| given: '$..[?(@.oneOf)]' | ||
| then: | ||
| function: 'IPA125OneOfMustHaveDiscriminator' |
61 changes: 61 additions & 0 deletions
61
tools/spectral/ipa/rulesets/functions/IPA125OneOfMustHaveDiscriminator.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,61 @@ | ||
| import { collectAdoption, collectAndReturnViolation } from './utils/collectionUtils.js'; | ||
| import { resolveObject } from './utils/componentUtils.js'; | ||
| import { hasException } from './utils/exceptions.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-125-oneOf-must-have-discriminator'; | ||
| const MISSING_DISCRIMINATOR_MESSAGE = 'The schema has oneOf but no discriminator property.'; | ||
| const INVALID_DISCRIMINATOR_MESSAGE = 'Discriminator property is not an object.'; | ||
| const MISSING_PROPERTY_NAME_MESSAGE = 'Discriminator has no propertyName defined.'; | ||
| const MISSING_MAPPING_MESSAGE = 'Discriminator must have a mapping object.'; | ||
| const MAPPING_ERROR_MESSAGE = | ||
| 'The discriminator mapping must match the oneOf references. Unmatched Discriminator mappings with oneOf references:'; | ||
|
|
||
| export default (input, _, { path, documentInventory }) => { | ||
| const oas = documentInventory.unresolved; // Use unresolved document to access raw $ref | ||
| const schema = resolveObject(oas, path); | ||
|
|
||
| if (!schema || !schema.oneOf || !Array.isArray(schema.oneOf)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check for exception first | ||
| if (hasException(schema, RULE_NAME)) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if all oneOf items are objects with a $ref property | ||
| const allReferences = schema.oneOf.every((item) => typeof item === 'object' && item.$ref); | ||
| if (!allReferences) { | ||
| return; | ||
| } | ||
|
|
||
| // Validate the presence of a discriminator with more specific error messages | ||
| if (!schema.discriminator) { | ||
| return collectAndReturnViolation(path, RULE_NAME, [{ message: MISSING_DISCRIMINATOR_MESSAGE }]); | ||
| } | ||
|
|
||
| if (typeof schema.discriminator !== 'object') { | ||
| return collectAndReturnViolation(path, RULE_NAME, [{ message: INVALID_DISCRIMINATOR_MESSAGE }]); | ||
| } | ||
|
|
||
| if (!schema.discriminator.propertyName) { | ||
| return collectAndReturnViolation(path, RULE_NAME, [{ message: MISSING_PROPERTY_NAME_MESSAGE }]); | ||
| } | ||
|
|
||
| if (!schema.discriminator.mapping) { | ||
| return collectAndReturnViolation(path, RULE_NAME, [{ message: MISSING_MAPPING_MESSAGE }]); | ||
| } | ||
|
|
||
| const oneOfRefs = schema.oneOf.map((item) => item.$ref); | ||
| const mappingValues = Object.values(schema.discriminator.mapping); | ||
|
|
||
| // Check for discriminator mappings that don't match any oneOf reference | ||
| const unmatchedMappings = mappingValues.filter((mapping) => !oneOfRefs.includes(mapping)); | ||
| if (unmatchedMappings.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, [ | ||
| { message: `${MAPPING_ERROR_MESSAGE} ${unmatchedMappings.join(', ')}` }, | ||
| ]); | ||
| } | ||
|
|
||
| collectAdoption(path, RULE_NAME); | ||
| }; |
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.
❤️