|
| 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 | +} |
0 commit comments