From a12c0caf37790fc749eafecb99ef8c01afe70737 Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Mon, 8 Jan 2024 16:27:08 +0100 Subject: [PATCH] test(flatten): asserts that the ExpandSpec fix in spec works 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 --- fixtures/bugs/2743/not-working/minimal.yaml | 7 +++++ fixtures/bugs/2743/not-working/spec.yaml | 11 +++++++ .../2743/not-working/swagger/definitions.yml | 9 ++++++ .../bugs/2743/not-working/swagger/items.yml | 15 ++++++++++ .../2743/not-working/swagger/paths/bar.yml | 8 +++++ .../2743/not-working/swagger/paths/foo.yml | 8 +++++ .../2743/not-working/swagger/paths/index.yml | 6 ++++ .../2743/not-working/swagger/paths/nested.yml | 14 +++++++++ .../2743/not-working/swagger/user/index.yml | 2 ++ .../2743/not-working/swagger/user/model.yml | 4 +++ fixtures/bugs/2743/working/spec.yaml | 11 +++++++ .../bugs/2743/working/swagger/definitions.yml | 9 ++++++ fixtures/bugs/2743/working/swagger/items.yml | 15 ++++++++++ .../bugs/2743/working/swagger/paths/bar.yml | 8 +++++ .../bugs/2743/working/swagger/paths/foo.yml | 8 +++++ .../bugs/2743/working/swagger/paths/index.yml | 6 ++++ .../2743/working/swagger/paths/nested.yml | 14 +++++++++ .../bugs/2743/working/swagger/user/index.yml | 2 ++ .../bugs/2743/working/swagger/user/model.yml | 4 +++ flatten_test.go | 29 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 22 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 fixtures/bugs/2743/not-working/minimal.yaml create mode 100644 fixtures/bugs/2743/not-working/spec.yaml create mode 100644 fixtures/bugs/2743/not-working/swagger/definitions.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/items.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/paths/bar.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/paths/foo.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/paths/index.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/paths/nested.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/user/index.yml create mode 100644 fixtures/bugs/2743/not-working/swagger/user/model.yml create mode 100644 fixtures/bugs/2743/working/spec.yaml create mode 100644 fixtures/bugs/2743/working/swagger/definitions.yml create mode 100644 fixtures/bugs/2743/working/swagger/items.yml create mode 100644 fixtures/bugs/2743/working/swagger/paths/bar.yml create mode 100644 fixtures/bugs/2743/working/swagger/paths/foo.yml create mode 100644 fixtures/bugs/2743/working/swagger/paths/index.yml create mode 100644 fixtures/bugs/2743/working/swagger/paths/nested.yml create mode 100644 fixtures/bugs/2743/working/swagger/user/index.yml create mode 100644 fixtures/bugs/2743/working/swagger/user/model.yml diff --git a/fixtures/bugs/2743/not-working/minimal.yaml b/fixtures/bugs/2743/not-working/minimal.yaml new file mode 100644 index 0000000..2c46773 --- /dev/null +++ b/fixtures/bugs/2743/not-working/minimal.yaml @@ -0,0 +1,7 @@ +swagger: '2.0' +info: + version: 0.0.0 + title: Simple API +paths: + /bar: + $ref: 'swagger/paths/bar.yml' diff --git a/fixtures/bugs/2743/not-working/spec.yaml b/fixtures/bugs/2743/not-working/spec.yaml new file mode 100644 index 0000000..9255173 --- /dev/null +++ b/fixtures/bugs/2743/not-working/spec.yaml @@ -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' diff --git a/fixtures/bugs/2743/not-working/swagger/definitions.yml b/fixtures/bugs/2743/not-working/swagger/definitions.yml new file mode 100644 index 0000000..438d6ee --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/definitions.yml @@ -0,0 +1,9 @@ +ErrorPayload: + title: Error Payload + required: + - errors + properties: + errors: + type: array + items: + $ref: './items.yml#/ErrorDetailsItem' \ No newline at end of file diff --git a/fixtures/bugs/2743/not-working/swagger/items.yml b/fixtures/bugs/2743/not-working/swagger/items.yml new file mode 100644 index 0000000..e4a050c --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/items.yml @@ -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 \ No newline at end of file diff --git a/fixtures/bugs/2743/not-working/swagger/paths/bar.yml b/fixtures/bugs/2743/not-working/swagger/paths/bar.yml new file mode 100644 index 0000000..c813579 --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/paths/bar.yml @@ -0,0 +1,8 @@ +get: + responses: + 200: + description: OK + schema: + type: array + items: + $ref: '../user/index.yml#/User' ## this doesn't work \ No newline at end of file diff --git a/fixtures/bugs/2743/not-working/swagger/paths/foo.yml b/fixtures/bugs/2743/not-working/swagger/paths/foo.yml new file mode 100644 index 0000000..a5c0de1 --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/paths/foo.yml @@ -0,0 +1,8 @@ +get: + responses: + 200: + description: OK + 500: + description: OK + schema: + $ref: '../definitions.yml#/ErrorPayload' diff --git a/fixtures/bugs/2743/not-working/swagger/paths/index.yml b/fixtures/bugs/2743/not-working/swagger/paths/index.yml new file mode 100644 index 0000000..7cdcdd4 --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/paths/index.yml @@ -0,0 +1,6 @@ +/foo: + $ref: ./foo.yml +/bar: + $ref: ./bar.yml +/nested: + $ref: ./nested.yml#/response diff --git a/fixtures/bugs/2743/not-working/swagger/paths/nested.yml b/fixtures/bugs/2743/not-working/swagger/paths/nested.yml new file mode 100644 index 0000000..5c7cf0d --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/paths/nested.yml @@ -0,0 +1,14 @@ +response: + get: + responses: + 200: + description: OK + schema: + $ref: '#/definitions/SameFileReference' + +definitions: + SameFileReference: + type: object + properties: + name: + type: string diff --git a/fixtures/bugs/2743/not-working/swagger/user/index.yml b/fixtures/bugs/2743/not-working/swagger/user/index.yml new file mode 100644 index 0000000..99c1c6f --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/user/index.yml @@ -0,0 +1,2 @@ +User: + $ref: './model.yml' diff --git a/fixtures/bugs/2743/not-working/swagger/user/model.yml b/fixtures/bugs/2743/not-working/swagger/user/model.yml new file mode 100644 index 0000000..5cb91cd --- /dev/null +++ b/fixtures/bugs/2743/not-working/swagger/user/model.yml @@ -0,0 +1,4 @@ +type: object +properties: + name: + type: string diff --git a/fixtures/bugs/2743/working/spec.yaml b/fixtures/bugs/2743/working/spec.yaml new file mode 100644 index 0000000..9255173 --- /dev/null +++ b/fixtures/bugs/2743/working/spec.yaml @@ -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' diff --git a/fixtures/bugs/2743/working/swagger/definitions.yml b/fixtures/bugs/2743/working/swagger/definitions.yml new file mode 100644 index 0000000..438d6ee --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/definitions.yml @@ -0,0 +1,9 @@ +ErrorPayload: + title: Error Payload + required: + - errors + properties: + errors: + type: array + items: + $ref: './items.yml#/ErrorDetailsItem' \ No newline at end of file diff --git a/fixtures/bugs/2743/working/swagger/items.yml b/fixtures/bugs/2743/working/swagger/items.yml new file mode 100644 index 0000000..e4a050c --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/items.yml @@ -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 \ No newline at end of file diff --git a/fixtures/bugs/2743/working/swagger/paths/bar.yml b/fixtures/bugs/2743/working/swagger/paths/bar.yml new file mode 100644 index 0000000..41dc76a --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/paths/bar.yml @@ -0,0 +1,8 @@ +get: + responses: + 200: + description: OK + schema: + type: array + items: + $ref: './swagger/user/index.yml#/User' ## this works \ No newline at end of file diff --git a/fixtures/bugs/2743/working/swagger/paths/foo.yml b/fixtures/bugs/2743/working/swagger/paths/foo.yml new file mode 100644 index 0000000..a5c0de1 --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/paths/foo.yml @@ -0,0 +1,8 @@ +get: + responses: + 200: + description: OK + 500: + description: OK + schema: + $ref: '../definitions.yml#/ErrorPayload' diff --git a/fixtures/bugs/2743/working/swagger/paths/index.yml b/fixtures/bugs/2743/working/swagger/paths/index.yml new file mode 100644 index 0000000..7cdcdd4 --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/paths/index.yml @@ -0,0 +1,6 @@ +/foo: + $ref: ./foo.yml +/bar: + $ref: ./bar.yml +/nested: + $ref: ./nested.yml#/response diff --git a/fixtures/bugs/2743/working/swagger/paths/nested.yml b/fixtures/bugs/2743/working/swagger/paths/nested.yml new file mode 100644 index 0000000..5c7cf0d --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/paths/nested.yml @@ -0,0 +1,14 @@ +response: + get: + responses: + 200: + description: OK + schema: + $ref: '#/definitions/SameFileReference' + +definitions: + SameFileReference: + type: object + properties: + name: + type: string diff --git a/fixtures/bugs/2743/working/swagger/user/index.yml b/fixtures/bugs/2743/working/swagger/user/index.yml new file mode 100644 index 0000000..99c1c6f --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/user/index.yml @@ -0,0 +1,2 @@ +User: + $ref: './model.yml' diff --git a/fixtures/bugs/2743/working/swagger/user/model.yml b/fixtures/bugs/2743/working/swagger/user/model.yml new file mode 100644 index 0000000..5cb91cd --- /dev/null +++ b/fixtures/bugs/2743/working/swagger/user/model.yml @@ -0,0 +1,4 @@ +type: object +properties: + name: + type: string diff --git a/flatten_test.go b/flatten_test.go index 4d34933..738c2d5 100644 --- a/flatten_test.go +++ b/flatten_test.go @@ -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) diff --git a/go.mod b/go.mod index 88fb872..8bed627 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/go-openapi/analysis require ( github.com/go-openapi/jsonpointer v0.20.2 - github.com/go-openapi/spec v0.20.13 + github.com/go-openapi/spec v0.20.14 github.com/go-openapi/strfmt v0.21.10 github.com/go-openapi/swag v0.22.6 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index bc4919c..407c4c8 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbX 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/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.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=