[JAVA] Enum field with default generated incorrectly
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
调研方向
从链接的 openapi.json 开始,使用所示的 swagger-codegen-cli Java 生成命令重现该问题。跟踪 Task 模型的 status 默认值如何呈现在生成的 Java 类中,然后验证生成的代码使用了 TaskStatus 枚举常量并且能够成功编译。
由索引模型根据 Issue 内容生成。
描述
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
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?
- 主要语言
- Mustache
- 星标
- 17.8k
- 派生
- 6k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
swagger-api/swagger-codegen 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
swagger-api/swagger-codegen#12755 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
swagger-api/swagger-codegen#12445 ·
-
security vulnerability
难度 1/5 1 小时以内 新手友好度 62/100
swagger-api/swagger-codegen#11942 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
swagger-api/swagger-codegen#11812 · 1 个 reaction ·
-
security vulnerability
难度 2/5 1-3 小时 新手友好度 65/100
swagger-api/swagger-codegen#11594 ·
查看 swagger-api/swagger-codegen 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
SciML/Evolutionary.jl#182 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
simonw/llm-mistral#43 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
rossoctl/context-guru#346 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 90/100
prime-radiant-inc/evener#2883 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
GoogleChromeLabs/project-sesame#216 ·
维护者通常 12 天内回复