Skip to content

Commit f439b22

Browse files
authored
Merge pull request #774 from mbj/fix/reject-unknown-fields-with-field-label-modifier
Fix reject unknown fields with field label modifier
2 parents 4d12557 + cd5fea4 commit f439b22

File tree

6 files changed

+61
-8
lines changed

6 files changed

+61
-8
lines changed

Data/Aeson/TH.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ parseRecord jc tvMap argTys opts tName conName fields obj inTaggedObject =
939939
if inTaggedObject then (tagFieldName (sumEncoding opts) :) else id
940940
knownFields = appE [|H.fromList|] $ listE $
941941
map (\knownName -> tupE [appE [|T.pack|] $ litE $ stringL knownName, [|()|]]) $
942-
tagFieldNameAppender $ map nameBase fields
942+
tagFieldNameAppender $ map (fieldLabel opts) fields
943943
checkUnknownRecords =
944944
caseE (appE [|H.keys|] $ infixApp (varE obj) [|H.difference|] knownFields)
945945
[ match (listP []) (normalB [|return ()|]) []

Data/Aeson/Types/FromJSON.hs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1260,15 +1260,15 @@ instance (ProductFromJSON arity f, ProductSize f
12601260
--------------------------------------------------------------------------------
12611261

12621262
class FieldNames f where
1263-
fieldNames :: f a -> [Text] -> [Text]
1263+
fieldNames :: f a -> [String] -> [String]
12641264

12651265
instance (FieldNames a, FieldNames b) => FieldNames (a :*: b) where
12661266
fieldNames _ =
12671267
fieldNames (undefined :: a x) .
12681268
fieldNames (undefined :: b y)
12691269

12701270
instance (Selector s) => FieldNames (S1 s f) where
1271-
fieldNames _ = (pack (selName (undefined :: M1 _i s _f _p)) :)
1271+
fieldNames _ = (selName (undefined :: M1 _i s _f _p) :)
12721272

12731273
class RecordFromJSON arity f where
12741274
recordParseJSON
@@ -1282,9 +1282,10 @@ instance ( FieldNames f
12821282
\obj -> checkUnknown obj >> recordParseJSON' p obj
12831283
where
12841284
knownFields :: H.HashMap Text ()
1285-
knownFields = H.fromList $ map (,()) $
1286-
fieldNames (undefined :: f a)
1287-
[pack (tagFieldName (sumEncoding opts)) | fromTaggedSum]
1285+
knownFields = H.fromList $ map ((,()) . pack) $
1286+
[tagFieldName (sumEncoding opts) | fromTaggedSum] <>
1287+
(fieldLabelModifier opts <$> fieldNames (undefined :: f a) [])
1288+
12881289
checkUnknown =
12891290
if not (rejectUnknownFields opts)
12901291
then \_ -> return ()

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
For the latest version of this document, please see [https://github.com/bos/aeson/blob/master/changelog.md](https://github.com/bos/aeson/blob/master/changelog.md).
22

3+
## 1.5.0.0
4+
* Fix bug in `rejectUnknownFields` not respecting `fieldLabelModifier`, thanks to Markus Schirp.
5+
36
#### 1.4.7.1
47

58
* GHC 8.10 compatibility, thanks to Ryan Scott.

tests/ErrorMessages.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tests :: [TestTree]
3131
tests =
3232
[ aesonGoldenTest "simple" "tests/golden/simple.expected" output
3333
, aesonGoldenTest "generic" "tests/golden/generic.expected" (outputGeneric G)
34+
, aesonGoldenTest "generic" "tests/golden/th.expected" (outputGeneric TH)
3435
]
3536

3637
output :: Output
@@ -140,7 +141,7 @@ outputGeneric choice = concat
140141
(select
141142
thSomeTypeParseJSONRejectUnknownFields
142143
gSomeTypeParseJSONRejectUnknownFields)
143-
[ "{\"tag\": \"record\", \"testOne\": 1.0, \"testZero\": 1}"
144+
[ "{\"tag\": \"record\", \"testone\": 1.0, \"testZero\": 1}"
144145
, "{\"testZero\": 1}"
145146
, "{\"tag\": \"record\", \"testone\": true, \"testtwo\": null, \"testthree\": null}"
146147
]

tests/golden/generic.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Error in $: not enough input. Expecting json list value
3434
SomeType (reject unknown fields)
3535
Error in $: parsing Types.SomeType(Record) failed, unknown fields: ["testZero"]
3636
Error in $: parsing Types.SomeType failed, expected Object with key "tag" containing one of ["nullary","unary","product","record","list"], key "tag" not found
37-
Error in $: parsing Types.SomeType(Record) failed, unknown fields: ["testtwo","testone","testthree"]
37+
Error in $.testone: parsing Double failed, unexpected Boolean
3838
Foo (reject unknown fields)
3939
Error in $: parsing Types.Foo(Foo) failed, unknown fields: ["tag"]
4040
Foo (reject unknown fields, tagged single)

tests/golden/th.expected

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
OneConstructor
2+
Error in $: When parsing the constructor OneConstructor of type Types.OneConstructor expected Array but got String.
3+
Error in $: When parsing the constructor OneConstructor of type Types.OneConstructor expected an empty Array but got Array of length 1.
4+
Nullary
5+
Error in $: When parsing Types.Nullary expected a String with the tag of a constructor but got X.
6+
Error in $: When parsing Types.Nullary expected String but got Array.
7+
SomeType (tagged)
8+
Error in $: parsing Int failed, expected Number, but encountered Boolean
9+
Error in $: key "contents" not found
10+
Error in $: When parsing the record record of type Types.SomeType the key testone was not present.
11+
Error in $.testone: parsing Double failed, unexpected Boolean
12+
Error in $: When parsing Types.SomeType expected an Object with a tag field where the value is one of [nullary, unary, product, record, list], but got X.
13+
Error in $: key "tag" not found
14+
Error in $: When parsing Types.SomeType expected Object but got Array.
15+
SomeType (single-field)
16+
Error in $: parsing Int failed, expected Number, but encountered Object
17+
Error in $: parsing Int failed, expected Number, but encountered Array
18+
Error in $: When parsing Types.SomeType expected an Object with a single tag/contents pair where the tag is one of [nullary, unary, product, record, list], but got X.
19+
Error in $: When parsing Types.SomeType expected an Object with a single tag/contents pair but got 2 pairs.
20+
Error in $: When parsing Types.SomeType expected an Object with a single tag/contents pair but got 0 pairs.
21+
Error in $: When parsing Types.SomeType expected Object but got Array.
22+
Error in $: not enough input. Expecting ':'
23+
Error in $: not enough input. Expecting object value
24+
Error in $: not enough input. Expecting ',' or '}'
25+
SomeType (two-element array)
26+
Error in $: parsing Int failed, expected Number, but encountered Boolean
27+
Error in $: When parsing the constructor Record of type Types.SomeType expected Object but got Null.
28+
Error in $: When parsing Types.SomeType expected a 2-element Array with a tag and contents element where the tag is one of [nullary, unary, product, record, list], but got X.
29+
Error in $: When parsing Types.SomeType expected an Array of 2 elements where the first element is a String but got Null at the first element.
30+
Error in $: When parsing Types.SomeType expected an Array of 2 elements but got 0 elements
31+
Error in $: When parsing Types.SomeType expected Array but got Object.
32+
Error in $: not enough input. Expecting ',' or ']'
33+
Error in $: not enough input. Expecting json list value
34+
SomeType (reject unknown fields)
35+
Error in $: Unknown fields: ["testZero"]
36+
Error in $: key "tag" not found
37+
Error in $.testone: parsing Double failed, unexpected Boolean
38+
Foo (reject unknown fields)
39+
Error in $: Unknown fields: ["tag"]
40+
Foo (reject unknown fields, tagged single)
41+
Error in $: Unknown fields: ["unknownField"]
42+
EitherTextInt
43+
Error in $: When parsing the constructor NoneNullary of type Types.EitherTextInt expected String but got String.
44+
Error in $: When parsing the constructor NoneNullary of type Types.EitherTextInt expected String but got Array.
45+
Product2 Int Bool
46+
Error in $: expected Bool, but encountered Null
47+
Error in $: When parsing the constructor Product2 of type Types.Product2 expected Array of length 2 but got Array of length 0.
48+
Error in $: When parsing the constructor Product2 of type Types.Product2 expected Array but got Object.

0 commit comments

Comments
 (0)