-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oas): add rule to detect duplicated entry in enum (#1485)
- Loading branch information
Showing
5 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,117 @@ | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import * as ruleset from '../index.json'; | ||
import { RuleType, Spectral } from '../../../spectral'; | ||
|
||
describe('duplicated-entry-in-enum', () => { | ||
const s = new Spectral(); | ||
s.setRules({ | ||
'duplicated-entry-in-enum': Object.assign(ruleset.rules['duplicated-entry-in-enum'], { | ||
recommended: true, | ||
type: RuleType[ruleset.rules['duplicated-entry-in-enum'].type], | ||
}), | ||
}); | ||
|
||
describe('oas2', () => { | ||
test('does not report anything for empty object', async () => { | ||
const results = await s.run({ | ||
swagger: '2.0', | ||
}); | ||
|
||
expect(results).toEqual([]); | ||
}); | ||
|
||
test('does not report anything when the model valid', async () => { | ||
const doc = { | ||
swagger: '2.0', | ||
definitions: { | ||
Test: { | ||
type: 'integer', | ||
enum: [1, 2, 3], | ||
}, | ||
}, | ||
}; | ||
|
||
const results = await s.run(doc); | ||
|
||
expect(results).toEqual([]); | ||
}); | ||
|
||
test('identifies enum with duplicated entries', async () => { | ||
const doc = { | ||
swagger: '2.0', | ||
definitions: { | ||
Test: { | ||
type: 'integer', | ||
enum: [1, 2, 3, 4, 5, 2], | ||
}, | ||
}, | ||
}; | ||
|
||
const results = await s.run(doc); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
code: 'duplicated-entry-in-enum', | ||
message: `A duplicated entry in the enum was found. Error: \`enum\` property should not have duplicate items (items ## 1 and 5 are identical)`, | ||
path: ['definitions', 'Test', 'enum'], | ||
range: expect.any(Object), | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('oas3', () => { | ||
test('does not report anything for empty object', async () => { | ||
const results = await s.run({ | ||
openapi: '3.0.0', | ||
}); | ||
|
||
expect(results).toEqual([]); | ||
}); | ||
|
||
test('does not report anything when the model is valid', async () => { | ||
const doc = { | ||
openapi: '3.0.0', | ||
components: { | ||
schemas: { | ||
Test: { | ||
type: 'integer', | ||
enum: [1, 2, 3], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = await s.run(doc); | ||
|
||
expect(results).toEqual([]); | ||
}); | ||
|
||
test('identifies enum with duplicated entries', async () => { | ||
const doc = { | ||
openapi: '3.0.0', | ||
components: { | ||
schemas: { | ||
Test: { | ||
type: 'integer', | ||
enum: [1, 2, 3, 4, 5, 2], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const results = await s.run(doc); | ||
|
||
expect(results).toEqual([ | ||
{ | ||
code: 'duplicated-entry-in-enum', | ||
message: `A duplicated entry in the enum was found. Error: \`enum\` property should not have duplicate items (items ## 1 and 5 are identical)`, | ||
path: ['components', 'schemas', 'Test', 'enum'], | ||
range: expect.any(Object), | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains 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
33 changes: 33 additions & 0 deletions
33
test-harness/scenarios/duplicated-entry-in-enum.oas.scenario
This file contains 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,33 @@ | ||
====test==== | ||
Identify enum values that does not respect the specified type | ||
====asset:ruleset==== | ||
extends: [[spectral:oas, off]] | ||
|
||
rules: | ||
duplicated-entry-in-enum: error | ||
====document==== | ||
openapi: 3.0.2 | ||
components: | ||
schemas: | ||
a_model: | ||
type: object | ||
properties: | ||
id: | ||
type: integer | ||
description: Unique asset identifier | ||
enum: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
- 2 | ||
====command==== | ||
{bin} lint {document} --ruleset {asset:ruleset} | ||
====stdout==== | ||
OpenAPI 3.x detected | ||
|
||
{document} | ||
10:16 error duplicated-entry-in-enum A duplicated entry in the enum was found. Error: `enum` property should not have duplicate items (items ## 1 and 5 are identical) components.schemas.a_model.properties.id.enum | ||
|
||
✖ 1 problem (1 error, 0 warnings, 0 infos, 0 hints) |
This file contains 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