@@ -10,6 +10,7 @@ import (
1010 "fmt"
1111 "net/http"
1212 "net/http/httptest"
13+ "net/url"
1314 "testing"
1415
1516 "github.com/pb33f/libopenapi"
@@ -1301,6 +1302,69 @@ paths:
13011302 assert .Len (t , errors , 0 )
13021303}
13031304
1305+ // https://github.com/pb33f/libopenapi-validator/issues/107
1306+ // https://github.com/pb33f/libopenapi-validator/issues/103
1307+ func TestNewValidator_TestCircularRefsInValidation_Response (t * testing.T ) {
1308+ spec := `openapi: 3.1.0
1309+ info:
1310+ title: Panic at response validation
1311+ version: 1.0.0
1312+ paths:
1313+ /operations:
1314+ delete:
1315+ description: Delete operations
1316+ responses:
1317+ default:
1318+ description: Any response
1319+ content:
1320+ application/json:
1321+ schema:
1322+ $ref: '#/components/schemas/Error'
1323+
1324+ components:
1325+ schemas:
1326+ Error:
1327+ type: object
1328+ properties:
1329+ code:
1330+ type: string
1331+ details:
1332+ type: array
1333+ items:
1334+ $ref: '#/components/schemas/Error'`
1335+
1336+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
1337+
1338+ m , _ := doc .BuildV3Model ()
1339+ v := NewResponseBodyValidator (& m .Model )
1340+
1341+ req := & http.Request {
1342+ Method : http .MethodDelete ,
1343+ URL : & url.URL {
1344+ Path : "/operations" ,
1345+ },
1346+ }
1347+ // simulate a request/response
1348+ res := httptest .NewRecorder ()
1349+ handler := func (w http.ResponseWriter , r * http.Request ) {
1350+ w .Header ().Set (helpers .ContentTypeHeader , helpers .JSONContentType )
1351+ w .WriteHeader (http .StatusOK )
1352+ _ , _ = w .Write (nil )
1353+ }
1354+
1355+ // fire the request
1356+ handler (res , req )
1357+
1358+ // record response
1359+ response := res .Result ()
1360+
1361+ valid , errors := v .ValidateResponseBody (req , response )
1362+
1363+ assert .False (t , valid )
1364+ assert .Len (t , errors , 1 )
1365+ assert .Equal (t , "cannot render circular reference: #/components/schemas/Error" , errors [0 ].Reason )
1366+ }
1367+
13041368type errorReader struct {}
13051369
13061370func (er * errorReader ) Read (p []byte ) (n int , err error ) {
0 commit comments