autorest icon indicating copy to clipboard operation
autorest copied to clipboard

What is the expected behavior of `default` in the Swagger specification?

Open matthchr opened this issue 10 years ago • 4 comments

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:

  1. Are we consistently treating default the same across default in operations and default in the body? I know that the body supports default but I can't seem to find any tests for it except Complex-Model which 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.
  2. 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.
  3. What happens if I have a property which is both required and default -- 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.

matthchr avatar Nov 14 '16 19:11 matthchr

Note that this is in some way philosophically related to my other question: #1583

matthchr avatar Nov 14 '16 19:11 matthchr

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.

bitsforthought avatar Nov 14 '16 19:11 bitsforthought

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.

fearthecowboy avatar Jan 31 '17 16:01 fearthecowboy

added to https://github.com/Azure/autorest/issues/2448

olydis avatar Sep 27 '17 21:09 olydis