-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP 306294: Add x-xgen-method-verb-override extension logic #817
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
Changes from 17 commits
0d7775d
c45f169
79da6ec
3f502ee
875b880
e00ebcf
794456c
d3deaae
9ae2c93
3879992
ea69c29
00a2052
1c864be
6c2f9fc
9680213
a15ca0e
c95204c
e5f2adc
c1dff45
eca80ae
a4d1db9
dc29321
30fdbac
c5cb6aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { describe, it, expect, toBe } from '@jest/globals'; | ||
import { | ||
hasMethodWithVerbOverride, | ||
hasCustomMethodOverride, | ||
hasMethodVerbOverride, | ||
} from '../../rulesets/functions/utils/extensions'; | ||
|
||
const methodWithExtension = { | ||
'x-xgen-method-verb-override': { | ||
verb: 'get', | ||
customMethod: false, | ||
}, | ||
}; | ||
|
||
const customMethod = { | ||
'x-xgen-method-verb-override': { | ||
verb: 'add', | ||
customMethod: true, | ||
}, | ||
}; | ||
|
||
const endpointWithMethodExtension = { | ||
delete: { | ||
'x-xgen-method-verb-override': { verb: '‘remove’', customMethod: true }, | ||
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
}; | ||
|
||
const endpointWithNoMethodExtension = { | ||
exception: true, | ||
}; | ||
|
||
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => { | ||
describe('hasMethodWithVerbOverride', () => { | ||
it('returns true if endpoint has method with the extension', () => { | ||
expect(hasMethodWithVerbOverride(endpointWithMethodExtension)).toBe(true); | ||
}); | ||
it('returns false if object does not a method with the extension', () => { | ||
expect(hasMethodWithVerbOverride(endpointWithNoMethodExtension)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => { | ||
describe('hasCustomMethodOverride', () => { | ||
it('returns true if the method has the extension with the cusotmMethod boolean set to true', () => { | ||
expect(hasCustomMethodOverride(customMethod)).toBe(true); | ||
}); | ||
it('returns false if the method does not have the extension', () => { | ||
expect(hasCustomMethodOverride({})).toBe(false); | ||
}); | ||
it('returns false if the method has the extension but is not a custom method', () => { | ||
expect(hasCustomMethodOverride(methodWithExtension)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => { | ||
describe('hasMethodVerbOverride', () => { | ||
it('returns true if the method has the extension with the expected verb', () => { | ||
expect(hasMethodVerbOverride(methodWithExtension, 'get')).toBe(true); | ||
}); | ||
it('returns false if the method does not have the extension', () => { | ||
expect(hasMethodVerbOverride({}, 'get')).toBe(false); | ||
}); | ||
it('returns false if the method has the extension but with an unexpected verb', () => { | ||
expect(hasMethodVerbOverride(methodWithExtension, 'put')).toBe(false); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,43 +2,67 @@ import { hasException } from './utils/exceptions.js'; | |||||||||||||||
import { collectAdoption, collectException, collectAndReturnViolation } from './utils/collectionUtils.js'; | ||||||||||||||||
import { isCustomMethodIdentifier, getCustomMethodName, stripCustomMethodName } from './utils/resourceEvaluation.js'; | ||||||||||||||||
import { generateOperationID } from './utils/operationIdGeneration.js'; | ||||||||||||||||
import { hasMethodWithVerbOverride, hasCustomMethodOverride } from './utils/extensions.js'; | ||||||||||||||||
|
||||||||||||||||
const RULE_NAME = 'xgen-IPA-109-valid-operation-id'; | ||||||||||||||||
const ERROR_MESSAGE = 'Invalid OperationID.'; | ||||||||||||||||
|
||||||||||||||||
export default (input, _, { path }) => { | ||||||||||||||||
let resourcePath = path[1]; | ||||||||||||||||
const methodName = getCustomMethodName(resourcePath); | ||||||||||||||||
|
||||||||||||||||
if (!isCustomMethodIdentifier(resourcePath)) { | ||||||||||||||||
if (!isCustomMethodIdentifier(resourcePath) && !hasMethodWithVerbOverride(input)) { | ||||||||||||||||
return; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// TODO detect custom method extension - CLOUDP-306294 | ||||||||||||||||
let errors = []; | ||||||||||||||||
let expectedOperationID = ''; | ||||||||||||||||
if (isCustomMethodIdentifier(resourcePath)) { | ||||||||||||||||
expectedOperationID = generateOperationID(getCustomMethodName(resourcePath), stripCustomMethodName(resourcePath)); | ||||||||||||||||
|
let expectedOperationID = ''; | |
if (isCustomMethodIdentifier(resourcePath)) { | |
expectedOperationID = generateOperationID(getCustomMethodName(resourcePath), stripCustomMethodName(resourcePath)); | |
if (isCustomMethodIdentifier(resourcePath)) { | |
const expectedOperationID = generateOperationID(getCustomMethodName(resourcePath), stripCustomMethodName(resourcePath)); |
Since you are only using expectedOperationID
in the scope of each if block, you can declare it within. the block itself and assign
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.
Same with errors
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.
Pushed a refactor, thank you!
sphterry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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 (let i = 0; i < methods.length; i++) { | |
let obj = input[methods[i]]; | |
const operationId = obj.operationId; | |
methods.forEach((method) => { | |
const operationId = method.operationId | |
... | |
}) |
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.
Tip: JS has a lot of fun functions on Arrays, so that you rarely need to use for loops: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 🚀
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export const VERB_OVERRIDE_EXTENSION = 'x-xgen-method-verb-override'; | ||
|
||
/** | ||
* Checks if the endpoint has a method with an extension "x-xgen-method-verb-override" | ||
* | ||
* @param endpoint the endpoint to evaluate | ||
* @returns {boolean} true if the endpoint has a nested method with the extension, otherwise false | ||
*/ | ||
export function hasMethodWithVerbOverride(endpoint) { | ||
const keys = Object.keys(endpoint); | ||
return keys.contains(VERB_OVERRIDE_EXTENSION) | ||
return false; | ||
} | ||
|
||
/** | ||
* Checks if the object has an extension "x-xgen-method-verb-override" with the customMethod boolean set to true | ||
* | ||
* @param object the object to evaluate | ||
* @returns {boolean} true if the object has an extension with customMethod=True, otherwise false | ||
*/ | ||
export function hasCustomMethodOverride(object) { | ||
return hasVerbOverride(object)) && object[VERB_OVERRIDE_EXTENSION].customMethod; | ||
} | ||
|
||
/** | ||
* Checks if the object has an extension "x-xgen-method-verb-override" with the verb set to a specific verb | ||
* | ||
* @param object the object to evaluate | ||
* @param verb the verb to inspect the extension for | ||
* @returns {boolean} true if the object has the extension with the given verb, otherwise false | ||
*/ | ||
return hasVerbOverride(object) && object[VERB_OVERRIDE_EXTENSION].verb === verb | ||
|
||
/** | ||
* Checks if the object has an extension "x-xgen-method-verb-override" | ||
* | ||
* @param object the object to evaluate | ||
* @returns {boolean} true if the object has the extension, otherwise false | ||
*/ | ||
function hasVerbOverride(object) { | ||
if (!object[VERB_OVERRIDE_EXTENSION]) { | ||
return false; | ||
} | ||
return true; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.