Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# code gen enums: descriptions + open-endedness #1159

Open
adamjones1 opened this issue Apr 2, 2020 · 5 comments
Open

C# code gen enums: descriptions + open-endedness #1159

adamjones1 opened this issue Apr 2, 2020 · 5 comments

Comments

@adamjones1
Copy link
Contributor

There are two scenarios with enums in specs which are not supported (both elaborated on in this thread):

Enums with item descriptions

According to one of the JSON Schema maintainers, the standard pattern for this is by wrapping each case in a singleton type with a description, ie like:

myEnum:
  type: int
  oneOf:
    - enum: [1]
      title: Foo
    - enum: [2]
      title: Bar
      description: This is a bar

to translate to

enum MyEnum
{
    Foo = 1,
    /// <summary>This is a bar</summary>
    Bar = 2
}

Open-ended/extensible enums

The pattern for this as described on the thread is allegedly:

myEnum:
  type: string
  - anyOf:
    - enum:
      - foo
      - bar
    - {}

to translate, I guess, to

enum MyEnum
{
    Foo,
    Bar,
    _Other
}

or perhaps just as a vanilla string together with the generation of a class like

static class MyEnum
{
    public const string Foo = "foo";
    public const string Bar = "bar";
}

I propose that both of these patterns, and their combination (as I described on the referenced thread), are detected and supported.

@kassemsandarusi
Copy link
Contributor

+1 for second open-ended enum support.

@RicoSuter
Copy link
Owner

Yes, id also like to have this. What if we generate an additional enum value Unknown and serialize to that if its not known in the enum?

@RicoSuter RicoSuter added this to the vNext milestone May 5, 2020
@adamjones1
Copy link
Contributor Author

I don't have a strong view on the choice of C# representation but note that the string + static class route has more flexibility to handle non-string enums too, if that's of interest. (Personally, I currently require only strings.)

@adamjones1
Copy link
Contributor Author

@RicoSuter has any work begun on this? If not I think I can lend some time to looking at the open-ended enum support.

@RicoSuter
Copy link
Owner

Not yet... we would probably need an EnumType option (Enum, EnumWithFallback, StringEnum).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants