swagger-api / swagger-api/swagger-codegen

Generate enums with integer values assigned

Open
#2,690 28 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Client: Swift Enhancement: General Feature: Enum
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I want to generate enum with integer values assigned.

Swift:

enum Weather: Int {
    case Sunny = 1
    case Cloudy = 2
    case Rainy = 3
}

Java:

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

    private final int type;

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

    public type getType() {
        return type;
    }
}

But I couldn't figure out how to do this.

In the Swagger 2.0 specs, it references JSON Schema specs for enum.
It says any type can be used, but it seems like type needs to be string for generating enums. Like this:

definitions:
  weather:
    type: object
    required:
      - type
    properties:
      type:
        type: string
        enum:
          - Sunny
          - Cloudy
          - Rainy

If I change type to integer, no enum is generated, and it will be a plain integer property.

Firstly, I couldn't find how to assign integers to enum items using Swagger specs.

Are there any plans or workarounds to support this kind of enum?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file, test, or generator entry point. Start by reading the cited Swagger 2.0 and JSON Schema enum/type references, then trace how the generator treats integer properties versus string enums. Done means the project has a clear supported approach for preserving integer enum values in generated output.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi
Domain
api, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.