microsoft/kiota

Add support for numerical enum values

Open

#5,165 opened on Aug 14, 2024

View on GitHub
 (11 comments) (14 reactions) (0 assignees)C# (326 forks)auto 404
help wantedtype:feature

Repository metrics

Stars
 (3,783 stars)
PR merge metrics
 (PR metrics pending)

Description

Is your feature request related to a problem? Please describe the problem.

The assertion made at the bottom of this article is false: https://learn.microsoft.com/en-us/openapi/kiota/models

Supporting proper enums does add lots of value. And x-enumNames like nswag does and the oneof syntax for enums that is fully supported in OpenApi 3.0 absolutely is of value.

Client library/SDK language

None

Describe the solution you'd like

i.e. in typescript, this should define an enum with a name that is defined in the openapi document AND the number value and when the client sets the enum, it's setting the number in the payload, while allowing the use of the fully defined enum

That means that it should support both x-enuNames and the properly defined syntax from OpenAPI 3.0 and generate, for all clients, full enums that map through if that client supports them.

Additional context

nswag client generator and countless other tools support this without issue out of the box.

Here's a verbose version that should support every permutation for all client generation tools that kiota should be able to pick from and generate enums. (and it should support all 3, especially the openapi 3.0 spec version using oneof)

      "RuleTypes": {
        "type": "integer",
        "description": "Rule Types\n            ",
        "x-enumNames": [
          "Validation",
          "Sum",
          "Average",
          "Max",
          "Min"
        ],
        "enum": [
          1,
          2,
          3,
          4,
          5
        ],
        "oneOf": [
          {
            "type": "integer",
            "enum": [
              1
            ],
            "title": "Validation"
          },
          {
            "type": "integer",
            "enum": [
              2
            ],
            "title": "Sum"
          },
          {
            "type": "integer",
            "enum": [
              3
            ],
            "title": "Average"
          },
          {
            "type": "integer",
            "enum": [
              4
            ],
            "title": "Max"
          },
          {
            "type": "integer",
            "enum": [
              5
            ],
            "title": "Min"
          }
        ],
        "x-ms-enum": {
          "name": "RuleTypes",
          "modelAsString": false,
          "values": [
            {
              "name": "Validation",
              "value": 1,
              "description": ""
            },
            {
              "name": "Sum",
              "value": 2,
              "description": ""
            },
            {
              "name": "Average",
              "value": 3,
              "description": ""
            },
            {
              "name": "Max",
              "value": 4,
              "description": ""
            },
            {
              "name": "Min",
              "value": 5,
              "description": ""
            }
          ]
        }
      },

This should generate in typescript as an example something like this:

/**
 * Rule Types
 */
enum RuleTypes {
    /**
     * Validation
     */
    Validation = 1,

    /**
     * Sum
     */
    Sum = 2,

    /**
     * Average
     */
    Average = 3,

    /**
     * Max
     */
    Max = 4,

    /**
     * Min
     */
    Min = 5
}

And C# this:

public enum RuleTypes
{
    Validation = 1,

    Sum = 2,

    Average = 3,

    Max = 4,

    Min = 5
}

And Java this:

/**
 * Rule Types
 */
public enum RuleTypes {
    /**
     * Validation
     */
    VALIDATION(1),

    /**
     * Sum
     */
    SUM(2),

    /**
     * Average
     */
    AVERAGE(3),

    /**
     * Max
     */
    MAX(4),

    /**
     * Min
     */
    MIN(5);

    private final int value;

    RuleTypes(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

And Go:

package main

// RuleTypes represents the different types of rules
type RuleTypes int

const (
    // ValidationRule represents a validation rule
    ValidationRule RuleTypes = iota + 1
    // SumRule represents a sum rule
    SumRule
    // AverageRule represents an average rule
    AverageRule
    // MaxRule represents a max rule
    MaxRule
    // MinRule represents a min rule
    MinRule
)

// String returns the string representation of RuleTypes
func (r RuleTypes) String() string {
    switch r {
    case ValidationRule:
        return "Validation"
    case SumRule:
        return "Sum"
    case AverageRule:
        return "Average"
    case MaxRule:
        return "Max"
    case MinRule:
        return "Min"
    default:
        return "Unknown"
    }
}

// Value returns the integer value of RuleTypes
func (r RuleTypes) Value() int {
    return int(r)
}

Etc.

And any language that doesn't support numeric assignment of enums, would fall back to the current behavior.

Contributor guide