mplanchard / falcon-marshmallow

Marshmallow serialization/deserialization middleware for Falcon
MIT License
4 stars 2 forks source link

Error Data Structure Malformatted #13

Open 0mars opened 4 years ago

0mars commented 4 years ago

When I call an endpoint with an unknown name I get the json data structure as a string in the description field:

{
    "title": "422 Unprocessable Entity",
    "description": "{\"myInvalidFieldName\": [\"Unknown field.\"]}"
}

marshmallow==3.2.0 falcon-marshmallow==0.3.0

0mars commented 4 years ago

this is what I did before to put it in a different field:

class RequestLoader(Marshmallow):
    def process_resource(self, *args, **kwargs):
        try:
            self.process_resource_inner(*args, **kwargs)
        except ValidationError as err:
            raise HTTPValidationError(status=falcon.status_codes.HTTP_400, errors=err.messages)
        except ValueError as err:
            raise falcon.HTTPError(status=falcon.status_codes.HTTP_400, title='Validation Error', description=str(err))
{
    "title": "400 Bad Request",
    "errors": {
        "namex": [
            "Unknown field."
        ]
    }
}

let me know if you want me to create a PR

falcon-marshmallow==0.2.0 marshmallow==3.0.1

mplanchard commented 4 years ago

Hey there! So the behavior you're expecting here is that additional fields are ignored?

I'm thinking this is due to marshmallow 3 and above defaulting to strict mode. Maybe we can add an option to set it to non-strict?

0mars commented 4 years ago

No, the errors are a JSON string inside a JSON string instead of just JSON all without being treated as a string

On Thu, 26 Sep 2019, 03:11 Matthew Planchard, notifications@github.com wrote:

Hey there! So the behavior you're expecting here is that additional fields are ignored?

I'm thinking this is due to marshmallow 3 and above defaulting to strict mode. Maybe we can add an option to set it to non-strict?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mplanchard/falcon-marshmallow/issues/13?email_source=notifications&email_token=AAIY6WZRC7XEMMPG6GLAMM3QLQD4FA5CNFSM4IX4M432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7T47FQ#issuecomment-535285654, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIY6W6XRQKK6XXRK7FWKSLQLQD4FANCNFSM4IX4M43Q .

mplanchard commented 4 years ago

Oh, okay. So you're looking for a nested object, then?

0mars commented 4 years ago

yes, that's what I meant

On Fri, Sep 27, 2019 at 2:56 AM Matthew Planchard notifications@github.com wrote:

Oh, okay. So you're looking for a nested object, then?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mplanchard/falcon-marshmallow/issues/13?email_source=notifications&email_token=AAIY6W5HMVIOFKRELD5YXPLQLVK2FA5CNFSM4IX4M432YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7XLUJA#issuecomment-535738916, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIY6W7QV6KVOYUWCGS7RXDQLVK2FANCNFSM4IX4M43Q .

mplanchard commented 4 years ago

This is interesting. The Falcon API explicitly states that error descriptions should be strings, which is why, I think, I did things the way they are originally.

It seems like we should be able to override the existing falcon error class and add an errors attribute expecting a dictionary, while also properly serializing using to_json() and to_xml()