autorest icon indicating copy to clipboard operation
autorest copied to clipboard

[Feature request]x-ms-enum doesn't support combination of enum values.

Open JasonYang-MSFT opened this issue 9 years ago • 8 comments

Here is the situation, we want set a parameter with None or any combination of Logging | Metrics | Other. But following extension only support to set single enum value. I think it is a common case trying to set a combination of enum types.

Are we supporting this feature now? If not, are we planning to support this?

Sample

      "type": "string",
      "enum": [
        "None",
        "Logging",
        "Metrics",
        "Other"
      ],
      "x-ms-enum": {
        "name": "Bypass",
        "modelAsString": false
      },
      "default": "Other",

JasonYang-MSFT avatar Feb 22 '17 03:02 JasonYang-MSFT

Agreed; This would be a useful feature. In another issue we've discussed adding the concept of an ExtensibleEnum to AutoRest: https://github.com/Azure/autorest/issues/1591

Here's an example of how we've used ExtensibleEnum in the Azure Search .NET client library to implement enum "flags": https://github.com/Azure/azure-sdk-for-net/blob/AutoRest/src/Search/Microsoft.Azure.Search/Customizations/Indexes/Models/RegexFlags.cs

This approach should also be generalizable.

bitsforthought avatar Feb 22 '17 16:02 bitsforthought

You can just model this as a collection of enums if you want (rendered into C# as List<Bypass> in this case).

JSON doesn't have a built in way to render a Logging | Metrics without putting them into a collection anyway.

A flag to allow AutoRest to render a collection of enums as a Flags enum for langauges that support it probably makes sense, but as far as the REST format is concerned I would think that what you're asking for is exactly a collection of enums.

matthchr avatar Mar 31 '17 18:03 matthchr

@matthchr In our case, we literally wanted a string like "OPTION_A | OPTION_B" in the JSON. I'm guessing that's what @JasonYang-MSFT wanted too.

bitsforthought avatar Mar 31 '17 23:03 bitsforthought

I'm hesitant to do something like that as it opposes (my understanding of) the Swagger spec. enum is supposed to literally enumerate all possible values that could go over the wire, so sending "... | ..." would sort of actively violate the contract.

olydis avatar Jun 01 '17 23:06 olydis

enum is supposed to literally enumerate all possible values that could go over the wire

That interpretation is already violated by modelAsString: true.

bitsforthought avatar Jun 02 '17 02:06 bitsforthought

fair point! Although in practice I've mainly seen it as breaking change prevention, so the contract will be violated merely when it becomes deprecated. We'll definitely think about this when building code generators 2.0 (OpenAPI 3.0 sort of requires some significant rethinking), at that point I'd also be more than happy to make your approach in #1591 one of the enum-implementations!

Needless to say, if this feature request gets significant traction, we'll consider adding [Flags] to the current generator.

olydis avatar Jun 02 '17 13:06 olydis

@olydis That would be great, thanks!

bitsforthought avatar Jun 02 '17 17:06 bitsforthought

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

olydis avatar Sep 19 '17 16:09 olydis