@@ -14,12 +14,45 @@ import (
1414 "github.com/stretchr/testify/assert"
1515)
1616
17- func TestValidateBody_MissingContentType (t * testing.T ) {
17+ func TestValidateBody_NotRequiredBody (t * testing.T ) {
1818 spec := `openapi: 3.1.0
1919paths:
2020 /burgers/createBurger:
2121 post:
2222 requestBody:
23+ required: false
24+ content:
25+ application/json:
26+ schema:
27+ type: object
28+ properties:
29+ name:
30+ type: string
31+ patties:
32+ type: integer
33+ vegetarian:
34+ type: boolean`
35+
36+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
37+
38+ m , _ := doc .BuildV3Model ()
39+ v := NewRequestBodyValidator (& m .Model )
40+
41+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" , http .NoBody )
42+
43+ valid , errors := v .ValidateRequestBody (request )
44+
45+ assert .True (t , valid )
46+ assert .Len (t , errors , 0 )
47+ }
48+
49+ func TestValidateBody_UnknownContentType (t * testing.T ) {
50+ spec := `openapi: 3.1.0
51+ paths:
52+ /burgers/createBurger:
53+ post:
54+ requestBody:
55+ required: true
2356 content:
2457 application/json:
2558 schema:
@@ -252,6 +285,7 @@ paths:
252285 /burgers/createBurger:
253286 post:
254287 requestBody:
288+ required: true
255289 content:
256290 application/json:
257291 schema:
@@ -299,6 +333,7 @@ paths:
299333 /burgers/createBurger:
300334 post:
301335 requestBody:
336+ required: true
302337 content:
303338 application/json:
304339 schema:
@@ -338,12 +373,62 @@ paths:
338373
339374}
340375
341- func TestValidateBody_InvalidBasicSchema (t * testing.T ) {
376+ func TestValidateBody_InvalidBasicSchema_NotRequired (t * testing.T ) {
377+ spec := `openapi: 3.1.0
378+ paths:
379+ /burgers/createBurger:
380+ post:
381+ requestBody:
382+ required: false
383+ content:
384+ application/json:
385+ schema:
386+ type: object
387+ properties:
388+ name:
389+ type: string
390+ patties:
391+ type: integer
392+ vegetarian:
393+ type: boolean`
394+
395+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
396+
397+ m , _ := doc .BuildV3Model ()
398+ v := NewRequestBodyValidator (& m .Model )
399+
400+ // mix up the primitives to fire two schema violations.
401+ body := map [string ]interface {}{
402+ "name" : "Big Mac" ,
403+ "patties" : false ,
404+ "vegetarian" : 2 ,
405+ }
406+
407+ bodyBytes , _ := json .Marshal (body )
408+
409+ request , _ := http .NewRequest (http .MethodPost , "https://things.com/burgers/createBurger" ,
410+ bytes .NewBuffer (bodyBytes ))
411+ request .Header .Set ("Content-Type" , "application/json" )
412+
413+ valid , errors := v .ValidateRequestBody (request )
414+
415+ // double-tap to hit the cache
416+ _ , _ = v .ValidateRequestBody (request )
417+
418+ assert .False (t , valid )
419+ assert .Len (t , errors , 1 )
420+ assert .Len (t , errors [0 ].SchemaValidationErrors , 2 )
421+ assert .Equal (t , "POST request body for '/burgers/createBurger' failed to validate schema" , errors [0 ].Message )
422+
423+ }
424+
425+ func TestValidateBody_InvalidBasicSchema_Required (t * testing.T ) {
342426 spec := `openapi: 3.1.0
343427paths:
344428 /burgers/createBurger:
345429 post:
346430 requestBody:
431+ required: false
347432 content:
348433 application/json:
349434 schema:
0 commit comments