@@ -3,47 +3,105 @@ import { assert } from 'chai';
3
3
import { mediaTypes , isSchemaElement , OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1' ;
4
4
import { evaluate } from '@swagger-api/apidom-json-pointer' ;
5
5
6
- import { parse , dereferenceApiDOM } from '../../../../../src' ;
6
+ import { parse , dereferenceApiDOM , Reference , ReferenceSet } from '../../../../../src' ;
7
7
8
8
describe ( 'dereference' , function ( ) {
9
9
context ( 'strategies' , function ( ) {
10
10
context ( 'openapi-3-1' , function ( ) {
11
11
context ( 'Schema Object' , function ( ) {
12
- context ( 'given single SchemaElement passed to dereferenceApiDOM' , function ( ) {
13
- const fixturePath = path . join ( __dirname , 'fixtures' , 'external-only' , 'root.json' ) ;
12
+ context (
13
+ 'given single SchemaElement passed to dereferenceApiDOM with internal references' ,
14
+ function ( ) {
15
+ const fixturePath = path . join ( __dirname , 'fixtures' , 'internal-only' , 'root.json' ) ;
14
16
15
- specify ( 'should dereference' , async function ( ) {
16
- const parseResult = await parse ( fixturePath , {
17
- parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
18
- } ) ;
19
- const schemaElement = evaluate (
20
- '/components/schemas/User/properties/profile' ,
21
- parseResult . api as OpenApi3_1Element ,
22
- ) ;
23
- const dereferenced = await dereferenceApiDOM ( schemaElement , {
24
- parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
25
- resolve : { baseURI : fixturePath } ,
17
+ specify ( 'should dereference' , async function ( ) {
18
+ const parseResult = await parse ( fixturePath , {
19
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
20
+ } ) ;
21
+ const schemaElement = evaluate (
22
+ '/components/schemas/User/properties/profile' ,
23
+ parseResult . api as OpenApi3_1Element ,
24
+ ) ;
25
+ const reference = Reference ( { uri : fixturePath , parseResult } ) ;
26
+ const refSet = ReferenceSet ( { refs : [ reference ] } ) ;
27
+ // @ts -ignore
28
+ refSet . rootRef = null ;
29
+
30
+ const dereferenced = await dereferenceApiDOM ( schemaElement , {
31
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
32
+ resolve : { baseURI : `${ fixturePath } #/components/schemas/User/properties/profile` } ,
33
+ } ) ;
34
+
35
+ assert . isTrue ( isSchemaElement ( dereferenced ) ) ;
26
36
} ) ;
27
37
28
- assert . isTrue ( isSchemaElement ( dereferenced ) ) ;
29
- } ) ;
38
+ specify ( 'should dereference and contain metadata about origin' , async function ( ) {
39
+ const parseResult = await parse ( fixturePath , {
40
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
41
+ } ) ;
42
+ const schemaElement = evaluate (
43
+ '/components/schemas/User/properties/profile' ,
44
+ parseResult . api as OpenApi3_1Element ,
45
+ ) ;
46
+ const reference = Reference ( { uri : fixturePath , parseResult } ) ;
47
+ const refSet = ReferenceSet ( { refs : [ reference ] } ) ;
48
+ // @ts -ignore
49
+ refSet . rootRef = null ;
30
50
31
- specify ( 'should dereference and contain metadata about origin' , async function ( ) {
32
- const parseResult = await parse ( fixturePath , {
33
- parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
51
+ const dereferenced = await dereferenceApiDOM ( schemaElement , {
52
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
53
+ resolve : { baseURI : `${ fixturePath } #/components/schemas/User/properties/profile` } ,
54
+ } ) ;
55
+
56
+ assert . match (
57
+ dereferenced . meta . get ( 'ref-origin' ) . toValue ( ) ,
58
+ / i n t e r n a l - o n l y \/ r o o t \. j s o n $ / ,
59
+ ) ;
34
60
} ) ;
35
- const pathItemElement = evaluate (
36
- '/components/schemas/User/properties/profile' ,
37
- parseResult . api as OpenApi3_1Element ,
38
- ) ;
39
- const dereferenced = await dereferenceApiDOM ( pathItemElement , {
40
- parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
41
- resolve : { baseURI : fixturePath } ,
61
+ } ,
62
+ ) ;
63
+
64
+ context (
65
+ 'given single SchemaElement passed to dereferenceApiDOM with external references' ,
66
+ function ( ) {
67
+ const fixturePath = path . join ( __dirname , 'fixtures' , 'external-only' , 'root.json' ) ;
68
+
69
+ specify ( 'should dereference' , async function ( ) {
70
+ const parseResult = await parse ( fixturePath , {
71
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
72
+ } ) ;
73
+ const schemaElement = evaluate (
74
+ '/components/schemas/User/properties/profile' ,
75
+ parseResult . api as OpenApi3_1Element ,
76
+ ) ;
77
+ const dereferenced = await dereferenceApiDOM ( schemaElement , {
78
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
79
+ resolve : { baseURI : fixturePath } ,
80
+ } ) ;
81
+
82
+ assert . isTrue ( isSchemaElement ( dereferenced ) ) ;
42
83
} ) ;
43
84
44
- assert . match ( dereferenced . meta . get ( 'ref-origin' ) . toValue ( ) , / e x t e r n a l - o n l y \/ e x \. j s o n $ / ) ;
45
- } ) ;
46
- } ) ;
85
+ specify ( 'should dereference and contain metadata about origin' , async function ( ) {
86
+ const parseResult = await parse ( fixturePath , {
87
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
88
+ } ) ;
89
+ const pathItemElement = evaluate (
90
+ '/components/schemas/User/properties/profile' ,
91
+ parseResult . api as OpenApi3_1Element ,
92
+ ) ;
93
+ const dereferenced = await dereferenceApiDOM ( pathItemElement , {
94
+ parse : { mediaType : mediaTypes . latest ( 'json' ) } ,
95
+ resolve : { baseURI : fixturePath } ,
96
+ } ) ;
97
+
98
+ assert . match (
99
+ dereferenced . meta . get ( 'ref-origin' ) . toValue ( ) ,
100
+ / e x t e r n a l - o n l y \/ e x \. j s o n $ / ,
101
+ ) ;
102
+ } ) ;
103
+ } ,
104
+ ) ;
47
105
} ) ;
48
106
} ) ;
49
107
} ) ;
0 commit comments