|
| 1 | +// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package integration_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/go-openapi/codescan" |
| 10 | + "github.com/go-openapi/codescan/internal/scantest" |
| 11 | + "github.com/go-openapi/testify/v2/assert" |
| 12 | + "github.com/go-openapi/testify/v2/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestCoverage_ResponseExamplesByMime covers response-level `examples` (a map |
| 16 | +// keyed by mime type) on a struct-based `swagger:response` (go-swagger#2871). |
| 17 | +// |
| 18 | +// The `swagger:operation` YAML path already carries examples for free (see |
| 19 | +// coverage_bug_1713 / coverage_bug_2871); this verifies the Go-struct |
| 20 | +// `swagger:response` path, where the `examples:` block in the decl comment is |
| 21 | +// parsed into the OAS2 Response.examples field. |
| 22 | +func TestCoverage_ResponseExamplesByMime(t *testing.T) { |
| 23 | + doc, err := codescan.Run(&codescan.Options{ |
| 24 | + Packages: []string{"./enhancements/response-examples-by-mime/..."}, |
| 25 | + WorkDir: scantest.FixturesDir(), |
| 26 | + ScanModels: true, |
| 27 | + }) |
| 28 | + require.NoError(t, err) |
| 29 | + require.NotNil(t, doc) |
| 30 | + |
| 31 | + resp, ok := doc.Responses["widgetResponse"] |
| 32 | + require.TrueT(t, ok, "expected a widgetResponse in the responses section") |
| 33 | + require.NotNil(t, resp.Examples, "response must carry an examples map") |
| 34 | + |
| 35 | + jsonEx, ok := resp.Examples["application/json"].(map[string]any) |
| 36 | + require.TrueT(t, ok, "expected an application/json example object") |
| 37 | + assert.MapEqualT(t, map[string]any{"name": "alice", "count": float64(3)}, jsonEx) |
| 38 | + |
| 39 | + xmlEx, ok := resp.Examples["application/xml"] |
| 40 | + require.TrueT(t, ok, "expected an application/xml example") |
| 41 | + assert.EqualT(t, any("<widget><name>alice</name></widget>"), xmlEx) |
| 42 | + |
| 43 | + // The body schema and description survive alongside the examples. |
| 44 | + assert.EqualT(t, "WidgetResponse returns a widget, with response-level examples per mime type.", resp.Description) |
| 45 | + require.NotNil(t, resp.Schema) |
| 46 | + |
| 47 | + scantest.CompareOrDumpJSON(t, doc, "enhancements_response_examples_by_mime.json") |
| 48 | +} |
0 commit comments