question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integer enum with named items

See original GitHub issue

I would like to propose a new field called enumNames.

Example:

definitions:
  weather:
    type: object
    required:
      - type
    properties:
      type:
        type: integer
        format: int32
        enum:
          - 1
          - 2
          - 3
        enumNames:
          - Sunny
          - Cloudy
          - Rainy

This information is necessary for generating the following Java enum.

public enum Weather {
    Sunny(1),
    Cloudy(2),
    Rainy(3);

    private final int type;

    private Weather(final int type) {
        this.type = type;
    }

    public type getType() {
        return type;
    }
}

Some web APIs require clients to send/retrieve integer enum items while knowing what they mean. So it is useful to be able to generate this kind of enum.

I had been seeking advice in the following swagger-codegen issue, and got a conclusion that we need a vendor extension (which corresponds to this proposal) using Swagger 2.0 spec. https://github.com/swagger-api/swagger-codegen/issues/2690

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:92
  • Comments:45 (21 by maintainers)

github_iconTop GitHub Comments

114reactions
ralfhandlcommented, Sep 19, 2022

I like this, combined with json-schema-org/json-schema-spec#348:

type: integer
format: int32
enum:
 - value: 1
   name: Sunny
   description: Blue sky
 - value: 2
   name: Cloudy
   description: Slightly overcast
 - value: 3
   name: Rainy
   description: Take an umbrella with you

If the names should be unique (probably a prerequisite for code generation) a hashmap would be more appropriate:

type: integer
format: int32
enum:
  Sunny:
    value: 1
    description: Blue sky
  Cloudy:
    value: 2
    description: Slightly overcast
  Rainy:
    value: 3
    description: Take an umbrella with you

So enum could either have an array value with just the list of values, or an object value with named and potentially further annotated values.

21reactions
jminicommented, Dec 17, 2018

With OpenAPI v3, some tools supports a x-enum-varnames extension to do so:

  weather:
    type: object
    properties:
      type:
        type: integer
        format: int32
        enum:
          - 1
          - 2
          - 3
        x-enum-varnames:
          - Sunny
          - Cloudy
          - Rainy

Other has started to use x-enum-values (see Extension Properties on OpenAPI Specs)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define enum mapping in OpenAPI? - swagger
This way you can specify both custom names ( title ) and descriptions for enum values. Severity: type: integer oneOf: - title: HIGH...
Read more >
Enums
You can use the enum keyword to specify possible values of a request parameter or a model property. For example, the sort parameter...
Read more >
C# Enumerations Type - Enum
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant...
Read more >
Enumeration types - C# reference
An enumeration type (or enum type) is a value type defined by a set of ... the associated constant values of enum members...
Read more >
How to Use Enum in C# | LoginRadius Blog
An effective way to describe a set of named integral constants assigned to a variable is to include a ... By default, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found