Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to decode :enums having keywords #819

Closed
billtuba opened this issue Jan 18, 2023 · 4 comments · Fixed by #849
Closed

Failure to decode :enums having keywords #819

billtuba opened this issue Jan 18, 2023 · 4 comments · Fixed by #849
Assignees
Labels
bug Something isn't working

Comments

@billtuba
Copy link
Contributor

Found in version : 0.10.0
Previously working in : 0.9.2

Reproducible in master : yes
Given schema:

(def Test [:map-of
           [:enum {:encode/json name
                   :decode/json keyword}
            :a :b :c]
           int?])

When I run this:

(let [m (mg/generate Test)
      encoded (m/encode Test m mt/json-transformer)
      decoded (m/decode Test encoded mt/json-transformer)]
  {:generated m
   :encoded encoded
   :decoded decoded})

decode under v0.9.2 behaves properly:

;; => {:generated {:b 621, :a 403, :c -14}, 
          :encoded {"b" 621, "a" 403, "c" -14},
          :decoded {:b 621, :a 403, :c -14}}

decode using v 0.10.0 does not:

;; => {:generated {:b -3, :a -191, :c -1}, 
          :encoded {"b" -3, "a" -191, "c" -1},
          :decoded {nil -1}} ;;; <<< here's the problem

transforming_enums.clj.zip

N.B. If remove/comment out the :enum/:= entries in -json-decoders the code behaves as expected.

@ikitommi ikitommi added the bug Something isn't working label Jan 21, 2023
@ikitommi
Copy link
Member

ikitommi commented Jan 21, 2023

Oh, I see. This is a bug. We are composing :map-of transformers via m/-comp and it has worked before as everything was defined as a function. :enum transformer is a real interceptor, so we should compose it with m/-intercepting. This should be easy to fix.

That said, I think the :map-of transformer is not working correctly anyways as it currently forcefully transforms keys into strings first, giving wrong results:

(m/decode
 [:map-of :int :int]
 {:kikka :kukka}
 (mt/json-transformer))
; => {"kikka" :kukka}

Like :or, it should validate the results after applying key transformation, if not valid, keep the original value.

@opqdonut
Copy link
Member

opqdonut commented Feb 8, 2023

#849 is a fix for the original bug. I'll see if I can also fix the thing @ikitommi mentioned just now.

opqdonut added a commit to opqdonut/malli that referenced this issue Feb 8, 2023
Previously, json-transformer would always decode map keys to strings,
even if the string could not be decoded to the target type.

See metosin#819 (comment)
@opqdonut
Copy link
Member

opqdonut commented Feb 8, 2023

Fixed the thing ikitommi mentioned. New behaviour:

(m/decode
 [:map-of :int :int]
 {:kikka :kukka}
 (mt/json-transformer))
; => {:kikka :kukka}

@opqdonut opqdonut moved this from 🏗 Doing to 👀 In review in Metosin Open Source Backlog Feb 8, 2023
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Metosin Open Source Backlog Feb 8, 2023
@billtuba
Copy link
Contributor Author

billtuba commented Mar 9, 2023

Thanks for getting this in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants