microsoft / typespec

https://typespec.io/
MIT License
4.1k stars 194 forks source link

How to create an integer enum #1724

Open mahomedalid opened 1 year ago

mahomedalid commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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? https://github.com/microsoft/typespec/blob/c2e12473283cdb6e409caf69d38fad70e3beed27/packages/compiler/core/types.ts#L286

mahomedalid commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

OpenAPI v2 does define the int32 format explicitly.

image

mahomedalid commented 1 year ago

3.0.3 also https://spec.openapis.org/oas/v3.0.3#dataTypeFormat

image

markcowl commented 1 year ago

@markcowl correlate with bitfield support

markcowl commented 1 year ago

`Related to: https://github.com/microsoft/typespec/issues/1237