Skip to content

Support generating enum varnames/descriptions#1540

Open
zackyancey wants to merge 2 commits intojuhaku:masterfrom
serialtek:enum-varnames
Open

Support generating enum varnames/descriptions#1540
zackyancey wants to merge 2 commits intojuhaku:masterfrom
serialtek:enum-varnames

Conversation

@zackyancey
Copy link
Copy Markdown
Contributor

This addresses #1317.

x-enum-varnames and x-enum-descriptions are commonly used extensions,
especially for enums with a numeric representation. This adds options to generate
both of those values, so an enum like this:

    #[schema(repr, enum_varnames, enum_descriptions)]
    #[repr(u8)]
    pub enum MyEnum {
        /// Value A
        A = 0,
        /// Value B
        B = 1,
        /// Value C
        C = 2,
    }

Will have a schema like this:

{
  "type": "integer",
  "enum": [
    0,
    1,
    2
  ],
  "x-enum-varnames": [
    "A",
    "B",
    "C"
  ],
  "x-enum-descriptions": [
    "Value A",
    "Value B",
    "Value C"
  ]
}

(Note that this example uses the #[schema(repr)] syntax from this PR, just because the enum_varnames feature makes most sense with a numerical enum. The two branches aren't dependent on each other though.)

This lets a builder build up extensions incrementally instead of with just one
call to extensions, which is helpful for some automatically-added extensions.
This addresses juhaku#1317.

`x-enum-varnames` and `x-enum-descriptions` are commonly used extensions,
especially for enums with a numeric representation. This adds options to generate
both of those values, so an enum like this:

```rust
    #[schema(repr, enum_varnames, enum_descriptions)]
    #[repr(u8)]
    pub enum MyEnum {
        /// Value A
        A = 0,
        /// Value B
        B = 1,
        /// Value C
        C = 2,
    }
```

Will have a schema like this:

```json
{
  "type": "integer",
  "enum": [
    0,
    1,
    2
  ],
  "x-enum-varnames": [
    "A",
    "B",
    "C"
  ],
  "x-enum-descriptions": [
    "Value A",
    "Value B",
    "Value C"
  ]
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant