| 3188 | } |
| 3189 | |
| 3190 | func TestOpenAPI(t *testing.T) { |
| 3191 | r, api := humatest.New(t, huma.DefaultConfig("Features Test API", "1.0.0")) |
| 3192 | |
| 3193 | // Used to validate exclusion of embedded structs from response headers |
| 3194 | type PaginationHeaders struct { |
| 3195 | Link string `header:"link"` |
| 3196 | } |
| 3197 | |
| 3198 | type Resp struct { |
| 3199 | PaginationHeaders |
| 3200 | Body struct { |
| 3201 | Greeting string `json:"greeting"` |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | huma.Register(api, huma.Operation{ |
| 3206 | Method: http.MethodGet, |
| 3207 | Path: "/test", |
| 3208 | }, func(ctx context.Context, input *struct{}) (*Resp, error) { |
| 3209 | resp := &Resp{} |
| 3210 | resp.Body.Greeting = "Hello, world" |
| 3211 | return resp, nil |
| 3212 | }) |
| 3213 | |
| 3214 | for _, url := range []string{ |
| 3215 | "/openapi.json", |
| 3216 | "/openapi-3.0.json", |
| 3217 | "/openapi.yaml", |
| 3218 | "/openapi-3.0.yaml", |
| 3219 | "/docs", |
| 3220 | "/schemas/RespBody.json", |
| 3221 | } { |
| 3222 | req, _ := http.NewRequest(http.MethodGet, url, nil) |
| 3223 | w := httptest.NewRecorder() |
| 3224 | r.ServeHTTP(w, req) |
| 3225 | |
| 3226 | assert.Equal(t, 200, w.Code, w.Body.String()) |
| 3227 | } |
| 3228 | |
| 3229 | t.Run("ignore-anonymous-header-structs", func(t *testing.T) { |
| 3230 | req, _ := http.NewRequest(http.MethodGet, "/openapi.yaml", nil) |
| 3231 | w := httptest.NewRecorder() |
| 3232 | r.ServeHTTP(w, req) |
| 3233 | |
| 3234 | openapiBody := w.Body.String() |
| 3235 | assert.Equal(t, 200, w.Code, openapiBody) |
| 3236 | assert.Contains(t, openapiBody, "link") |
| 3237 | assert.NotContains(t, openapiBody, "PaginationHeaders") |
| 3238 | }) |
| 3239 | } |
| 3240 | |
| 3241 | type CTFilterBody struct { |
| 3242 | Field string `json:"field"` |