autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Need a way to specify a constant header value in some operations

Open bitsforthought opened this issue 11 years ago • 3 comments

Our API is OData V4 compliant, which requires that PUT requests by default return an empty response and HTTP 204, unless you specify "return=representation" for the "Prefer" header. The pattern we're aiming for with our SDK is for CreateOrUpdate (a.k.a. PUT) to always return the model object that was passed to it, which means that we always need to send Prefer="return=representation" on PUT requests. Currently there is no way in Swagger to specify a header that doesn't become a parameter of the operation. In this case, we don't want it to be a parameter because its value should always be the same.

I can think of two approaches to this. One is to add a single extension, something like:

"x-ms-constant-header": { "name": "Prefer", "value": "return=representation" }

Another would be to add two extensions to Swagger parameters that have more general semantics:

{
  "name": "Prefer",
  "type": "string",
  "in": "header",
  "x-ms-client-default": "return=representation",  // Tells code-gen to assign parameter to constant value
  "x-ms-hidden": true  // Omits the parameter in the operation method signature; Must be used with x-ms-client-default
}

I prefer option 2. I can see the two more primitive extensions being very useful, especially if they can also be applied to model properties. Also, it avoids any potential complexity of representing different data types for header values, since Swagger parameters already describe those.

@devigned @Azure/adx-sdk-team Thoughts on approach?

bitsforthought avatar Nov 12 '15 04:11 bitsforthought

It looks like we need to add support for constant parameters sooner or later. Why don't we do something like "x-ms-constant": "value" on a parameter and property element?

stankovski avatar Nov 12 '15 05:11 stankovski

:+1:

That definitely makes sense on a parameter... Plus it automatically implies that the parameter doesn't need to be exposed in the method signature. Not sure if it would be as useful in a property though.

BTW, I found an existing extension to meet my need for "x-ms-hidden": "x-ms-external".

bitsforthought avatar Nov 12 '15 05:11 bitsforthought

This is supported as of https://github.com/Azure/autorest/pull/674

stankovski avatar Feb 06 '16 01:02 stankovski