This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseReferenceObject-test.js
173 lines (136 loc) · 5.8 KB
/
parseReferenceObject-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { Fury } = require('fury');
const { expect } = require('../../chai');
const parse = require('../../../../lib/parser/oas/parseReferenceObject');
const Context = require('../../../../lib/context');
const { minim: namespace } = new Fury();
describe('Reference Object', () => {
let context;
let dataStructure;
beforeEach(() => {
context = new Context(namespace);
dataStructure = new namespace.elements.DataStructure();
dataStructure.id = 'Node';
dataStructure.content = new namespace.elements.Object();
context.state.components = new namespace.elements.Object({
schemas: {
Node: dataStructure,
},
});
});
it('errors when parsing non-string $ref', () => {
const reference = new namespace.elements.Object({
$ref: true,
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error("'Reference Object' '$ref' is not a string");
});
it('can parse a reference', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult.length).to.equal(1);
const structure = parseResult.get(0);
expect(structure).to.equal(dataStructure);
});
it('can parse a reference and return reference', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
});
const parseResult = parse(context, 'schemas', reference, true);
expect(parseResult.length).to.equal(1);
const structure = parseResult.get(0);
expect(structure.element).to.equal('Node');
expect(structure).to.be.instanceof(namespace.elements.Object);
});
it('can parse a reference to a component with multiple values', () => {
context.state.components.set('requestBodies', new namespace.elements.Object([
new namespace.elements.Member('Example', new namespace.elements.HttpRequest()),
new namespace.elements.Member('Example', new namespace.elements.HttpRequest()),
]));
const reference = new namespace.elements.Object({
$ref: '#/components/requestBodies/Example',
});
const parseResult = parse(context, 'requestBodies', reference);
expect(parseResult.length).to.equal(2);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.HttpRequest);
expect(parseResult.get(1)).to.be.instanceof(namespace.elements.HttpRequest);
});
it('can parse a reference to a component that could not be parsed', () => {
context.state.components = new namespace.elements.Object({
schemas: {
Node: undefined,
},
});
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult.length).to.equal(0);
});
describe('invalid references', () => {
it('errors when parsing a non-components reference', () => {
const reference = new namespace.elements.Object({
$ref: '#/info/title',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error("Only local references to '#/components' within the same file are supported");
});
it('errors when parsing reference to a nonexistent component', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/parameters/Node',
});
const parseResult = parse(context, 'parameters', reference);
expect(parseResult).to.contain.error("'#/components/parameters' is not defined");
});
it('errors when parsing reference to incorrect component name', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/parameters/Node',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error("Only references to 'schemas' are permitted from this location");
});
it('errors when parsing reference to nonexistent property in component', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/BaseNode',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error("'#/components/schemas/BaseNode' is not defined");
});
it('errors when parsing reference thats too deep', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node/inside',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error(
"Only references to a reusable component inside '#/components/schemas' are supported"
);
});
it('errors when parsing reference to nonexistent components', () => {
context.state.components = undefined;
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.error("'#/components' is not defined");
});
});
describe('warnings for invalid properties', () => {
it('provides warning for invalid keys', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
invalid: {},
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.warning("'Reference Object' contains invalid key 'invalid'");
});
it('provides warning for extensions', () => {
const reference = new namespace.elements.Object({
$ref: '#/components/schemas/Node',
'x-extension': {},
});
const parseResult = parse(context, 'schemas', reference);
expect(parseResult).to.contain.warning("Extensions are not permitted in 'Reference Object'");
});
});
});