(r *http.Request, destination interface{}, o *httpDecoderOptions)
| 351 | } |
| 352 | |
| 353 | func decodeForm(r *http.Request, destination interface{}, o *httpDecoderOptions) error { |
| 354 | if o.jsonSchemaCompiler == nil { |
| 355 | return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to decode HTTP Form Body because no validation schema was provided. This is a code bug.")) |
| 356 | } |
| 357 | |
| 358 | reader, err := requestBody(r, o) |
| 359 | if err != nil { |
| 360 | return err |
| 361 | } |
| 362 | |
| 363 | defer func() { |
| 364 | r.Body = reader |
| 365 | }() |
| 366 | |
| 367 | if err := r.ParseForm(); err != nil { |
| 368 | return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode HTTP %s form body: %s", strings.ToUpper(r.Method), err).WithDebug(err.Error())) |
| 369 | } |
| 370 | |
| 371 | paths, err := jsonschemax.ListPathsWithRecursion(r.Context(), o.jsonSchemaRef, o.jsonSchemaCompiler, o.maxCircularReferenceDepth) |
| 372 | if err != nil { |
| 373 | return errors.WithStack(herodot.ErrInternalServerError.WithTrace(err).WithReasonf("Unable to prepare JSON Schema for HTTP Post Body Form parsing: %s", err).WithDebugf("%+v", err)) |
| 374 | } |
| 375 | |
| 376 | values := r.PostForm |
| 377 | if r.Method == "GET" || o.queryAndBody { |
| 378 | values = r.Form |
| 379 | } |
| 380 | |
| 381 | raw, err := decodeURLValues(values, paths, o) |
| 382 | if err != nil && !errors.Is(err, errKeyNotFound) { |
| 383 | return err |
| 384 | } |
| 385 | |
| 386 | if err := json.NewDecoder(bytes.NewReader(raw)).Decode(destination); err != nil { |
| 387 | return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Unable to decode JSON payload: %s", err)) |
| 388 | } |
| 389 | |
| 390 | return validatePayload(r.Context(), raw, o) |
| 391 | } |
| 392 | |
| 393 | func decodeURLValues(values url.Values, paths []jsonschemax.Path, o *httpDecoderOptions) (json.RawMessage, error) { |
| 394 | raw := json.RawMessage(`{}`) |
no test coverage detected