@@ -149,8 +149,6 @@ func TestExpandResponseSchema(t *testing.T) {
149149
150150func TestSpecExpansion (t * testing.T ) {
151151 spec := new (Swagger )
152- // resolver, err := defaultSchemaLoader(spec, nil, nil,nil)
153- // assert.NoError(t, err)
154152
155153 err := ExpandSpec (spec , nil )
156154 assert .NoError (t , err )
@@ -208,6 +206,14 @@ func TestResolveRef(t *testing.T) {
208206 assert .NoError (t , err )
209207 b , _ := sch .MarshalJSON ()
210208 assert .JSONEq (t , `{"id":"Category","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}}}` , string (b ))
209+
210+ // WithBase variant
211+ sch , err = ResolveRefWithBase (root , & ref , & ExpandOptions {
212+ RelativeBase : "/" ,
213+ })
214+ assert .NoError (t , err )
215+ b , _ = sch .MarshalJSON ()
216+ assert .JSONEq (t , `{"id":"Category","properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"}}}` , string (b ))
211217}
212218
213219func TestResponseExpansion (t * testing.T ) {
@@ -229,11 +235,30 @@ func TestResponseExpansion(t *testing.T) {
229235 err = expandParameterOrResponse (& expected , resolver , basePath )
230236 assert .NoError (t , err )
231237
238+ jazon , _ := json .MarshalIndent (expected , "" , " " )
239+ assert .JSONEq (t , `{
240+ "description": "pet response",
241+ "schema": {
242+ "required": [
243+ "id",
244+ "name"
245+ ],
246+ "properties": {
247+ "id": {
248+ "type": "integer",
249+ "format": "int64"
250+ },
251+ "name": {
252+ "type": "string"
253+ },
254+ "tag": {
255+ "type": "string"
256+ }
257+ }
258+ }
259+ }` , string (jazon ))
260+
232261 err = expandParameterOrResponse (& resp , resolver , basePath )
233- // b, _ := resp.MarshalJSON()
234- // log.Printf(string(b))
235- // b, _ = expected.MarshalJSON()
236- // log.Printf(string(b))
237262 assert .NoError (t , err )
238263 assert .Equal (t , expected , resp )
239264
@@ -244,18 +269,45 @@ func TestResponseExpansion(t *testing.T) {
244269 assert .NoError (t , err )
245270 assert .Equal (t , expected , * resp2 )
246271
272+ // cascading ref
247273 resp = spec .Paths .Paths ["/" ].Get .Responses .StatusCodeResponses [200 ]
248274 expected = spec .Responses ["petResponse" ]
275+ jazon , _ = json .MarshalIndent (resp , "" , " " )
276+ assert .JSONEq (t , `{
277+ "$ref": "#/responses/anotherPet"
278+ }` , string (jazon ))
249279
250280 err = expandParameterOrResponse (& resp , resolver , basePath )
251281 assert .NoError (t , err )
252- // assert.Equal(t, expected, resp)
282+ // NOTE(fredbi): fixed this testcase in schema_loader.go#227
283+ assert .Equal (t , expected , resp )
284+ }
285+
286+ func TestResponseResolve (t * testing.T ) {
287+ specDoc , err := jsonDoc ("fixtures/expansion/all-the-things.json" )
288+ assert .NoError (t , err )
289+
290+ spec := new (Swagger )
291+ err = json .Unmarshal (specDoc , spec )
292+ assert .NoError (t , err )
293+
294+ // Resolve with root version
295+ resp := spec .Paths .Paths ["/" ].Get .Responses .StatusCodeResponses [200 ]
296+ resp2 , err := ResolveResponse (spec , resp .Ref )
297+ assert .NoError (t , err )
298+ // resolve resolves the ref, but dos not expand
299+ jazon , _ := json .MarshalIndent (resp2 , "" , " " )
300+ assert .JSONEq (t , `{
301+ "$ref": "#/responses/petResponse"
302+ }` , string (jazon ))
253303}
254304
255305// test the exported version of ExpandResponse
256306func TestExportedResponseExpansion (t * testing.T ) {
257307 specDoc , err := jsonDoc ("fixtures/expansion/all-the-things.json" )
258- assert .NoError (t , err )
308+ if ! assert .NoError (t , err ) {
309+ t .FailNow ()
310+ }
259311
260312 basePath , err := absPath ("fixtures/expansion/all-the-things.json" )
261313 assert .NoError (t , err )
@@ -270,10 +322,6 @@ func TestExportedResponseExpansion(t *testing.T) {
270322 assert .NoError (t , err )
271323
272324 err = ExpandResponse (& resp , basePath )
273- // b, _ := resp.MarshalJSON()
274- // log.Printf(string(b))
275- // b, _ = expected.MarshalJSON()
276- // log.Printf(string(b))
277325 assert .NoError (t , err )
278326 assert .Equal (t , expected , resp )
279327
@@ -289,7 +337,8 @@ func TestExportedResponseExpansion(t *testing.T) {
289337
290338 err = ExpandResponse (& resp , basePath )
291339 assert .NoError (t , err )
292- // assert.Equal(t, expected, resp)
340+ // NOTE(fredbi): fixed this testcase in schema_loader.go#227
341+ assert .Equal (t , expected , resp )
293342}
294343
295344func TestExpandResponseAndParamWithRoot (t * testing.T ) {
@@ -325,6 +374,28 @@ func TestExpandResponseAndParamWithRoot(t *testing.T) {
325374 assert .Nil (t , m )
326375}
327376
377+ func TestResolveParam (t * testing.T ) {
378+ specDoc , err := jsonDoc ("fixtures/expansion/all-the-things.json" )
379+ if ! assert .NoError (t , err ) {
380+ t .FailNow ()
381+ }
382+ var spec Swagger
383+ _ = json .Unmarshal (specDoc , & spec )
384+
385+ param := spec .Paths .Paths ["/pets/{id}" ].Get .Parameters [0 ]
386+ par , err := ResolveParameter (spec , param .Ref )
387+ assert .NoError (t , err )
388+ jazon , _ := json .MarshalIndent (par , "" , " " )
389+ assert .JSONEq (t , `{
390+ "name": "id",
391+ "in": "path",
392+ "description": "ID of pet to fetch",
393+ "required": true,
394+ "type": "integer",
395+ "format": "int64"
396+ }` , string (jazon ))
397+ }
398+
328399func TestIssue3 (t * testing.T ) {
329400 spec := new (Swagger )
330401 specDoc , err := jsonDoc ("fixtures/expansion/overflow.json" )
@@ -1513,6 +1584,185 @@ func expandRootWithID(t *testing.T, root *Swagger, testcase string) {
15131584 }
15141585}
15151586
1587+ const pathItemsFixture = "fixtures/expansion/pathItems.json"
1588+
1589+ func TestExpandPathItem (t * testing.T ) {
1590+ spec := new (Swagger )
1591+ specDoc , err := jsonDoc (pathItemsFixture )
1592+ assert .NoError (t , err )
1593+ _ = json .Unmarshal (specDoc , spec )
1594+ specPath , _ := absPath (pathItemsFixture )
1595+
1596+ // ExpandSpec use case
1597+ err = ExpandSpec (spec , & ExpandOptions {RelativeBase : specPath })
1598+ if ! assert .NoError (t , err ) {
1599+ t .FailNow ()
1600+ }
1601+ jazon , _ := json .MarshalIndent (spec , "" , " " )
1602+ assert .JSONEq (t , `{
1603+ "swagger": "2.0",
1604+ "info": {
1605+ "title": "PathItems refs",
1606+ "version": "1.0"
1607+ },
1608+ "paths": {
1609+ "/todos": {
1610+ "get": {
1611+ "responses": {
1612+ "200": {
1613+ "description": "List Todos",
1614+ "schema": {
1615+ "type": "array",
1616+ "items": {
1617+ "type": "string"
1618+ }
1619+ }
1620+ },
1621+ "404": {
1622+ "description": "error"
1623+ }
1624+ }
1625+ }
1626+ }
1627+ }
1628+ }` , string (jazon ))
1629+ }
1630+
1631+ func TestResolvePathItem (t * testing.T ) {
1632+ spec := new (Swagger )
1633+ specDoc , err := jsonDoc (pathItemsFixture )
1634+ assert .NoError (t , err )
1635+ _ = json .Unmarshal (specDoc , spec )
1636+ specPath , _ := absPath (pathItemsFixture )
1637+
1638+ // Resolve use case
1639+ pth := spec .Paths .Paths ["/todos" ]
1640+ pathItem , err := ResolvePathItem (spec , pth .Ref , & ExpandOptions {RelativeBase : specPath })
1641+ assert .NoError (t , err )
1642+ jazon , _ := json .MarshalIndent (pathItem , "" , " " )
1643+ assert .JSONEq (t , `{
1644+ "get": {
1645+ "responses": {
1646+ "200": {
1647+ "description": "List Todos",
1648+ "schema": {
1649+ "type": "array",
1650+ "items": {
1651+ "type": "string"
1652+ }
1653+ }
1654+ },
1655+ "404": {
1656+ "description": "error"
1657+ }
1658+ }
1659+ }
1660+ }` , string (jazon ))
1661+ }
1662+
1663+ const extraRefFixture = "fixtures/expansion/extraRef.json"
1664+
1665+ func TestExpandExtraItems (t * testing.T ) {
1666+ spec := new (Swagger )
1667+ specDoc , err := jsonDoc (extraRefFixture )
1668+ assert .NoError (t , err )
1669+ _ = json .Unmarshal (specDoc , spec )
1670+ specPath , _ := absPath (extraRefFixture )
1671+
1672+ // ExpandSpec use case: unsupported $refs are not expanded
1673+ err = ExpandSpec (spec , & ExpandOptions {RelativeBase : specPath })
1674+ if ! assert .NoError (t , err ) {
1675+ t .FailNow ()
1676+ }
1677+ jazon , _ := json .MarshalIndent (spec , "" , " " )
1678+ assert .JSONEq (t , `{
1679+ "schemes": [
1680+ "http"
1681+ ],
1682+ "swagger": "2.0",
1683+ "info": {
1684+ "title": "Supported, but non Swagger 20 compliant $ref constructs",
1685+ "version": "2.1.0"
1686+ },
1687+ "host": "item.com",
1688+ "basePath": "/extraRefs",
1689+ "paths": {
1690+ "/employees": {
1691+ "get": {
1692+ "summary": "List Employee Types",
1693+ "operationId": "LIST-Employees",
1694+ "parameters": [
1695+ {
1696+ "description": "unsupported $ref in simple param",
1697+ "type": "array",
1698+ "items": {
1699+ "$ref": "#/definitions/arrayType"
1700+ },
1701+ "name": "myQueryParam",
1702+ "in": "query"
1703+ }
1704+ ],
1705+ "responses": {
1706+ "200": {
1707+ "description": "unsupported $ref in header",
1708+ "schema": {
1709+ "type": "string"
1710+ },
1711+ "headers": {
1712+ "X-header": {
1713+ "type": "array",
1714+ "items": {
1715+ "$ref": "#/definitions/headerType"
1716+ }
1717+ }
1718+ }
1719+ }
1720+ }
1721+ }
1722+ }
1723+ },
1724+ "definitions": {
1725+ "arrayType": {
1726+ "type": "integer",
1727+ "format": "int32"
1728+ },
1729+ "headerType": {
1730+ "type": "string",
1731+ "format": "uuid"
1732+ }
1733+ }
1734+ }` , string (jazon ))
1735+ }
1736+
1737+ func TestResolveExtraItem (t * testing.T ) {
1738+ // go-openapi extra goodie: $ref in simple schema Items and Headers
1739+ spec := new (Swagger )
1740+ specDoc , err := jsonDoc (extraRefFixture )
1741+ assert .NoError (t , err )
1742+ _ = json .Unmarshal (specDoc , spec )
1743+ specPath , _ := absPath (extraRefFixture )
1744+
1745+ // Resolve param Items use case: here we explicitly resolve the unsuppord case
1746+ parm := spec .Paths .Paths ["/employees" ].Get .Parameters [0 ]
1747+ parmItem , err := ResolveItems (spec , parm .Items .Ref , & ExpandOptions {RelativeBase : specPath })
1748+ assert .NoError (t , err )
1749+ jazon , _ := json .MarshalIndent (parmItem , "" , " " )
1750+ assert .JSONEq (t , `{
1751+ "type": "integer",
1752+ "format": "int32"
1753+ }` , string (jazon ))
1754+
1755+ // Resolve header Items use case: here we explicitly resolve the unsuppord case
1756+ hdr := spec .Paths .Paths ["/employees" ].Get .Responses .StatusCodeResponses [200 ].Headers ["X-header" ]
1757+ hdrItem , err := ResolveItems (spec , hdr .Items .Ref , & ExpandOptions {RelativeBase : specPath })
1758+ assert .NoError (t , err )
1759+ jazon , _ = json .MarshalIndent (hdrItem , "" , " " )
1760+ assert .JSONEq (t , `{
1761+ "type": "string",
1762+ "format": "uuid"
1763+ }` , string (jazon ))
1764+ }
1765+
15161766// PetStoreJSONMessage json raw message for Petstore20
15171767var PetStoreJSONMessage = json .RawMessage ([]byte (PetStore20 ))
15181768
0 commit comments