connexion
connexion copied to clipboard
Support for date and date-time type casting
It would be great to have type casting of formats 'string/date-time' and 'string/date' to python datetime.datetime and datetime.date. I guess this would need custom flask converters to be added to url_map (http://flask.pocoo.org/docs/0.11/api/#flask.Flask.url_map)
@cbornet You have access to the Flask.app using the attribute .app
from the object connexion.App
. So you can use the Flask converters you want. And for the payloads you can also add custom converters, please see an example here: http://connexion.readthedocs.io/en/latest/cookbook.html#custom-type-format
Thanks for your link but it seems it's only for format validation, not conversion. I think what would be needed is to enhance Operation::get_path_parameter_types() to also read the format and PATH_PARAMETER_CONVERTERS for date and date-time
@cbornet You are right. Are you planning on working in a PR for that?
Any update on this?
related: a pr for serializing date-time #851
deserializing to datetimes would be cool too!
it would be great to create a generic solution that could support other formats like uuid
s and uri
s.
also related is #760 , which is a work-in-progress to make pluggable deserializers based on content-type. One could override them to also add more complex casting.
Is it possible to plug into jsonschema
's FormatChecker
validation, and modify the value in place? Is it something we should request from them as a feature?
There's a similar project to jsonschema called Cerberus
developed by the Eve
guys which I have used before. On top of pure validation, it allows for normalization rules to get a normalized version of the validated input, e.g. with types casted. It even allows plugging in custom handlers, so you could transform input / output data whatever way you want to.
Something similar would be useful here I guess.
I tried extending .apis.flask_utils.flaskify_path
(and related), and adding a custom flask.Flask.url_map
to convert date
parameters into datetime.date
.
That approach was unsuccessful, since the parameter is validated after the flask conversion in validate_path_parameter
(and the converted datetime.date
cannot be verified against the schema).
So I'm pretty sure the best way forward is by hooking converters into the schema validation.
I was going to ask whether this would be supported in jsonschema
, but the question has already been raised a few times (see Julian/jsonschema#441, Julian/jsonschema#195 and Julian/jsonschema#475).
Following the suggestions in these issues, I'd like to see that we shift our efforts toward Seep, a "transformation and data extraction language on top of JSON Schema".
Any plans to get this implemented? I would love to not have to litter our controller code with custom calls to deserialize_date