autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Add option to AutoRest to not generate client side validation

Open matthchr opened this issue 10 years ago • 9 comments

In some contexts it seems to make sense to have validation occur on the server (and not on the client). What I mean by validation is things like int foo must be greater than 5. Currently if you specify a minimum in Swagger, AutoRest generates code on the client which enforces that minimum and maximum.

There are upsides to enforcement on the client, for example it may improve unit testability and saves a round trip call to the service. But the downside of enforcement on the client is that the server can change its behavior and the client cannot make use of it.

For example consider the following: An operation List_Foos specifies a "minimum": 1 and "maximum": 10 for it's parameter maxResults

At some point in the future, the service that controls List_Foos overcomes a performance bottleneck and now they can process Foos much faster, so they bump the maxResults maximum to 100. There are a couple of possibilities here:

  1. They only increase the maximum in a new REST API version.
  2. They increase the maximum across all existing REST API versions (what previously had a max of 10 now has a max of 100).

It seems like option 2 above is valid in at least some cases (causes goodness for clients and what seems to be no pain, since the behavior is identical as to what it as before unless clients start opting into the new limits). But what should the Swagger specification look like? Should services which might one day possibly change the bounds of a value in a nonbreaking way just never list the mins/maxs (for example) in their Swagger spec? Should services just never do option 2 above?

One possible alternative might be to allow min/max to not be enforced on the client through the use of some flag, so that they could be there as an "FYI" but not actually enforced until the client hits the server.

matthchr avatar Nov 09 '16 02:11 matthchr

👍 @matthchr

Client-side validation is a versioning problem waiting to happen.

bitsforthought avatar Nov 10 '16 00:11 bitsforthought

My 2 cents on this topic.

  • If the constraint is contractual (i.e. if it associated to an api-version) then it should definitely be described in the spec. Otherwise it should be documented in the property/parameter/operation description in the spec.
  • The description should also mention the constraint with a caveat that the constraint is subject to change. This makes the documentation accurate, customer gets a rough idea about the constraint and clients don't need to be updated as well.

amarzavery avatar Nov 11 '16 23:11 amarzavery

@amarzavery Tying every single API change to a new API version is highly impractical. It is common practice to relax constraints without changing the API version. This is one of the benefits of the loose coupling that comes with REST APIs. Having client-side validation breaks this for zero practical benefit.

If a service relaxes a constraint, the documentation of that constraint should be updated in the spec and therefore the latest version of the client. With client-side validation, users who don't upgrade to the latest client are unable to benefit from the relaxed validation. Without client-side validation, the service gets to decide how to enforce the rules, which means many more users benefit from the relaxed constraint, even those that haven't yet upgraded to the latest client.

bitsforthought avatar Nov 12 '16 00:11 bitsforthought

@brjohnstmsft - I am saying the same thing. If a service developer or spec author feels that the constraint is not contractual (i.e. it is not associated with the api-version) then he should not specify that constraint in the spec. Simply documenting them with a caveat is fine.

If he wants to be in the business of updating the description every time the service changes the constraint then that is his call. This makes everyone's life simple.

Having a switch to flip the bit on honoring client side validation is not going to help you much. Whenever the switch is flipped it is a breaking change any ways.

amarzavery avatar Nov 12 '16 11:11 amarzavery

@amarzavery

If a service developer or spec author feels that the constraint is not contractual (i.e. it is not associated with the api-version) then he should not specify that constraint in the spec. Simply documenting them with a caveat is fine.

This is a reasonable short-term workaround, but I'm not sure it's the right approach. Also, it seems to be a departure from the current policy being applied during code review. I initially started with just documenting the limits, but was told explicitly to use Swagger constraints instead. I don't necessarily disagree with this; Swagger specs are now used for REST API reference documentation too (speaking generally; the Azure Search data plane specs are currently exempt from this), and maybe having the constraints represented as first-class properties in the spec will prove useful in the future in some context besides code gen that we may not foresee.

What I'm arguing for here is specifically about the behavior of the code generator, which is a separate issue from the composition of the Swagger specs. We need to think more broadly about how we write the specs now that AutoRest is not the only consumer of them.

bitsforthought avatar Nov 12 '16 19:11 bitsforthought

@amarzavery

Having a switch to flip the bit on honoring client side validation is not going to help you much. Whenever the switch is flipped it is a breaking change any ways.

If I could go back in time and change history, I would pre-empt implementing client-side validation altogether. A switch is just a concession to backwards compatibility; I firmly believe that if you added such a switch, it would be in every service team's best interest to immediately flip it and leave it off forever.

bitsforthought avatar Nov 12 '16 19:11 bitsforthought

This is another one of the "AutoRest 1.0 assumptions" that aren't overridable.

Overriding AutoRest assumptions is on the list for 2.0 .

fearthecowboy avatar Jan 31 '17 16:01 fearthecowboy

Whoops, we added this some time back - at least to C#. One can disable client side validation using client-side-validation: false in the config file or --client-side-validation=false on the CLI. Example usage: https://github.com/Azure/autorest/tree/master/Samples/1d-common-settings

Will tag the remaining generators to track their compliance.

olydis avatar Jun 26 '17 18:06 olydis

moved to https://github.com/Azure/autorest/issues/2100

olydis avatar Sep 28 '17 18:09 olydis