(r *http.Request, destination interface{}, o *httpDecoderOptions)
| 301 | } |
| 302 | |
| 303 | func decodeJSONForm(r *http.Request, destination interface{}, o *httpDecoderOptions) error { |
| 304 | if o.jsonSchemaCompiler == nil { |
| 305 | return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to decode HTTP Form Body because no validation schema was provided. This is a code bug.")) |
| 306 | } |
| 307 | |
| 308 | paths, err := jsonschemax.ListPathsWithRecursion(r.Context(), o.jsonSchemaRef, o.jsonSchemaCompiler, o.maxCircularReferenceDepth) |
| 309 | if err != nil { |
| 310 | return errors.WithStack(herodot.ErrInternalServerError.WithTrace(err).WithReasonf("Unable to prepare JSON Schema for HTTP Post Body Form parsing: %s", err).WithDebugf("%+v", err)) |
| 311 | } |
| 312 | |
| 313 | reader, err := requestBody(r, o) |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
| 317 | |
| 318 | var interim json.RawMessage |
| 319 | if err := json.NewDecoder(reader).Decode(&interim); err != nil { |
| 320 | return errors.WithStack(herodot.ErrBadRequest.WithError(err.Error()).WithReason("Unable to decode form as JSON.")) |
| 321 | } |
| 322 | |
| 323 | parsed := gjson.ParseBytes(interim) |
| 324 | if !parsed.IsObject() { |
| 325 | return errors.WithStack(herodot.ErrBadRequest.WithReasonf("Expected JSON sent in request body to be an object but got: %s", parsed.Type.String())) |
| 326 | } |
| 327 | |
| 328 | values := url.Values{} |
| 329 | parsed.ForEach(func(k, v gjson.Result) bool { |
| 330 | values.Set(k.String(), v.String()) |
| 331 | return true |
| 332 | }) |
| 333 | |
| 334 | if o.queryAndBody { |
| 335 | _ = r.ParseForm() |
| 336 | for k := range r.Form { |
| 337 | values.Set(k, r.Form.Get(k)) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | raw, err := decodeURLValues(values, paths, o) |
| 342 | if err != nil { |
| 343 | return err |
| 344 | } |
| 345 | |
| 346 | if err := json.Unmarshal(raw, destination); err != nil { |
| 347 | return errors.WithStack(err) |
| 348 | } |
| 349 | |
| 350 | return validatePayload(r.Context(), raw, o) |
| 351 | } |
| 352 | |
| 353 | func decodeForm(r *http.Request, destination interface{}, o *httpDecoderOptions) error { |
| 354 | if o.jsonSchemaCompiler == nil { |
no test coverage detected