swagger-api / swagger-api/swagger-codegen

[JAVA] Enum field with default generated incorrectly

Open
#12,469 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have a fastapi defined interface with a enum (TaskStatus) and a structure (Task) containing an instance of that enum with a default.

I extract the openapi.json from the running fastapi using wget.

Then I generate the Java client code using swagger codegen.

The enum is generated just fine, but the default value in the object is missing the Enum type. I'm wondering if I'm missing something I need to add to the fastapi main.py or the generated openapi.json.

main.py:

from typing import Union
from enum import Enum
from pydantic import BaseModel

from fastapi import FastAPI

app = FastAPI()

class TaskStatus(str,Enum):
    NIL = "NIL"
    NOT_APPROVED = "NOT_APPROVED"    
    NOT_READY = "NOT_READY"                  
    NOT_ASSIGNED = "NOT_ASSIGNED"      
    PENDING = "PENDING"               
    EXECUTING = "EXECUTING"          
    DONE = "DONE"                 

class Task(BaseModel):
    '''Base class for tasks'''

    task_id: int = -1
    '''ID of the task'''
    
    status: TaskStatus = TaskStatus.NOT_ASSIGNED
    '''Status of task to be tracked throughout runtime'''

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/task/{task_id}")
def read_task(task_id: int ) -> Task:
    task = Task( id = task_id )
    return task

generated java enum:

public enum TaskStatus {
  NIL("NIL"),
  NOT_APPROVED("NOT_APPROVED"),
  NOT_READY("NOT_READY"),
  NOT_ASSIGNED("NOT_ASSIGNED"),
  PENDING("PENDING"),
  EXECUTING("EXECUTING"),
  DONE("DONE");

INVALID generated java Task class - enum default value should be TaskStatus.NOT_ASSIGNED:

public class Task {
  @JsonProperty("task_id")
  private Object taskId = -1;

  @JsonProperty("status")
  private TaskStatus status = NOT_ASSIGNED;
Swagger-codegen version

swagger-codegen-cli-3.0.61.jar

Swagger declaration file content or url

Full openapi.json

snip:

{
....
        "/task/{task_id}": {
            "get": {
                "summary": "Read Task",
                "operationId": "read_task_task__task_id__get",
                "parameters": [{
                        "name": "task_id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "title": "Task Id"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Task"
                                }
                ...
    },
    "components": {
        "schemas": {
         ...
            "Task": {
                "properties": {
                    "task_id": {
                        "type": "integer",
                        "title": "Task Id",
                        "default": -1
                    },
                    "status": {
                        "$ref": "#/components/schemas/TaskStatus",
                        "default": "NOT_ASSIGNED"
                    }
                },
                "type": "object",
                "title": "Task",
                "description": "Base class for tasks"
            },
            "TaskStatus": {
                "type": "string",
                "enum": ["NIL", "NOT_APPROVED", "NOT_READY", "NOT_ASSIGNED", "PENDING", "EXECUTING", "DONE"],
                "title": "TaskStatus"
            },

Command line used for generation

wget http://127.0.0.1:8000/openapi.json

java -jar swagger-codegen-cli-3.0.61.jar generate -l java -i openapi.json -Dlibrary=jersey1,hideGenerationTimestamp=true -o java-jersey1

Steps to reproduce

regenerate the code with the linked openapi.json

Maybe I'm missing something somewhere?

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

Start with the linked openapi.json and reproduce the issue using the shown swagger-codegen-cli Java generation command. Trace how the Task model's status default is rendered in the generated Java class, then verify that the generated code uses the TaskStatus enum constant and compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, java, openapi, python
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.