Support custom converter for request parameters #207
Replies: 9 comments 12 replies
-
Hi @supermomonga. Thanks for starting this discussion. When talking about request parameters, do you also mean the request body or just query parameters? |
Beta Was this translation helpful? Give feedback.
-
One method to add a way to modify all validated properties could be via json_schemers :after_property_validation option (link). We could add an option to set this either globally or only for header, query, cookie, path, body. I have not thought about an interface, yet. Do you have an idea for an interface to do that? |
Beta Was this translation helpful? Give feedback.
-
This looks very smart. Thanks for taking the time to come up with that approach.
So if there are two converters that match only the first match will be applied? I like that. I guess this has to be clearly mentioned somewhere so people don't expect multiple converters to be run in sequence, but this makes sense to me! I am a little hesitant to use pattern matching here since this will be run after validation on every property. I have read somewhere that there still is a performance impact as compared to an |
Beta Was this translation helpful? Give feedback.
-
I think I understand the approach where the part inside the brackets almost looks like a pattern, but is one item of a predefined list. My concern is that maintaining that list would be cumbersome. Here is another one: # Only call the first converter that matches for a given property
module MyConverter
def self.match?(data, property, property_schema, _parent)
property_schema['format'] == 'date-time'
end
def self.call(data, property, property_schema, _parent)
data[property] = DateTime.parse(data[property])
end
end
# use this in every Schema validation
config.converters.append MyConverter
# use this only in query parameters (can be query, path, header, body, cookie)
config.converters.query.prepend MyConverter I think it's a bit bulky, but also easy to understand. What do you think? |
Beta Was this translation helpful? Give feedback.
-
I have another question about the feature in general: Since this feature, at least as it looks in my head right now, would modify the data after validation, it would change the return value of |
Beta Was this translation helpful? Give feedback.
-
Here are three variants that should summarize the above: Variant A – adding
|
Beta Was this translation helpful? Give feedback.
-
Here is another suggested API that touches this topic: #221 |
Beta Was this translation helpful? Give feedback.
-
Hey @supermomonga. I have implemented an interface where you could pass procs to json_schemers after_property_validation on the 1.4.0 branch. The |
Beta Was this translation helpful? Give feedback.
-
There is an API to build custom converters in the "dev" branch. See I am closing this for now, but think this topic will pop up again in the future in a new discussion building on top of the hooks API. |
Beta Was this translation helpful? Give feedback.
-
It would be helpful to have the ability to set custom converters for request parameters.
For example, consider the following cases:
date-time
string toActiveSupport::TimeWithZone
Personally, I would like to use this feature in the openapi_rails_typed_parameters gem I'm developing.
ref: #98 (comment)
Beta Was this translation helpful? Give feedback.
All reactions