ring-clojure / ring-codec

Utility library for encoding and decoding data
MIT License
63 stars 30 forks source link

Nested map encoding #19

Closed hosiawak closed 6 years ago

hosiawak commented 6 years ago

This library doesn't handle nested maps in a format that can be later decoded with form-decode, eg.:

(ring.util.codec/form-encode {:a {:b 1 :c 2}}) ;; "a=b=1&c=2"

(ring.util.codec/form-decode "a=b=1&c=2") ;; {"a" "b=1", "c" "2"}

Specifically it should handle nested params in such a way that it is then parseable by ring.middleware.nested-params eg.:

(ring.util.codec/form-decode "a%5Bb%5D=1&a%5Bc%5D=2") ;; {"a[b]" "1", "a[c]" "2"}

(ring.middleware.nested-params/nested-params-request {:params {"a[b]" "1" "a[c]" "2"}}) ;; {:params {"a" {"b" "1", "c" "2"}}}

Is this something we could fix ?

weavejester commented 6 years ago

The form-encode and form-decode functions are designed to encode and decode data in the application/x-www-form-urlencoded format. The format used by the nested-params middleware is a custom format on top of that. If you want functions to encode/decode in the format of the nested-params middleware, then the functions should be placed in the nested-params middleware namespace.

The first example you give should probably either raise an error or put the edn-encoded map in, instead.

devurandom commented 1 year ago

This would be a very useful addition. Could it be implemented by adding an optional flag to ring.util.codec/form-encode?

The first example you give should probably either raise an error or put the edn-encoded map in, instead.

In version 1.2.0 this does not raise an error:

(require 'ring.util.codec)
;=> nil
(ring.util.codec/form-encode {:a {:b 2}})
;=> "a=b=2"

Should this issue be reopened?

weavejester commented 1 year ago

I think open a separate issue, one specifically for better handling the error caused by nested maps.

devurandom commented 1 year ago

I think open a separate issue, one specifically for better handling the error caused by nested maps.

Done: https://github.com/ring-clojure/ring-codec/issues/42