Skip to content

Commit

Permalink
test(flatten): asserts that the ExpandSpec fix in spec works
Browse files Browse the repository at this point in the history
This reproduces the $ref issue with relative paths in sibling folders
in responses (same applies to parameters, but not schemas).

Added a test to verify that:
* (i) the invalid path that used to work no longer does
* (ii) the valid path that used not to work now does

* contributes go-swagger/go-swagger#2743

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
  • Loading branch information
fredbi committed Jan 8, 2024
1 parent 3e717a5 commit 6d91d00
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 2 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ linters-settings:
linters:
enable-all: true
disable:
- gomoddirectives # temporarily disabled
- maligned
- unparam
- lll
Expand Down
7 changes: 7 additions & 0 deletions fixtures/bugs/2743/not-working/minimal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/bar:
$ref: 'swagger/paths/bar.yml'
11 changes: 11 additions & 0 deletions fixtures/bugs/2743/not-working/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/foo:
$ref: 'swagger/paths/foo.yml'
/bar:
$ref: 'swagger/paths/bar.yml'
/nested:
$ref: 'swagger/paths/nested.yml#/response'
9 changes: 9 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/definitions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ErrorPayload:
title: Error Payload
required:
- errors
properties:
errors:
type: array
items:
$ref: './items.yml#/ErrorDetailsItem'
15 changes: 15 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ErrorDetailsItem:
title: Error details item
description: Represents an item of the list of details of an error.
required:
- message
- code
properties:
message:
type: string
code:
type: string
details:
type: array
items:
type: string
8 changes: 8 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/paths/bar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get:
responses:
200:
description: OK
schema:
type: array
items:
$ref: '../user/index.yml#/User' ## this doesn't work
8 changes: 8 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/paths/foo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get:
responses:
200:
description: OK
500:
description: OK
schema:
$ref: '../definitions.yml#/ErrorPayload'
6 changes: 6 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/paths/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/foo:
$ref: ./foo.yml
/bar:
$ref: ./bar.yml
/nested:
$ref: ./nested.yml#/response
14 changes: 14 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/paths/nested.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
response:
get:
responses:
200:
description: OK
schema:
$ref: '#/definitions/SameFileReference'

definitions:
SameFileReference:
type: object
properties:
name:
type: string
2 changes: 2 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/user/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User:
$ref: './model.yml'
4 changes: 4 additions & 0 deletions fixtures/bugs/2743/not-working/swagger/user/model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: object
properties:
name:
type: string
11 changes: 11 additions & 0 deletions fixtures/bugs/2743/working/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
swagger: '2.0'
info:
version: 0.0.0
title: Simple API
paths:
/foo:
$ref: 'swagger/paths/foo.yml'
/bar:
$ref: 'swagger/paths/bar.yml'
/nested:
$ref: 'swagger/paths/nested.yml#/response'
9 changes: 9 additions & 0 deletions fixtures/bugs/2743/working/swagger/definitions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ErrorPayload:
title: Error Payload
required:
- errors
properties:
errors:
type: array
items:
$ref: './items.yml#/ErrorDetailsItem'
15 changes: 15 additions & 0 deletions fixtures/bugs/2743/working/swagger/items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ErrorDetailsItem:
title: Error details item
description: Represents an item of the list of details of an error.
required:
- message
- code
properties:
message:
type: string
code:
type: string
details:
type: array
items:
type: string
8 changes: 8 additions & 0 deletions fixtures/bugs/2743/working/swagger/paths/bar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get:
responses:
200:
description: OK
schema:
type: array
items:
$ref: './swagger/user/index.yml#/User' ## this works
8 changes: 8 additions & 0 deletions fixtures/bugs/2743/working/swagger/paths/foo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get:
responses:
200:
description: OK
500:
description: OK
schema:
$ref: '../definitions.yml#/ErrorPayload'
6 changes: 6 additions & 0 deletions fixtures/bugs/2743/working/swagger/paths/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/foo:
$ref: ./foo.yml
/bar:
$ref: ./bar.yml
/nested:
$ref: ./nested.yml#/response
14 changes: 14 additions & 0 deletions fixtures/bugs/2743/working/swagger/paths/nested.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
response:
get:
responses:
200:
description: OK
schema:
$ref: '#/definitions/SameFileReference'

definitions:
SameFileReference:
type: object
properties:
name:
type: string
2 changes: 2 additions & 0 deletions fixtures/bugs/2743/working/swagger/user/index.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User:
$ref: './model.yml'
4 changes: 4 additions & 0 deletions fixtures/bugs/2743/working/swagger/user/model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: object
properties:
name:
type: string
29 changes: 29 additions & 0 deletions flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,35 @@ func TestFlatten_RemoveUnused_2657(t *testing.T) {
require.Empty(t, sp.Definitions)
}

func TestFlatten_Relative_2743(t *testing.T) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stdout)

t.Run("used to work, but should NOT", func(t *testing.T) {
bp := filepath.Join("fixtures", "bugs", "2743", "working", "spec.yaml")
sp := antest.LoadOrFail(t, bp)
an := New(sp)

require.Error(t, Flatten(FlattenOpts{
Spec: an, BasePath: bp, Verbose: true,
Minimal: true,
Expand: false,
}))
})

t.Run("used not to, but should work", func(t *testing.T) {
bp := filepath.Join("fixtures", "bugs", "2743", "not-working", "spec.yaml")
sp := antest.LoadOrFail(t, bp)
an := New(sp)

require.NoError(t, Flatten(FlattenOpts{
Spec: an, BasePath: bp, Verbose: true,
Minimal: true,
Expand: false,
}))
})
}

func getDefinition(t testing.TB, sp *spec.Swagger, key string) string {
d, ok := sp.Definitions[key]
require.Truef(t, ok, "Expected definition for %s", key)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/go-openapi/analysis

replace github.com/go-openapi/spec => github.com/fredbi/spec v0.20.14-fred

require (
github.com/go-openapi/jsonpointer v0.20.2
github.com/go-openapi/spec v0.20.13
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3d
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fredbi/spec v0.20.14-fred h1:CYey/Hz4x7z/C9iNbOwpql9RELsd3Cl8T1qiu52WqJ0=
github.com/fredbi/spec v0.20.14-fred/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw=
github.com/go-openapi/errors v0.21.0 h1:FhChC/duCnfoLj1gZ0BgaBmzhJC2SL/sJr8a2vAobSY=
github.com/go-openapi/errors v0.21.0/go.mod h1:jxNTMUxRCKj65yb/okJGEtahVd7uvWnuWfj53bse4ho=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU=
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/spec v0.20.13 h1:XJDIN+dLH6vqXgafnl5SUIMnzaChQ6QTo0/UPMbkIaE=
github.com/go-openapi/spec v0.20.13/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw=
github.com/go-openapi/strfmt v0.21.10 h1:JIsly3KXZB/Qf4UzvzJpg4OELH/0ASDQsyk//TTBDDk=
github.com/go-openapi/strfmt v0.21.10/go.mod h1:vNDMwbilnl7xKiO/Ve/8H8Bb2JIInBnH+lqiw6QWgis=
github.com/go-openapi/swag v0.22.6 h1:dnqg1XfHXL9aBxSbktBqFR5CxVyVI+7fYWhAf1JOeTw=
Expand Down

0 comments on commit 6d91d00

Please sign in to comment.