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

How to create an integer enum #1724

Open
mahomedalid opened this issue Mar 20, 2023 · 10 comments
Open

How to create an integer enum #1724

mahomedalid opened this issue Mar 20, 2023 · 10 comments
Labels
design:needed A design request has been raised that needs a proposal
Milestone

Comments

@mahomedalid
Copy link

The following enum:

import "@typespec/rest";
import "@typespec/openapi3";

...

enum DayOfWeek
{
    Monday: 0,
    Tuesday: 1,
    Wednesday: 2,
    Thrusday: 3,
    Friday: 4,
    Saturday: 5,
    Sunday: 6
}

Generates a number enum. Is there any decorator (or way) to force it to an int32 enum? I did try to find something in the documentation without luck.

@bterlson
Copy link
Member

What is an int32 enum? OpenAPI doesn't have that concept I believe. Are you wanting to see a particular output in some other emitter?

@mahomedalid mahomedalid changed the title How to create an int32 enum How to create an integer enum Mar 21, 2023
@mahomedalid
Copy link
Author

@bterlson sorry, it was integer.

When generating the spec from the base asp.net sample todo app it generates this:

"DayOfWeek": {
        "enum": [
          0,
          1,
          2,
          3,
          4,
          5,
          6
        ],
        "type": "integer",
        "format": "int32"
      },

Looking into the spec (https://spec.openapis.org/oas/v3.0.3#schema-object) seems type and format are taken directly from JSONSchema. Looking into the code, seems member can either be a string or a number correct?

export interface EnumMember extends BaseType, DecoratedType {

@mahomedalid
Copy link
Author

I think at the end it does not matter, I suppose, since there is a set of predefined values, it cannot be outside any of those values independently if are integer or number.

@bterlson
Copy link
Member

JSON Schema doesn't define that format afaict, so I'm actually a little confused why we generate it in the first place. @mikekistler any thoughts? Is this something we should even be including in the output?

@markcowl
Copy link
Contributor

markcowl commented Mar 21, 2023

The format is from here: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-7.3

Use of format in OpenAPI2 predates any mention in Json Schema, though.

@bterlson
Copy link
Member

It doesn't define the int32 format specifically though. While the spec allows for arbitrary values, it seems weird that we would include a format that most (all?) implementations wouldn't support. If the range of values is important, we could emit minimum and maximum which would be broadly supported...

@mikekistler
Copy link
Member

OpenAPI v2 does define the int32 format explicitly.

image

@mahomedalid
Copy link
Author

@markcowl markcowl self-assigned this Mar 21, 2023
@markcowl
Copy link
Contributor

@markcowl correlate with bitfield support

@markcowl
Copy link
Contributor

`Related to: #1237

@markcowl markcowl removed their assignment Mar 21, 2023
@markcowl markcowl added this to the Backlog milestone Mar 21, 2023
@markcowl markcowl added the design:needed A design request has been raised that needs a proposal label Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design:needed A design request has been raised that needs a proposal
Projects
None yet
Development

No branches or pull requests

4 participants