Skip to content

Commit 865bc29

Browse files
committed
Fix rejectUnknownFields to respect fieldLabelModifier
* Apply `fieldLabelModifier` to known fields reflected from the `FieldName` class. While NOT applying the `fieldLabelModofier` to the encoding tags. * Change the intermediary type returned by the `FieldName` class from `Text` to `String` to reduce `{un,}pack` calls to a minimum. * Update tests which specified the problem before to assert the fixed semantics. [fix #773]
1 parent 5233387 commit 865bc29

File tree

6 files changed

+11
-9
lines changed

6 files changed

+11
-9
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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ For the latest version of this document, please see [https://github.com/bos/aeso
33
#### 1.4.7.1
44

55
* GHC 8.10 compatibility, thanks to Ryan Scott.
6+
* Fix bug in `rejectUnknownFields` not respecting `fieldLabelModifier`.
67

78
### 1.4.7.0
89

tests/ErrorMessages.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ outputGeneric choice = concat
141141
(select
142142
thSomeTypeParseJSONRejectUnknownFields
143143
gSomeTypeParseJSONRejectUnknownFields)
144-
[ "{\"tag\": \"record\", \"testOne\": 1.0, \"testZero\": 1}"
144+
[ "{\"tag\": \"record\", \"testone\": 1.0, \"testZero\": 1}"
145145
, "{\"testZero\": 1}"
146146
, "{\"tag\": \"record\", \"testone\": true, \"testtwo\": null, \"testthree\": null}"
147147
]

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

+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 $: Unknown fields: ["testZero"]
3636
Error in $: key "tag" not found
37-
Error in $: Unknown fields: ["testtwo","testone","testthree"]
37+
Error in $.testone: parsing Double failed, unexpected Boolean
3838
Foo (reject unknown fields)
3939
Error in $: Unknown fields: ["tag"]
4040
Foo (reject unknown fields, tagged single)

0 commit comments

Comments
 (0)