MCPcopy
hub / github.com/ory/hydra / decodeForm

Function decodeForm

oryx/decoderx/http.go:353–391  ·  view source on GitHub ↗
(r *http.Request, destination interface{}, o *httpDecoderOptions)

Source from the content-addressed store, hash-verified

351}
352
353func 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
393func decodeURLValues(values url.Values, paths []jsonschemax.Path, o *httpDecoderOptions) (json.RawMessage, error) {
394 raw := json.RawMessage(`{}`)

Callers 1

DecodeFunction · 0.85

Calls 9

requestBodyFunction · 0.85
decodeURLValuesFunction · 0.85
validatePayloadFunction · 0.85
WithDebugMethod · 0.80
WithDebugfMethod · 0.80
WithTraceMethod · 0.80
DecodeMethod · 0.65
ErrorMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected