Skip to content

Commit 9baaddb

Browse files
test: Improve test coverage for example reference resolution
This commit improves the test coverage for the fix in the previous commit. It adds new test cases that cover all the places where `resolveExampleRef` is called, ensuring that the fix is working correctly in all cases. The new test file `example_refs_test.go` contains individual tests for each scenario.
1 parent 307e9f4 commit 9baaddb

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

openapi3/example_refs_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package openapi3_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/getkin/kin-openapi/openapi3"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestParameterExampleRef(t *testing.T) {
11+
loader := openapi3.NewLoader()
12+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
13+
require.NoError(t, err)
14+
err = doc.Validate(loader.Context)
15+
require.NoError(t, err)
16+
param := doc.Paths.Value("/test").Post.Parameters[0].Value
17+
require.NotNil(t, param, "Parameter should not be nil")
18+
require.NotNil(t, param.Examples["Test"].Value, "Parameter example should not be nil")
19+
require.Equal(t, "An example", param.Examples["Test"].Value.Summary)
20+
}
21+
22+
func TestParameterExampleWithContentRef(t *testing.T) {
23+
loader := openapi3.NewLoader()
24+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
25+
require.NoError(t, err)
26+
err = doc.Validate(loader.Context)
27+
require.NoError(t, err)
28+
param := doc.Paths.Value("/test").Post.Parameters[1].Value
29+
require.NotNil(t, param, "Parameter should not be nil")
30+
require.NotNil(t, param.Content["application/json"].Examples["Test"].Value, "Parameter example should not be nil")
31+
require.Equal(t, "An example", param.Content["application/json"].Examples["Test"].Value.Summary)
32+
}
33+
34+
func TestRequestBodyExampleRef(t *testing.T) {
35+
loader := openapi3.NewLoader()
36+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
37+
require.NoError(t, err)
38+
err = doc.Validate(loader.Context)
39+
require.NoError(t, err)
40+
requestBody := doc.Paths.Value("/test").Post.RequestBody.Value
41+
require.NotNil(t, requestBody, "Request body should not be nil")
42+
require.NotNil(t, requestBody.Content["application/json"].Examples["Test"].Value, "Request body example should not be nil")
43+
require.Equal(t, "An example", requestBody.Content["application/json"].Examples["Test"].Value.Summary)
44+
}
45+
46+
func TestResponseExampleRef(t *testing.T) {
47+
loader := openapi3.NewLoader()
48+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
49+
require.NoError(t, err)
50+
err = doc.Validate(loader.Context)
51+
require.NoError(t, err)
52+
response := doc.Paths.Value("/test").Post.Responses.Value("200").Value
53+
require.NotNil(t, response, "Response should not be nil")
54+
require.NotNil(t, response.Content["application/json"].Examples["Test"].Value, "Response example should not be nil")
55+
require.Equal(t, "An example", response.Content["application/json"].Examples["Test"].Value.Summary)
56+
}
57+
58+
func TestHeaderExampleRef(t *testing.T) {
59+
loader := openapi3.NewLoader()
60+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
61+
require.NoError(t, err)
62+
err = doc.Validate(loader.Context)
63+
require.NoError(t, err)
64+
response := doc.Paths.Value("/test").Post.Responses.Value("200").Value
65+
header := response.Headers["X-Test-Header"].Value
66+
require.NotNil(t, header, "Header should not be nil")
67+
require.NotNil(t, header.Examples["Test"].Value, "Header example should not be nil")
68+
require.Equal(t, "An example", header.Examples["Test"].Value.Summary)
69+
}
70+
71+
func TestComponentExampleRef(t *testing.T) {
72+
loader := openapi3.NewLoader()
73+
doc, err := loader.LoadFromFile("testdata/example_refs.yml")
74+
require.NoError(t, err)
75+
err = doc.Validate(loader.Context)
76+
require.NoError(t, err)
77+
example := doc.Components.Examples["RefExample"].Value
78+
require.NotNil(t, example, "Example should not be nil")
79+
require.Equal(t, "An example", example.Summary)
80+
}

openapi3/testdata/issue_resolve_example_ref.yml renamed to openapi3/testdata/example_refs.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ paths:
88
summary: Test endpoint
99
parameters:
1010
- $ref: '#/components/parameters/TestParameter'
11+
- $ref: '#/components/parameters/TestParameterWithContent'
1112
requestBody:
1213
$ref: '#/components/requestBodies/TestBody'
1314
responses:
@@ -18,6 +19,8 @@ components:
1819
TestExample:
1920
summary: An example
2021
value: 'hello'
22+
RefExample:
23+
$ref: '#/components/examples/TestExample'
2124
headers:
2225
TestHeader:
2326
description: A test header
@@ -35,10 +38,22 @@ components:
3538
examples:
3639
Test:
3740
$ref: '#/components/examples/TestExample'
41+
TestParameterWithContent:
42+
name: testContent
43+
in: query
44+
content:
45+
application/json:
46+
schema:
47+
type: string
48+
examples:
49+
Test:
50+
$ref: '#/components/examples/TestExample'
3851
requestBodies:
3952
TestBody:
4053
content:
4154
application/json:
55+
schema:
56+
type: string
4257
examples:
4358
Test:
4459
$ref: '#/components/examples/TestExample'
@@ -50,6 +65,8 @@ components:
5065
$ref: '#/components/headers/TestHeader'
5166
content:
5267
application/json:
68+
schema:
69+
type: string
5370
examples:
5471
Test:
55-
$ref: '#/components/examples/TestExample'
72+
$ref: '#/components/examples/TestExample'

0 commit comments

Comments
 (0)