From cd6560ade1d509f276ed8027ec27b40743511487 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Wed, 1 Feb 2023 15:56:44 +0100 Subject: [PATCH 1/5] add failing test Signed-off-by: Pierre Fenoll --- openapi3/issue759_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 openapi3/issue759_test.go diff --git a/openapi3/issue759_test.go b/openapi3/issue759_test.go new file mode 100644 index 000000000..255d8b7b6 --- /dev/null +++ b/openapi3/issue759_test.go @@ -0,0 +1,34 @@ +package openapi3 + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIssue759(t *testing.T) { + spec := []byte(` +openapi: 3.0.0 +info: + title: title + description: description + version: 0.0.0 +paths: + /slash: + get: + responses: + "200": + # Ref should point to a response, not a schema + $ref: "#/components/schemas/UserStruct" +components: + schemas: + UserStruct: + type: object +`[1:]) + + loader := NewLoader() + + doc, err := loader.LoadFromData(spec) + require.Nil(t, doc) + require.EqualError(t, err, `bad data in "#/components/schemas/UserStruct" (expecting ref to response object)`) +} From 8218f0c0e542850ed7492496eb92777f9b743027 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Wed, 1 Feb 2023 15:57:29 +0100 Subject: [PATCH 2/5] make error more actionable Signed-off-by: Pierre Fenoll --- openapi3/loader.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/openapi3/loader.go b/openapi3/loader.go index 7f389cdfe..51020111e 100644 --- a/openapi3/loader.go +++ b/openapi3/loader.go @@ -351,12 +351,41 @@ func (loader *Loader) resolveComponent(doc *T, ref string, path *url.URL, resolv return nil } if err := codec(cursor, resolved); err != nil { - return nil, nil, fmt.Errorf("bad data in %q", ref) + return nil, nil, fmt.Errorf("bad data in %q (expecting %s)", ref, readableType(resolved)) } return componentDoc, componentPath, nil default: - return nil, nil, fmt.Errorf("bad data in %q", ref) + return nil, nil, fmt.Errorf("bad data in %q (expecting %s)", ref, readableType(resolved)) + } +} + +func readableType(x interface{}) string { + switch x.(type) { + case *Callback: + return "callback object" + case *CallbackRef: + return "ref to callback object" + case *ExampleRef: + return "ref to example object" + case *HeaderRef: + return "ref to header object" + case *LinkRef: + return "ref to link object" + case *ParameterRef: + return "ref to parameter object" + case *PathItem: + return "pathItem object" + case *RequestBodyRef: + return "ref to requestBody object" + case *ResponseRef: + return "ref to response object" + case *SchemaRef: + return "ref to schema object" + case *SecuritySchemeRef: + return "ref to securityScheme object" + default: + panic(fmt.Sprintf("unreachable %T", x)) } } From 1f92c77f8385f1971a95c5216bcfaa8d48697f9f Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Wed, 1 Feb 2023 15:57:58 +0100 Subject: [PATCH 3/5] add CI check for pattern exhaustiveness Signed-off-by: Pierre Fenoll --- .github/workflows/go.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 8dbb0620f..a6ed869cf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -176,6 +176,24 @@ jobs: Tag XML + - if: runner.os == 'Linux' + name: Ensure readableType() covers all possible values of resolved var + run: | + [[ "$(git grep -F 'var resolved ' -- openapi3/loader.go | awk '{print $4}' | sort | tr '\n' ' ')" = "$(echo "$RESOLVEDS" | tr '\n' ' ')" ]] + env: + RESOLVEDS: > + Callback + CallbackRef + ExampleRef + HeaderRef + LinkRef + ParameterRef + PathItem + RequestBodyRef + ResponseRef + SchemaRef + SecuritySchemeRef + check-goimports: runs-on: ubuntu-latest steps: From a6fcabf7b2e32ff5be8daddaf5b9c6ab2d405954 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Wed, 1 Feb 2023 16:04:29 +0100 Subject: [PATCH 4/5] come on CI Signed-off-by: Pierre Fenoll --- .github/workflows/go.yml | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a6ed869cf..0279ed043 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -179,20 +179,9 @@ jobs: - if: runner.os == 'Linux' name: Ensure readableType() covers all possible values of resolved var run: | - [[ "$(git grep -F 'var resolved ' -- openapi3/loader.go | awk '{print $4}' | sort | tr '\n' ' ')" = "$(echo "$RESOLVEDS" | tr '\n' ' ')" ]] + [[ "$(git grep -F 'var resolved ' -- openapi3/loader.go | awk '{print $4}' | sort | tr '\n' ' ')" = "$RESOLVEDS" ]] env: - RESOLVEDS: > - Callback - CallbackRef - ExampleRef - HeaderRef - LinkRef - ParameterRef - PathItem - RequestBodyRef - ResponseRef - SchemaRef - SecuritySchemeRef + RESOLVEDS: 'Callback CallbackRef ExampleRef HeaderRef LinkRef ParameterRef PathItem RequestBodyRef ResponseRef SchemaRef SecuritySchemeRef' check-goimports: runs-on: ubuntu-latest From 7fad0a023354631a69d9bdafad4da27a8cbf4994 Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Wed, 1 Feb 2023 16:36:57 +0100 Subject: [PATCH 5/5] gnnnn Signed-off-by: Pierre Fenoll --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0279ed043..9d9ff726f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -181,7 +181,7 @@ jobs: run: | [[ "$(git grep -F 'var resolved ' -- openapi3/loader.go | awk '{print $4}' | sort | tr '\n' ' ')" = "$RESOLVEDS" ]] env: - RESOLVEDS: 'Callback CallbackRef ExampleRef HeaderRef LinkRef ParameterRef PathItem RequestBodyRef ResponseRef SchemaRef SecuritySchemeRef' + RESOLVEDS: 'Callback CallbackRef ExampleRef HeaderRef LinkRef ParameterRef PathItem RequestBodyRef ResponseRef SchemaRef SecuritySchemeRef ' check-goimports: runs-on: ubuntu-latest