autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Enum with name value pair

Open tapan-p opened this issue 10 years ago • 8 comments

I was trying to generate API documents using swagger and autorest, but I have a situation where, I need enums with string and int value. Is there a way to do so using autorest? As of now, it only generates either string or int values. what I need is : Enum{ string value = int value, string value = int value }

tapan-p avatar Jun 09 '16 12:06 tapan-p

Is there anyone who can help me on this topic?

tapan-p avatar Jun 14 '16 20:06 tapan-p

@tpatel11 It might help if you explain what target language you're discussing. I don't recognize this construct:

Enum{
 string value = int value,
 string value = int value
 }

bitsforthought avatar Jun 14 '16 20:06 bitsforthought

apologies for missing the information. I am using c# and I have string enum with values as :

Enum colors
{
red=1,
blue=-5
green=7
}

But when it generates the document, it either ends up in string representation or in Nullable int representation based on the x-ms option provided. I am trying to achieve both.

tapan-p avatar Jun 14 '16 20:06 tapan-p

@tpatel11 Why do you need both? How would you achieve both if you were hand-writing the C# code? Would you use a class instead of an enum? What would it look like?

bitsforthought avatar Jun 14 '16 21:06 bitsforthought

The main reason behind is, I have a complex business logic classes/Models where I am using this enum frequently, and doing some calculation based on the enum values. I am trying to generate the API for multiple clients, so that while making the call to APIs, I need not to rely on the specific business model (where these enums are defined - means, I don't want to include those classes as dependencies). Instead, I am trying to achieve it using the code generator so that, I can have those enums locally while making the calls.

tapan-p avatar Jun 14 '16 21:06 tapan-p

@tpatel11 That doesn't really explain what you mean by needing both string and int representations of the same enum.

I suspect that you'll have to use x-ms-external in your spec and write the enums yourself.

bitsforthought avatar Jun 14 '16 21:06 bitsforthought

This issue is getting old. It doesn't seem part of Swagger 3.0 but I would like to have this feature too as it is an important feature of c# enums.

Any plans on this ? What about adding this information to ms-x-enum extension ?

"enum": [
 "ValueA",
 "ValueB" 
],
"x-ms-enum": {
 "name": "TestEnum",
 "modelAsString": false,
 "values": [
   1,
   2
 ]
}  

fhoering avatar Mar 09 '17 10:03 fhoering

detaching name from value works now (example: https://github.com/Azure/autorest/pull/2308/files#diff-5ff92b971534063d584387de2c15ae37R37), however in your specific example you also want to use numbers for the values which is an orthogonal issue. This issue is tracked by https://github.com/Azure/autorest/issues/833 so I will close this issue.

Please reopen if there are further issues regarding the feature.

olydis avatar Jun 26 '17 19:06 olydis