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
request.content.decode should treat empty strings for optionals as nil #3046
Comments
If i understand correctly, you basically want a I realize this is not nice to work around, but i don't think adding this as the default behavior is a good idea, considering it will be inconsistent with the usual You can use a custom There are a number of other workarounds to this problem too, like using property wrappers, which is more complicated, see https://gist.github.com/MahdiBM/260a6551f4b9a1af1994f44e305c313e as an example. |
The opposite - I want an empty string to be decoded as |
Ah OK. But again, this isn't something that a Decoder should care about. If we change it to turn empty strings to nil, there might be another user complaining than they would like the original empty string. Other than the original argument which is that this is inconsistent behavior with all the other decoders in the ecosystem. I think you should try to just check with 'string.isEmpty'. You are not losing any information about the string either. |
Yes, but now I go from “implement Maybe a configuration option to enable this behavior? |
I'm not against a configuration option if it'll be very useful 🤔 I still think you could try to use something like a property wrapper. There are some new Codable macros and stuff like that with Swift 5.9 getting closer to release. On a personal level I hope they can solve some of these issues with Codable sometimes being too uncustomizable. |
If my read of the gist is correct, it’s doing the opposite of what I want - i.e., given a |
What the gist does is decodes |
I'm not sure but from what I am reading this looks like it might be what you're looking for? :) |
I think I could do that, yes, but I'm still of the opinion that a web server framework should support " |
I'm faced with the same problem: I need to handle a callback from a third-party service that insists on sending keys with null values rather than excluding them. These payload contains a variety of types (strings, ints, dates, etc.) and therefore I seem to be stymied. ( Update: in the mean time I've worked around this by creating a middleware that strips out such empty entries from the request which I attach to the affected endpoints. |
Describe the bug
I've got a custom
Codable
type that throws an error when decoding... because it's being asked to decode an empty string, rather than being skipped because I've gotT?
rather thanT
as the type in myContent
.To Reproduce
Codable
type that throws an error if told to decode from an empty string.Content
struct that has anOptional
of your custom type as a parameter.yourThing:
in the body. (Doable in Safari by creating an HTML form, giving it a date input with the rightname
, and then... not selecting a date before hitting submit)Expected behavior
Vapor should see that it was given a blank value and decode that as
nil
, not tell my custom type to try to decode.Environment
Additional context
I suspect this is the same kind of thing as #2460, and that a similar fix to #2463 would work - modifying
URLEncodedFormDecoder._Decoder.KeyedContainer.decodeNil(forKey:)
to also check for empty values ought to do it? (I did some poking at it in the debugger, and... == nil || (self.data.children[key.stringValue]?.values.count == 1 && self.data.children[key.stringValue]?.values.first == .urlDecoded(""))
worked right, but feels a little clunky.)The text was updated successfully, but these errors were encountered: