autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Discussion: dealing with multiple models used with different verbs

Open stankovski opened this issue 10 years ago • 3 comments

Problem: Very often we see swagger specs where the same model behaves differently depending on the HTTP operation. For example in the GET operation a Resource may be returned with all properties filled in. In the PUT operation the same Resource is expected to be provided without ID and Name. In the POST operation the set of expected properties may be shrunk even further allowing only certain properties to be updated.

As a result most of the swagger specs have a number of models to describe a Resource. There is a Resource, CreateResourceParameters, UpdateResourceParameters, etc.

Would like to start this discussion to see what can be done to make both the swagger spec and the generated client look less hackish.

@brjohnstmsft @devigned @NiklasGustafsson @markcowl @Azure/adx-sdk-team

stankovski avatar Mar 05 '16 23:03 stankovski

Option 1:

  • On the client side introduce a common interfaces (where supported) - for example have IResource interface generated which includes only Get for each property.
  • Resource, CreateResourceParameters, and UpdateResourceParameters would implement IResource.
  • All operations that return either of these models would return an interface.
  • The user would be able to easily cast a Resource returned from GET into a model used for POST or PUT
  • The "common" models in swagger (e.g. Resource, CreateResourceParameters, and UpdateResourceParameters) will have an extensions that will tell AutoRest that they should implememt the common interface defined by another model (e.g. x-ms-parent-model: #/definitions/Resource)

stankovski avatar Mar 05 '16 23:03 stankovski

We should define some design goals for the generated code based on what we think the best developer experience is going to be. If I had to pick some goals, they'd be:

  1. Simplicity: The fewer mandatory "infrastructure" model classes/interfaces, the better.
  2. Uniformity: Each concept in the API that is represented as a resource ought to be modeled by a single type on the client that can be used across all operations (Create, CreateOrUpdate, Delete, List).
  3. Safety: Each model type ought to guide the developer away from misuse. Examples of things that the model type should either make impossible or at least discourage are:
    • Trying to modify a read-only property on a PUT or PATCH
    • Trying to set a read-only property on a POST
    • Forgetting to set a required property on a PUT or POST

It looks to me like option 1 above trades off simplicity and uniformity for safety. I would argue we should make the opposite tradeoff, for a couple reasons:

  • The REST API should already be enforcing correct usage. If the API doesn't return 400 for the misuse cases listed above, then it's a broken API. Conversely, if the API is overly restrictive (i.e. -- it returns 400 on a PUT when the client provides the current value of a read-only property), then it's a broken API.
  • The big goal for Azure services is increasing adoption. We've seen in our conversations with customers the huge impact that having a simple client programming model can have. If customers occasionally get a CloudException when they didn't expect it, there are ways we can improve that. On the other hand, if customers have to deal with a more complex programming model and find it painful, that's much harder to fix down the road.

With that in mind, here's my proposed option 2 (leaving ARM-specific stuff until the end, because this is a broader issue than just ARM-based clients):

  • Each API's Swagger should define a single type for each resource (e.g.: Index in Azure Search).
  • Every operation for that resource should be defined in terms of that type (e.g.: Indexes.Get returns an Index, Indexes.List returns a list of Index, and Indexes.Create and Indexes.CreateOrUpdate each take an Index as a parameter).
    • Aside: I've opened an issue to make generated CreateOrUpdate methods more streamlined: https://github.com/Azure/autorest/issues/382
  • The definition of a resource type in the Swagger file should make liberal use of the required and readOnly attributes. AutoRest already does the right thing with required in terms of validation. If we really want early validation, it can enforce readOnly too, but only for POST (because only the server knows whether the value is equal to the current value for PUT or PATCH).
    • Aside: I've opened an issue for better handling of readOnly in terms of nullability here: https://github.com/Azure/autorest/issues/513
  • For ARM-specific resource types, the generated model classes can inherit from a common IResource interface that includes all common properties, but per the design goals above its properties should be mutable so it can be re-used across all operations.

bitsforthought avatar Mar 07 '16 01:03 bitsforthought

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 17:06 fearthecowboy