autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Allow marking some model definitions as non-generating

Open pareshverma91 opened this issue 10 years ago • 3 comments

Consider some model definitions:

"definitions": {
        "bModel": {
            "properties": {
                "someProperty": {
                     "type": "string"
        },
        "cModel": {
            "allOf": [
                {
                    "$ref": "#/definitions/bModel"
                }
            ]
        },
        "dModel": {
            "allOf": [
                {
                    "$ref": "#/definitions/bModel"
                }
            ]
        }
    }

cModel and dModel have some similarities in their schema, which have been expressed by putting those similarities in a base class bModel. However, this would also lead to generation of class bModel, which might not be needed by user. So, while the user needs bModel to avoid duplication, the generated code of bModel will just pollute their SDK. Is there a way to suggest autorest to not generate code for bModel?

pareshverma91 avatar Dec 02 '16 00:12 pareshverma91

@pareshverma91 To clarify, do you mean that AutoRest would:

  1. Duplicate the properties of bModel in both cModel and dModel, or
  2. Generate derived classes cModel and dModel that both inherit from a base class bModel that is not generated, but rather assumed to be provided via custom code.

I ask because the latter scenario is already possible today with x-ms-external.

bitsforthought avatar Dec 02 '16 01:12 bitsforthought

@brjohnstmsft I meant 1. Let me provide a better example.

      "Object": {
            "type": "object"
        },
       "bModel": {
            "properties": {
                "someProperty": {
                     "type": "string"
        },
        "cModel": {
            "allOf": [
                {
                    "$ref": "#/definitions/bModel"
                },
                {
                    "$ref": "#/definitions/Object"
                }
            ]
        },
        "dModel": {
            "allOf": [
                {
                    "$ref": "#/definitions/bModel"
                },
                {
                    "$ref": "#/definitions/Object"
                }
            ]
        }

For this swagger specification autorest will generate classes Object, BModel, CModel, DModel, with no hierarchies at all (CModel, DModel will have all the properties of BModel expanded in place). I would like to leverage this capability of autorest to avoid duplication of specs in my swagger doc, by defining common properties in BModel, and then basing CModel and DModel off of BModel. The issue with this approach is that it will create an additional noun BModel in the SDK, which is meaningless from business logic perspective. So I need to avoid generating it.

pareshverma91 avatar Dec 02 '16 22:12 pareshverma91

"So I need to avoid generating it." - and you can do just that by marking it as x-ms-external. Since #1694 you can use this extension when generating using the CSharp generator (while it was restricted to Azure generators before).

I'll close this since as far as I understand AutoRest can do precisely what you want (now). Feel free to reopen in case we can further assist you or got your use case wrong! :-)

olydis avatar Dec 29 '16 18:12 olydis