autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Support extension for discriminator definition property to allow making base class abstract.

Open tudordid opened this issue 10 years ago • 10 comments

Today, if using the "discriminator" class on a contract definition to implement class polymorphism, the generated base class is still instantiable.

This can break server code assumptions because it allows for creation of incomplete contract objects (if instantiating to the base class type rather than the expected derived classes).

Since "discriminator" is a base Swagger property, can AutoRest support an extension to allow making the base class abstract for languages that support that during code generation ?

tudordid avatar May 02 '16 20:05 tudordid

We have this problem in the MachineLearning web services RP swagger spec, https://github.com/tudordid/azure-rest-api-specs/blob/master/arm-machinelearning/2016-05-01-preview/swagger/webservices.json

Where we define a base _WebServiceProperties _ class and then a derived WebServicePropertiesForGraph class based on whether the "packageType" discriminator is set to "Graph"

"WebServiceProperties": {
  "type": "object",
  "description": "The set of properties specific to the Azure ML web service resource.",
  "required": [
    "packageType"
  ],
  "**discriminator**": "packageType",
  "properties": {
   ......
    "**packageType**": {
      "type": "string",
      "description": "The type of package deployed with this web service. Only Graph based services are supported at this time.",
      "enum": [
        "Graph"
      ] 
    }

}

We will shortly support another value of "Code" for this discriminator, which will in turn generate a _WebServicePropertiesForCode _class.

We only consider instantiations of the derived classes to be a fully specified wire contract, so we should be able to prevent users from instantiating _WebServiceProperties _ only by making the class abstract.

tudordid avatar May 02 '16 20:05 tudordid

We also have this problem in Data Lake analytics, where JobProperties is the base class for two different, complete, properties objects that must be chosen instead. Adding a a plus 1 here :+1:

begoldsm avatar May 03 '16 01:05 begoldsm

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

Just adding a +1 here. We've had customers get confused when they try to instantiate a base class that should be abstract, but isn't.

bitsforthought avatar Feb 23 '17 00:02 bitsforthought

Adding + 1

raveeshupahuja avatar Jun 28 '19 01:06 raveeshupahuja

Another +1 !

This would be great

venkat24 avatar Mar 03 '20 00:03 venkat24

This is interesting to Cognitive Search, as without it we get bad codegen in JS where the abstract base class appears to be a type the user can use/provide and removing it in the convenience layer is painful

xirzec avatar Mar 16 '20 23:03 xirzec

Re-opening to consider for AutoRest v3

daviwil avatar Mar 16 '20 23:03 daviwil

This is an interesting feature for Azure Synapse Resource Provider as well. Having abstract classes will be really useful in conveying the intent of Swagger request/response objects

piyushdubey avatar Mar 14 '22 15:03 piyushdubey

We decided to not add this extension. The reason is when using a discriminator, the extensibility of additional subtypes is typically a very likely scenario. Looking at a single version of SDK, it does not present a problem. However looking across versions, older versions of SDK would be impacted.

Consider LIST or GET operations, if the base class is marked abstract, any time new sub-types are introduced, the older version SDK would NOT have the models to recognize these new objects. With base class marked as 'abstract', those data could NOT be instantiated as instances of the base class and fail.

Having said that, if you KNOW FOR SURE that the discriminator values are CLOSED (No possibility of adding additional types) and you absolutely want the base class as abstract, you could leverage the customization capabilities of each language CodeGen to override it. For example for C#, you can copy baseclass.cs into customization folder and add abstract keyword. The autorest.csharp codegen will respect that. BUT please be VERY clear on the potential implication. If a new value shows up, the older SDK would stop working and you may have on-call incidents.

From the server code perspective, I think data validation must always be done anyway. You cannot rely on SDK to enforce data correctness and completeness.

allenjzhang avatar Mar 14 '22 21:03 allenjzhang