-
Notifications
You must be signed in to change notification settings - Fork 98
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
Handle datetime objects #206
base: master
Are you sure you want to change the base?
Conversation
This patch adds support for date and datetime objects. Since the formatter creates these objects from type: string format: date and format: date-time specs, it's helpful to handle the reverse translation so that data retrieved using GET can be passed into subsequent POST/PATCH/PUT calls.
This patch adds support for date and datetime objects. Since the formatter creates these objects from type: string format: date and format: date-time specs, it's helpful to handle the reverse translation so that data retrieved using GET can be passed into subsequent POST/PATCH/PUT calls.
…ravado-core into handle-datetime-objects
@bobh66 could you give more context around the error that you're reporting and fixing? And potentially ways of reproducing it?
I tried to reproduce it on empty_swagger_spec = Spec(spec_dict={})
param_spec = {
'in': 'body',
'name': 'body',
'schema': {
'type': 'string',
'format': 'date'
},
}
param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec)
request = {
'headers': {
}
}
marshal_param(param, datetime.date(2017, 10, 26), request) == datetime.date.today().isoformat()
assert request == {
'headers': {'Content-Type': APP_JSON},
'data': '"2017-10-26"',
} |
@macisamuele - thanks - you helped me realize that it's only a problem with nested datetime objects, which is what I'm working with. I have a top-level body parameter that is defined as type: object because it could be any of multiple different things depending on what's happening, and it's the embedded datetime objects that are seeing the problem. I updated the tests to cause the problem and pushed a new patchset. if you try to marshal {'updated_at": datetime(2017, 10, 26)} as type "object" it will fail on master. Thanks |
@bobh66 now I understand your issue. Have you tried to specify the schema of the object? {
"type": "object",
"properties": {
"updated_at": {"type": "string", "format": "date-time"}
}
} |
@macisamuele - I'm sure it would work if I could specify the schema, but the application requires me to accept a variety of different object schemas, and since Swagger2.0 doesn't support anyOf, there isn't much I can do beyond using object. Even if anyOf was supported there are scenarios where I simply don't know what the object will be until I receive it, so it's impossible to specify all of the possible schemas. For example, in one case I need to supply JSON object inputs to a workflow engine, which vary depending on the workflow that is being executed, and the workflows can change over time. I don't want to have to specify the schema for every possible workflow input object, which is impossible to keep up with, so I have to call them objects and rely on the workflow engine to do the validation. |
What you're describing is a problem with the specs and not an issue with the library. For what's my understanding of your problem you have a python application (could be a script or could be a service) that communicates with a swagger service using bravado-core. I don't understand how you could have a If you're not able to workaround your issue in any different way I would suggest you to open a PR to make |
The datetime object comes from bravado converting the response of a separate query based on the spec for that response. Then I have to take that response, modify it and pass it to another service nested inside another object. Yes I could convert the datetime fields at that point, but I thought it was reasonable to expect bravado to accept the datetime object that it generated in the first place. I understand that it does that today just fine at the top level, and it would work at lower levels if I could spec it that way, but I can't specify something that can be changed at will by the user at a higher level. I considered making dumps configurable but I thought that was a rather complicated solution to a simple problem, and I avoid complexity whenever I can. If that's the preferred solution I'll go that way instead. |
bravado converted the string representation of a datetime (ISO8001) to a datetime because that was made explicit from specs
it did it correctly at top level because you specified type and format that instructed bravado-core to do that
having the default behavior that is not compliant with what the specs define is riskier.
If you're using bravado, probably you can use |
This patch adds support for nested date and datetime objects when the schema only defines
type: object with no further specification. The lack of anyOf support in Swagger2.0 and the need to support generic object parameters make it impossible to always specify the exact schema of an object.
When a datetime object is embedded in an object that has no specified schema, the json.dumps() would fail without a handler for datetime types.