Skip to content

Commit

Permalink
fix(flatten): onboard fix for relative $ref in params&responses
Browse files Browse the repository at this point in the history
* fixes go-swagger#2743

This PR onboards the fix to go-openapi/spec#182:
 * invalid relative path in $ref in schema for parameters or response
   when SkipSchema=true (flatten use-case)

Added a test to assert that the fix works fine with the version update.

Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Jan 10, 2024
1 parent 70c3781 commit 4aff943
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 7 deletions.
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
30 changes: 29 additions & 1 deletion generator/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,36 @@ func TestShared_Issue2113(t *testing.T) {

opts := testGenOpts()
opts.Spec = specPath
opts.Spec = specPath
opts.ValidateSpec = true
_, err = opts.validateAndFlattenSpec()
assert.NoError(t, err)
}

func TestShared_Issue2743(t *testing.T) {
defer discardOutput()()

// acknowledge fix in go-openapi/spec
t.Run("should NOT flatten invalid spec that used to work", func(t *testing.T) {
specPath := filepath.Join("..", "fixtures", "bugs", "2743", "working", "spec.yaml")
_, err := loads.Spec(specPath)
require.NoError(t, err)

opts := testGenOpts()
opts.Spec = specPath
opts.ValidateSpec = true
_, err = opts.validateAndFlattenSpec()
assert.Error(t, err)
})

t.Run("should flatten valid spec that used NOT to work", func(t *testing.T) {
specPath := filepath.Join("..", "fixtures", "bugs", "2743", "not-working", "spec.yaml")
_, err := loads.Spec(specPath)
require.NoError(t, err)

opts := testGenOpts()
opts.Spec = specPath
opts.ValidateSpec = true
_, err = opts.validateAndFlattenSpec()
assert.NoError(t, err)
})
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/davecgh/go-spew v1.1.1
github.com/go-openapi/analysis v0.22.1
github.com/go-openapi/analysis v0.22.2
github.com/go-openapi/errors v0.21.0
github.com/go-openapi/inflect v0.19.0
github.com/go-openapi/loads v0.21.5
github.com/go-openapi/runtime v0.26.0
github.com/go-openapi/spec v0.20.13
github.com/go-openapi/spec v0.20.14
github.com/go-openapi/strfmt v0.22.0
github.com/go-openapi/swag v0.22.7
github.com/go-openapi/validate v0.22.6
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-openapi/analysis v0.22.1 h1:nBWSKtx/OQr6FADwy0AYDZFdT05Abv5eYWECByHEQMg=
github.com/go-openapi/analysis v0.22.1/go.mod h1:acDnkkCI2QxIo8sSIPgmp1wUlRohV7vfGtAIVae73b0=
github.com/go-openapi/analysis v0.22.2 h1:ZBmNoP2h5omLKr/srIC9bfqrUGzT6g6gNv03HE9Vpj0=
github.com/go-openapi/analysis v0.22.2/go.mod h1:pDF4UbZsQTo/oNuRfAWWd4dAh4yuYf//LYorPTjrpvo=
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/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4=
Expand All @@ -97,8 +97,8 @@ github.com/go-openapi/loads v0.21.5 h1:jDzF4dSoHw6ZFADCGltDb2lE4F6De7aWSpe+IcsRz
github.com/go-openapi/loads v0.21.5/go.mod h1:PxTsnFBoBe+z89riT+wYt3prmSBP6GDAQh2l9H1Flz8=
github.com/go-openapi/runtime v0.26.0 h1:HYOFtG00FM1UvqrcxbEJg/SwvDRvYLQKGhw2zaQjTcc=
github.com/go-openapi/runtime v0.26.0/go.mod h1:QgRGeZwrUcSHdeh4Ka9Glvo0ug1LC5WyE+EV88plZrQ=
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/spec v0.20.14 h1:7CBlRnw+mtjFGlPDRZmAMnq35cRzI91xj03HVyUi/Do=
github.com/go-openapi/spec v0.20.14/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw=
github.com/go-openapi/strfmt v0.22.0 h1:Ew9PnEYc246TwrEspvBdDHS4BVKXy/AOVsfqGDgAcaI=
github.com/go-openapi/strfmt v0.22.0/go.mod h1:HzJ9kokGIju3/K6ap8jL+OlGAbjpSv27135Yr9OivU4=
github.com/go-openapi/swag v0.22.7 h1:JWrc1uc/P9cSomxfnsFSVWoE1FW6bNbrVPmpQYpCcR8=
Expand Down

0 comments on commit 4aff943

Please sign in to comment.