-
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 Reference Object
Refs #3097
- Loading branch information
Showing
13 changed files
with
186 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,19 @@ | ||
import { StringElement, ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core'; | ||
|
||
class Reference extends ObjectElement { | ||
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) { | ||
super(content, meta, attributes); | ||
this.element = 'reference'; | ||
this.classes.push('openapi-reference'); | ||
} | ||
|
||
get $ref(): StringElement | undefined { | ||
return this.get('$ref'); | ||
} | ||
|
||
set $ref($ref: StringElement | undefined) { | ||
this.set('$ref', $ref); | ||
} | ||
} | ||
|
||
export default Reference; |
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
17 changes: 17 additions & 0 deletions
17
packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/reference/$RefVisitor.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,17 @@ | ||
import stampit from 'stampit'; | ||
import { StringElement, BREAK, cloneDeep } from '@swagger-api/apidom-core'; | ||
|
||
import FallbackVisitor from '../../FallbackVisitor'; | ||
|
||
const $RefVisitor = stampit(FallbackVisitor, { | ||
methods: { | ||
StringElement(stringElement: StringElement) { | ||
this.element = cloneDeep(stringElement); | ||
this.element.classes.push('reference-value'); | ||
|
||
return BREAK; | ||
}, | ||
}, | ||
}); | ||
|
||
export default $RefVisitor; |
32 changes: 32 additions & 0 deletions
32
packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/reference/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,32 @@ | ||
import stampit from 'stampit'; | ||
import { always } from 'ramda'; | ||
import { ObjectElement, isStringElement } from '@swagger-api/apidom-core'; | ||
|
||
import ReferenceElement from '../../../../elements/Reference'; | ||
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor'; | ||
import FallbackVisitor from '../../FallbackVisitor'; | ||
|
||
const ReferenceVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, { | ||
props: { | ||
specPath: always(['document', 'objects', 'Reference']), | ||
canSupportSpecificationExtensions: false, | ||
}, | ||
init() { | ||
this.element = new ReferenceElement(); | ||
}, | ||
methods: { | ||
ObjectElement(objectElement: ObjectElement) { | ||
// @ts-ignore | ||
const result = FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement); | ||
|
||
// mark this ReferenceElement with reference metadata | ||
if (isStringElement(this.element.$ref)) { | ||
this.element.classes.push('reference-element'); | ||
} | ||
|
||
return result; | ||
}, | ||
}, | ||
}); | ||
|
||
export default ReferenceVisitor; |
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
8 changes: 8 additions & 0 deletions
8
packages/apidom-ns-openapi-2/test/refractor/elements/Reference/__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,8 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`refractor elements ReferenceElement should refract to semantic ApiDOM tree 1`] = ` | ||
(ReferenceElement | ||
(MemberElement | ||
(StringElement) | ||
(StringElement))) | ||
`; |
18 changes: 18 additions & 0 deletions
18
packages/apidom-ns-openapi-2/test/refractor/elements/Reference/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 { expect } from 'chai'; | ||
import { sexprs } from '@swagger-api/apidom-core'; | ||
|
||
import { ReferenceElement } from '../../../../src'; | ||
|
||
describe('refractor', function () { | ||
context('elements', function () { | ||
context('ReferenceElement', function () { | ||
specify('should refract to semantic ApiDOM tree', function () { | ||
const referenceElement = ReferenceElement.refract({ | ||
$ref: '#/path/to/somewhere', | ||
}); | ||
|
||
expect(sexprs(referenceElement)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}); |