-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ns-openapi-2): add support for Scopes Object (#3226)
Refs #3097
- Loading branch information
Showing
11 changed files
with
224 additions
and
5 deletions.
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
10 changes: 10 additions & 0 deletions
10
packages/apidom-ns-openapi-2/src/elements/ScopesElement.ts
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,10 @@ | ||
import { ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core'; | ||
|
||
class Scopes extends ObjectElement { | ||
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) { | ||
super(content, meta, attributes); | ||
this.element = 'scopes'; | ||
} | ||
} | ||
|
||
export default Scopes; |
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
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
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
18 changes: 18 additions & 0 deletions
18
packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/scopes/index.ts
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,18 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
|
||
import ScopesElement from '../../../../elements/ScopesElement'; | ||
import MapVisitor from '../../generics/MapVisitor'; | ||
import FallbackVisitor from '../../FallbackVisitor'; | ||
|
||
const ScopesVisitor = stampit(MapVisitor, FallbackVisitor, { | ||
props: { | ||
specPath: always(['value']), | ||
canSupportSpecificationExtensions: true, | ||
}, | ||
init() { | ||
this.element = new ScopesElement(); | ||
}, | ||
}); | ||
|
||
export default ScopesVisitor; |
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,124 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { | ||
ScopesElement, | ||
SecurityRequirementElement, | ||
isScopesElement, | ||
isSecurityRequirementElement, | ||
} from '../src'; | ||
|
||
describe('predicates', function () { | ||
context('isScopesElement', function () { | ||
context('given ScopesElement instance value', function () { | ||
specify('should return true', function () { | ||
const element = new ScopesElement(); | ||
|
||
assert.isTrue(isScopesElement(element)); | ||
}); | ||
}); | ||
|
||
context('given subtype instance value', function () { | ||
specify('should return true', function () { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
class ScopesSubElement extends ScopesElement {} | ||
|
||
assert.isTrue(isScopesElement(new ScopesSubElement())); | ||
}); | ||
}); | ||
|
||
context('given non ScopesSubElement instance value', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(isScopesElement(1)); | ||
assert.isFalse(isScopesElement(null)); | ||
assert.isFalse(isScopesElement(undefined)); | ||
assert.isFalse(isScopesElement({})); | ||
assert.isFalse(isScopesElement([])); | ||
assert.isFalse(isScopesElement('string')); | ||
}); | ||
}); | ||
|
||
specify('should support duck-typing', function () { | ||
const scopesElementDuck = { | ||
_storedElement: 'scopes', | ||
_content: [], | ||
primitive() { | ||
return 'object'; | ||
}, | ||
get element() { | ||
return this._storedElement; | ||
}, | ||
}; | ||
|
||
const scopesElementSwan = { | ||
_storedElement: undefined, | ||
_content: undefined, | ||
primitive() { | ||
return 'swan'; | ||
}, | ||
get length() { | ||
return 0; | ||
}, | ||
}; | ||
|
||
assert.isTrue(isScopesElement(scopesElementDuck)); | ||
assert.isFalse(isScopesElement(scopesElementSwan)); | ||
}); | ||
}); | ||
|
||
context('isSecurityRequirementElement', function () { | ||
context('given SecurityRequirementElement instance value', function () { | ||
specify('should return true', function () { | ||
const element = new SecurityRequirementElement(); | ||
|
||
assert.isTrue(isSecurityRequirementElement(element)); | ||
}); | ||
}); | ||
|
||
context('given subtype instance value', function () { | ||
specify('should return true', function () { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
class SecurityRequirementSubElement extends SecurityRequirementElement {} | ||
|
||
assert.isTrue(isSecurityRequirementElement(new SecurityRequirementSubElement())); | ||
}); | ||
}); | ||
|
||
context('given non SecurityRequirementSubElement instance value', function () { | ||
specify('should return false', function () { | ||
assert.isFalse(isSecurityRequirementElement(1)); | ||
assert.isFalse(isSecurityRequirementElement(null)); | ||
assert.isFalse(isSecurityRequirementElement(undefined)); | ||
assert.isFalse(isSecurityRequirementElement({})); | ||
assert.isFalse(isSecurityRequirementElement([])); | ||
assert.isFalse(isSecurityRequirementElement('string')); | ||
}); | ||
}); | ||
|
||
specify('should support duck-typing', function () { | ||
const securityRequirementElementDuck = { | ||
_storedElement: 'securityRequirement', | ||
_content: [], | ||
primitive() { | ||
return 'object'; | ||
}, | ||
get element() { | ||
return this._storedElement; | ||
}, | ||
}; | ||
|
||
const securityRequirementElementSwan = { | ||
_storedElement: undefined, | ||
_content: undefined, | ||
primitive() { | ||
return 'swan'; | ||
}, | ||
get length() { | ||
return 0; | ||
}, | ||
}; | ||
|
||
assert.isTrue(isSecurityRequirementElement(securityRequirementElementDuck)); | ||
assert.isFalse(isSecurityRequirementElement(securityRequirementElementSwan)); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/apidom-ns-openapi-2/test/refractor/elements/Scopes/__snapshots__/index.ts.snap
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,14 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`refractor elements ScopesElement should refract to semantic ApiDOM tree 1`] = ` | ||
(ScopesElement | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)) | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)) | ||
(MemberElement | ||
(StringElement) | ||
(StringElement))) | ||
`; |
35 changes: 35 additions & 0 deletions
35
packages/apidom-ns-openapi-2/test/refractor/elements/Scopes/index.ts
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,35 @@ | ||
import { expect, assert } from 'chai'; | ||
import { sexprs, includesClasses } from '@swagger-api/apidom-core'; | ||
|
||
import { ScopesElement } from '../../../../src'; | ||
|
||
describe('refractor', function () { | ||
context('elements', function () { | ||
context('ScopesElement', function () { | ||
specify('should refract to semantic ApiDOM tree', function () { | ||
const scopesElement = ScopesElement.refract({ | ||
'write:pets': 'modify pets in your account', | ||
'read:pets': 'read your pets', | ||
'x-extension': 'extension', | ||
}); | ||
|
||
expect(sexprs(scopesElement)).toMatchSnapshot(); | ||
}); | ||
|
||
specify('should support specification extensions', function () { | ||
const scopesElement = ScopesElement.refract({ | ||
'write:pets': 'modify pets in your account', | ||
'read:pets': 'read your pets', | ||
'x-extension': 'extension', | ||
}) as ScopesElement; | ||
|
||
assert.isFalse( | ||
includesClasses(['specification-extension'], scopesElement.getMember('write:pets')), | ||
); | ||
assert.isTrue( | ||
includesClasses(['specification-extension'], scopesElement.getMember('x-extension')), | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |