What is the expected behavior of `default` in the Swagger specification?
When I read default in the Swagger spec, what I see is "optional, and if omitted the server will use the following value."
It seems like what actually happens at least in some languages is that AutoRest uses the default in the signature (for languages which support that such as Python and C#) and we end up with a signature which looks like this:
DoFoo(string required, string optional = null, string default = "Test")
This actually ends up always serializing the string "Test" to the server. Which means as far as the server is concerned, this value is now never omitted.
So I have a few questions about the expected behavior of default in the clients:
- Are we consistently treating default the same across
defaultin operations anddefaultin the body? I know that the body supportsdefaultbut I can't seem to find any tests for it exceptComplex-Modelwhich only seems to be generated for Node.JS -- although from some investigation into the Python generator by @annatisch on my team it seems at least python treats the two consistently. - Is it desirable to actually serialize the default value across the wire? Taking this to it's extreme (which my service probably does...) if we have an object with 2 required properties and 15 optional properties. The defaults of these optional properties may be documentable in the Swagger specification, and if they were documented we would serialize 17 values instead of 2.
- What happens if I have a property which is both
requiredanddefault-- and is the behavior the same in the body and the operation parameters?
Overall I think the win of including default probably outweighs the awkwardness of always serializing that value to the server (better method signatures, etc), but curious what others think or if anybody has an alternative option that is the best of both worlds.
Note that this is in some way philosophically related to my other question: #1583
My take on default is similar to my take on client-side validation of Swagger constraints like minimum, maximum, etc.: The client should not be "smart". Assume the service knows what it's doing, and avoid the extra coupling and wasted bandwidth of redundant default values being sent over the wire.
It's ok for the client to be smart, just not "smart" :D
IMHO, default values should not be sent across the wire.
I don't particularly like the current assumption/behavior, but we can't change that right now.
AutoRest 1.0 has a number of implicit, not-well-documented assumptions that are not overridable; this will change drastically in AutoRest 2.0, where we will document every single assumption and provide the ability to change the assumption via configuration.
added to https://github.com/Azure/autorest/issues/2448