swagger-api / swagger-api/swagger-codegen
[JAVA] Enum with Integer types formatted int64 does not generate the right
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Enum with Integer types formatted int64 value is mapped long type and TypeAdapter raise below Exception
Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Long (java.lang.String and java.lang.Long are in module java.base of loader 'bootstrap')
generated code
/*
* API
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 0.0.1
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
package io.swagger.client.model;
import java.util.Objects;
import java.util.Arrays;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.IOException;
/**
* Model
*/
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2022-02-24T19:18:55.072810+09:00[Asia/Tokyo]")
public class Model {
/**
* Gets or Sets status
*/
@JsonAdapter(StatusEnum.Adapter.class)
public enum StatusEnum {
NUMBER_0(0l),
NUMBER_1(1l);
private Long value;
StatusEnum(Long value) {
this.value = value;
}
public Long getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
public static StatusEnum fromValue(Long input) {
for (StatusEnum b : StatusEnum.values()) {
if (b.value.equals(input)) {
return b;
}
}
return null;
}
public static class Adapter extends TypeAdapter<StatusEnum> {
@Override
public void write(final JsonWriter jsonWriter, final StatusEnum enumeration) throws IOException {
jsonWriter.value(String.valueOf(enumeration.getValue()));
}
@Override
public StatusEnum read(final JsonReader jsonReader) throws IOException {
Object value = jsonReader.nextString();
return StatusEnum.fromValue((Long)(value));
}
}
} @SerializedName("status")
private StatusEnum status = null;
public Model status(StatusEnum status) {
this.status = status;
return this;
}
/**
* Get status
* @return status
**/
@Schema(description = "")
public StatusEnum getStatus() {
return status;
}
public void setStatus(StatusEnum status) {
this.status = status;
}
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Model model = (Model) o;
return Objects.equals(this.status, model.status);
}
@Override
public int hashCode() {
return Objects.hash(status);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class Model {\n");
sb.append(" status: ").append(toIndentedString(status)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Swagger-codegen version
3.0.31
Swagger declaration file content or url
openapi: 3.0.2
info:
version: "0.0.1"
title: API
servers:
- url: /
paths:
/api:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Model"
components:
schemas:
Model:
properties:
status:
type: integer
enum:
- 0
- 1
format: int64
Command line used for generation
java -jar swagger-codegen-cli-3.0.31.jar generate -i ${yml} -l java -o ${outputDir}
Steps to reproduce
cli command
Related issues/PRs
https://github.com/swagger-api/swagger-codegen/issues/11166
https://github.com/swagger-api/swagger-codegen-generators/pull/978/files
Suggest a fix/enhancement
Add a tag for long type , maybe fix this problem.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the OpenAPI YAML and the swagger-codegen CLI command shown, then inspect the Java client generator/template responsible for the generated Model.StatusEnum.Adapter. Compare the related issue and generators PR 978; done means an int64 enum can be deserialized without the reported ClassCastException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100