autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Designate "global" parameters explicitly.

Open NiklasGustafsson opened this issue 10 years ago • 12 comments

Autorest has adopted the convention that parameters that are declared in the top-level ‘parameter’ section of a Swagger specification, are by implication “global” in the sense of being passed on the generated client instance instead of to each operation that requires them.

This may not be ideal in general, as the top-level section is specifically defined (see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parametersDefinitionsObject) not to imply any kind of "globalness" about the parameters declared therein. The parameters section is, like the definitions section is for payload models, a convenient location to declare parameters that are used in many APIs, whether “global” or not.

Thus, it would seem reasonable to add a vendor extension to explicitly mark parameters (regardless or where they are found) as “global” in this code-generation-centric sense.

Something like:

"ApiVersionParameter": { "name": "x-ms-version", "in": "header", "required": false, "type": "string", "x-ms-global": true, "enum": [ "2015-04-05", "2014-02-14", "2013-08-15", "2012-02-12", "2011-08-18", "2009-09-19", "2009-07-17", "2009-04-14" ], "default": "2015-04-05" }

NiklasGustafsson avatar Mar 03 '16 16:03 NiklasGustafsson

Agreed. A common use case for Azure services in particular is ClientRequestId. It would be great to be able to define it only once in the Swagger file without exposing it as a property on the client.

bitsforthought avatar Mar 05 '16 22:03 bitsforthought

  • By default global parameters are not set as properties on the client.
  • If the global parameter has an extension "x-ms-client-property": true then it will be set as a property on the client.
    • On other note, if some one wants to treat something as a client property, will this extension be more useful if it can be applied to any parameter or a model property?

What does everyone think about this proposal?

amarzavery avatar Mar 16 '16 16:03 amarzavery

If by 'global parameter' you mean a parameter defined in the top-level 'parameters' section, I think we should separate where a parameter is defined from the semantics of 'x-ms-client-property' (which I think is a better name than what I suggested).

In other words, it should be available, and have the same effect, regardless of whether it is declared under an operation, or whether it's declared in the 'parameters' section.

What would it mean for a model property to be a client property? That the property is filled from the property when sent? What about models that are used in response bodies -- does the client property get modified from it?

NiklasGustafsson avatar Mar 16 '16 17:03 NiklasGustafsson

In order to avoid existing Swagger docs being impacted by this extension, the default value may have to be different for parameters declared globally and parameters declared directly under operations.

NiklasGustafsson avatar Mar 16 '16 17:03 NiklasGustafsson

I still think that our original convention was an easy way for someone to get a client with a certain shape without having to worry about extensions. I would treat this extensions as an override for the original behavior so as not to break existing clients and maintain backwards compatibility.

The design should be:

  • Every parameter is treated as either "method" or "client"
  • By default globally defined parameters are treated as "client" parameters and locally defined parameters are treated as "method" parameters
  • "x-ms-client-property": true or "x-ms-client-property": false allows to override this logic - it can be applied on either globally defined or locally defined parameters

@NiklasGustafsson model based parameters should be treated just like a primitive parameter. If there is a body parameter that is marked "x-ms-client-property": true then it will be exposed on the client. The user will be responsible for setting it's value and AutoRest should apply the same validation logic to it as to a method parameter.

stankovski avatar Mar 16 '16 18:03 stankovski

I am in sync with what Denis says. In that way we have lesser breaking changes and we can support new functionality.

@stankovski - I think Niklas' underlying question is:

  • should we then not expose the body parameter on the method if, it is marked as a client property and use it (this.client.foo) during serialization or still expose the body parameter in the method, mark it as optional and use it only if it is provided explicitly as a method parameter? If this needs to be done for a property of a complex model definition then this gets tricky.

I think having this extension only on global parameters is simpler and cleaner.

amarzavery avatar Mar 16 '16 19:03 amarzavery

I would not have any special logic for body vs non-body parameters. If the parameter is a "client" one, it should not show up on the method. Another option is to have an enum instead of bool (e.g. "x-ms-client-property": "client|method|both"). This may be an interesting feature that would allow having something like SubscriptionId defined on the client, yet allowing a user to overwrite it.

As far as having the extension on global parameters only, I think since you are already exposing this customization, why not allow it both ways.

stankovski avatar Mar 16 '16 19:03 stankovski

I agree with the 'override' semantics (as my second comment today meant to imply). If that's the semantics, then maybe it should be reflected in the name?

NiklasGustafsson avatar Mar 16 '16 19:03 NiklasGustafsson

Wow. Enum is really interesting. I would like to know what should we do when we have both specified in the spec.

If the spec has "x-ms-client-property": "both", for a parameter (irrespective of its location {path|query|body|header|formdata})

  1. what should happen if the parameter is required?
    1. if we keep it required on the method then local takes precedence no matter what
    2. if we make such a required parameter optional for that method, then
      1. if the user provides it, use that
      2. else use the global one (set as a client property) since it is a required parameter
  2. what should happen if the parameter is optional?
    1. if the user provides it then use it. Do not use the global (set as a client property) as the parameter is optional
  3. what happens if the parameter is a constant (required and an enum with one value)?
    1. Currently we do not expose constants. I think this behavior should precede no matter what is provided to the x-ms-client-property extension. I am saying this in context of 1.ii

amarzavery avatar Mar 16 '16 19:03 amarzavery

This is getting complicated. The override, Boolean, semantics is simple.

NiklasGustafsson avatar Mar 16 '16 20:03 NiklasGustafsson

I agree. We shouldn't complicate this feature. Having said that I would still go with enum value "x-ms-property-location": "client|method" to allow us to add this functionality in the future.

Also the above scenarios are too complicated. If there is a concept of "both" the algorithm should be: At the beginning of method check if arg is null, if so, set arg to client.Property. That's it.

stankovski avatar Mar 16 '16 23:03 stankovski

Howdy!

In our planning for driving towards a stable '1.0' release, I'm marking this issue as 'deferred' :zzz: and we're going to review it during the post-1.0 planning cycle.

It's not to say that we're not going to work on it, or that this isn't not important, but at the moment, we're picking and choosing the stuff we must do before 1.0. :horse_racing: :horse_racing: :horse_racing:

We'll make sure we pick this back up at that point. :tada:

fearthecowboy avatar Jun 15 '16 16:06 fearthecowboy