@@ -6,9 +6,13 @@ package validator
66import (
77 "bytes"
88 "encoding/json"
9+ "fmt"
10+ "io"
911 "net/http"
1012 "net/http/httptest"
13+ "net/url"
1114 "os"
15+ "strings"
1216 "sync"
1317 "testing"
1418
@@ -1539,3 +1543,150 @@ paths:
15391543 assert .True (t , valid )
15401544 assert .Len (t , errors , 0 )
15411545}
1546+
1547+ // https://github.com/pb33f/libopenapi-validator/issues/107
1548+ func TestNewValidator_TestCircularRefsInValidation_Request (t * testing.T ) {
1549+
1550+ spec := `openapi: 3.1.0
1551+ info:
1552+ title: Panic at response validation
1553+ version: 1.0.0
1554+ paths:
1555+ /operations:
1556+ delete:
1557+ description: Delete operations
1558+ responses:
1559+ default:
1560+ description: Any response
1561+ content:
1562+ application/json:
1563+ schema:
1564+ $ref: '#/components/schemas/Error'
1565+
1566+ components:
1567+ schemas:
1568+ Error:
1569+ type: object
1570+ properties:
1571+ code:
1572+ type: string
1573+ details:
1574+ type: array
1575+ items:
1576+ $ref: '#/components/schemas/Error'`
1577+
1578+ document , err := libopenapi .NewDocument ([]byte (spec ))
1579+ if err != nil {
1580+ panic (fmt .Sprintf ("failed to create new document: %v\n " , err ))
1581+ }
1582+
1583+ model , errs := document .BuildV3Model ()
1584+ circ := model .Index .GetCircularReferences ()
1585+ fmt .Println ("Circular references: " , len (circ ))
1586+
1587+ if len (errs ) > 0 {
1588+ for i := range errs {
1589+ fmt .Printf ("model error: %e\n " , errs [i ])
1590+ }
1591+ panic (fmt .Sprintf ("failed to create v3 model from document: %d errors reported" , len (errs )))
1592+ }
1593+
1594+ fmt .Println ("Successfully parsed OpenAPI spec" )
1595+
1596+ oapiValidator , errs := NewValidator (document )
1597+ if errs != nil {
1598+ panic (fmt .Sprintf ("failed to create validator: %v" , errs ))
1599+ }
1600+ if ok , errs := oapiValidator .ValidateDocument (); ! ok {
1601+ panic (fmt .Sprintf ("document validation errors: %v" , errs ))
1602+ }
1603+
1604+ req := & http.Request {
1605+ Method : http .MethodDelete ,
1606+ URL : & url.URL {
1607+ Path : "/operations" ,
1608+ },
1609+ }
1610+ res := & http.Response {
1611+ StatusCode : http .StatusOK ,
1612+ Header : map [string ][]string {
1613+ "Content-Type" : {"application/json" },
1614+ },
1615+ Body : io .NopCloser (strings .NewReader (`{"code":"abc","details":[{"code":"def"}]}` )),
1616+ }
1617+ if ok , errs := oapiValidator .ValidateHttpResponse (req , res ); ! ok {
1618+ assert .Equal (t , 1 , len (errs ))
1619+ assert .Equal (t , "cannot render circular reference: #/components/schemas/Error" , errs [0 ].Reason )
1620+
1621+ }
1622+ }
1623+
1624+ // https://github.com/pb33f/libopenapi-validator/issues/107
1625+ func TestNewValidator_TestCircularRefsInValidation_Response (t * testing.T ) {
1626+
1627+ spec := `openapi: 3.1.0
1628+ info:
1629+ title: Panic at response validation
1630+ version: 1.0.0
1631+ paths:
1632+ /operations:
1633+ delete:
1634+ description: Delete operations
1635+ responses:
1636+ default:
1637+ description: Any response
1638+ content:
1639+ application/json:
1640+ schema:
1641+ $ref: '#/components/schemas/Error'
1642+
1643+ components:
1644+ schemas:
1645+ Error:
1646+ type: object
1647+ properties:
1648+ code:
1649+ type: string
1650+ details:
1651+ type: array
1652+ items:
1653+ $ref: '#/components/schemas/Error'`
1654+
1655+ document , err := libopenapi .NewDocument ([]byte (spec ))
1656+ if err != nil {
1657+ panic (fmt .Sprintf ("failed to create new document: %v\n " , err ))
1658+ }
1659+
1660+ model , errs := document .BuildV3Model ()
1661+ circ := model .Index .GetCircularReferences ()
1662+ fmt .Println ("Circular references: " , len (circ ))
1663+
1664+ if len (errs ) > 0 {
1665+ for i := range errs {
1666+ fmt .Printf ("model error: %e\n " , errs [i ])
1667+ }
1668+ panic (fmt .Sprintf ("failed to create v3 model from document: %d errors reported" , len (errs )))
1669+ }
1670+
1671+ fmt .Println ("Successfully parsed OpenAPI spec" )
1672+
1673+ oapiValidator , errs := NewValidator (document )
1674+ if errs != nil {
1675+ panic (fmt .Sprintf ("failed to create validator: %v" , errs ))
1676+ }
1677+ if ok , errs := oapiValidator .ValidateDocument (); ! ok {
1678+ panic (fmt .Sprintf ("document validation errors: %v" , errs ))
1679+ }
1680+
1681+ req := & http.Request {
1682+ Method : http .MethodDelete ,
1683+ URL : & url.URL {
1684+ Path : "/operations" ,
1685+ },
1686+ }
1687+ if ok , errs := oapiValidator .ValidateHttpRequest (req ); ! ok {
1688+ assert .Equal (t , 1 , len (errs ))
1689+ assert .Equal (t , "cannot render circular reference: #/components/schemas/Error" , errs [0 ].Reason )
1690+
1691+ }
1692+ }
0 commit comments