@@ -2,15 +2,10 @@ const expect = require('expect');
2
2
const {
3
3
validate
4
4
} = require ( '../../../../src/plugins/validation/oas3/semantic-validators/operations' ) ;
5
+ const config = require ( '../../../../src/.defaultsForValidator' ) . defaults . oas3 ;
5
6
6
7
describe ( 'validation plugin - semantic - operations - oas3' , function ( ) {
7
8
it ( 'should complain about a request body not having a content field' , function ( ) {
8
- const config = {
9
- operations : {
10
- no_request_body_content : 'error'
11
- }
12
- } ;
13
-
14
9
const spec = {
15
10
paths : {
16
11
'/pets' : {
@@ -25,12 +20,173 @@ describe('validation plugin - semantic - operations - oas3', function() {
25
20
}
26
21
} ;
27
22
28
- const res = validate ( { resolvedSpec : spec } , config ) ;
23
+ const res = validate ( { resolvedSpec : spec , jsSpec : spec } , config ) ;
29
24
expect ( res . errors . length ) . toEqual ( 1 ) ;
30
25
expect ( res . errors [ 0 ] . path ) . toEqual ( 'paths./pets.post.requestBody' ) ;
31
26
expect ( res . errors [ 0 ] . message ) . toEqual (
32
27
'Request bodies MUST specify a `content` property'
33
28
) ;
34
29
expect ( res . warnings . length ) . toEqual ( 0 ) ;
35
30
} ) ;
31
+
32
+ it ( 'should warn about an operation with a non-form, array schema request body that does not set a name' , function ( ) {
33
+ const spec = {
34
+ paths : {
35
+ '/pets' : {
36
+ post : {
37
+ summary : 'this is a summary' ,
38
+ operationId : 'operationId' ,
39
+ requestBody : {
40
+ description : 'body for request' ,
41
+ content : {
42
+ 'application/json' : {
43
+ schema : {
44
+ type : 'array' ,
45
+ items : {
46
+ type : 'string'
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+ } ;
56
+
57
+ const res = validate ( { resolvedSpec : spec , jsSpec : spec } , config ) ;
58
+ expect ( res . warnings . length ) . toEqual ( 1 ) ;
59
+ expect ( res . warnings [ 0 ] . path ) . toEqual ( 'paths./pets.post' ) ;
60
+ expect ( res . warnings [ 0 ] . message ) . toEqual (
61
+ 'Operations with non-form request bodies should set a name with the x-codegen-request-body-name annotation.'
62
+ ) ;
63
+ expect ( res . errors . length ) . toEqual ( 0 ) ;
64
+ } ) ;
65
+
66
+ it ( 'should not warn about an operation with a non-array json request body that does not set a name' , function ( ) {
67
+ const spec = {
68
+ paths : {
69
+ '/pets' : {
70
+ post : {
71
+ summary : 'this is a summary' ,
72
+ operationId : 'operationId' ,
73
+ requestBody : {
74
+ description : 'body for request' ,
75
+ content : {
76
+ 'application/json' : {
77
+ schema : {
78
+ type : 'string'
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ } ;
87
+
88
+ const res = validate ( { resolvedSpec : spec , jsSpec : spec } , config ) ;
89
+ expect ( res . warnings . length ) . toEqual ( 0 ) ;
90
+ expect ( res . errors . length ) . toEqual ( 0 ) ;
91
+ } ) ;
92
+
93
+ it ( 'should not warn about an operation with a non-form request body that sets a name' , function ( ) {
94
+ const spec = {
95
+ paths : {
96
+ '/pets' : {
97
+ post : {
98
+ 'x-codegen-request-body-name' : 'goodRequestBody' ,
99
+ summary : 'this is a summary' ,
100
+ operationId : 'operationId' ,
101
+ requestBody : {
102
+ description : 'body for request' ,
103
+ content : {
104
+ 'application/json' : {
105
+ schema : {
106
+ type : 'string'
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
114
+ } ;
115
+
116
+ const res = validate ( { resolvedSpec : spec , jsSpec : spec } , config ) ;
117
+ expect ( res . warnings . length ) . toEqual ( 0 ) ;
118
+ expect ( res . errors . length ) . toEqual ( 0 ) ;
119
+ } ) ;
120
+
121
+ it ( 'should not warn about an operation with a form request body that does not set a name' , function ( ) {
122
+ const spec = {
123
+ paths : {
124
+ '/pets' : {
125
+ post : {
126
+ summary : 'this is a summary' ,
127
+ operationId : 'operationId' ,
128
+ requestBody : {
129
+ description : 'body for request' ,
130
+ content : {
131
+ 'multipart/form-data' : {
132
+ schema : {
133
+ type : 'object' ,
134
+ properties : {
135
+ name : {
136
+ type : 'string'
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ } ;
147
+
148
+ const res = validate ( { resolvedSpec : spec , jsSpec : spec } , config ) ;
149
+ expect ( res . warnings . length ) . toEqual ( 0 ) ;
150
+ expect ( res . errors . length ) . toEqual ( 0 ) ;
151
+ } ) ;
152
+
153
+ it ( 'should not warn about an operation with a referenced request body that does not set a name' , function ( ) {
154
+ const resolvedSpec = {
155
+ paths : {
156
+ '/pets' : {
157
+ post : {
158
+ summary : 'this is a summary' ,
159
+ operationId : 'operationId' ,
160
+ requestBody : {
161
+ content : {
162
+ 'application/json' : {
163
+ schema : {
164
+ type : 'string'
165
+ }
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ } ;
173
+
174
+ const jsSpec = {
175
+ paths : {
176
+ '/pets' : {
177
+ post : {
178
+ summary : 'this is a summary' ,
179
+ operationId : 'operationId' ,
180
+ requestBody : {
181
+ $ref : '#/components/requestBodies/SomeBody'
182
+ }
183
+ }
184
+ }
185
+ }
186
+ } ;
187
+
188
+ const res = validate ( { resolvedSpec, jsSpec } , config ) ;
189
+ expect ( res . warnings . length ) . toEqual ( 0 ) ;
190
+ expect ( res . errors . length ) . toEqual ( 0 ) ;
191
+ } ) ;
36
192
} ) ;
0 commit comments