Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: example ref to schemas cause error #669

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions openapi3/loader.go
Expand Up @@ -683,6 +683,23 @@ func (loader *Loader) resolveResponseRef(doc *T, component *ResponseRef, documen
sort.Strings(examples)
for _, name := range examples {
example := contentType.Examples[name]
if exampleRef := example.Ref; strings.HasPrefix(exampleRef, "#/components/schemas") {
parsedUrl, err := url.Parse(exampleRef)
if err != nil {
return err
}
var resolved SchemaRef
_, err = loader.resolveComponent(doc, exampleRef, parsedUrl, &resolved)
if err != nil {
return err
}
contentType.Examples[name] = &ExampleRef{
Ref: exampleRef,
Value: NewExample(resolved.Value),
}
continue

}
if err := loader.resolveExampleRef(doc, example, documentPath); err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions openapi3/loader_exmaple_schema_ref_test.go
@@ -0,0 +1,20 @@
package openapi3

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestLoadSpecWithExampleRefToSchemas(t *testing.T) {
loader := NewLoader()
doc, err := loader.LoadFromFile("testdata/example-schemas-ref.json")
require.NoError(t, err)

err = doc.Validate(loader.Context)
require.NoError(t, err)
exampleValue := doc.Paths["/pet"].Put.Responses.Get(400).Value.Content.Get("*/*").Examples["200"].Value.Value
schemaRef := doc.Components.Schemas["Pet"].Value
require.Equal(t, exampleValue, schemaRef)
require.NoError(t, err)
dominicqi marked this conversation as resolved.
Show resolved Hide resolved
}