Open stephen-mojo opened 3 years ago
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
I added this VERY quick and dirty Moshi adapter for the time being and added an instance of it for each of my enums. It is probably missing a lot of error handling and I am not really sure if I am doing things correctly as I have never worked with reflecting annotations before, but it seems to work:
class IntegerEnumAdapter<T : Enum<T>>(enumType: Class<T>): JsonAdapter<T>() {
private val intToEnumMap = mutableMapOf<Int, T>()
private val enumToIntMap = mutableMapOf<T, Int>()
init {
// Loop through all of the possible enum values.
enumType.enumConstants.forEach { enum ->
// For each possible enum, get the associated name specified in its annotation.
val constantName: String = enum.name
val annotation = enumType.getField(constantName).getAnnotation(Json::class.java)
// The name is always an integer in string form. It is safe to convert it to an integer.
val integerEnumNameAnnotation = annotation.name.toInt()
intToEnumMap[integerEnumNameAnnotation] = enum
enumToIntMap[enum] = integerEnumNameAnnotation
}
}
override fun fromJson(reader: JsonReader): T? {
val integer = reader.nextInt()
val enum = intToEnumMap[integer]
return enum
}
override fun toJson(writer: JsonWriter, enum: T?) {
val integer = enumToIntMap[enum]
writer.value(integer)
}
}
sounds very similar to #8442
The Bug in #8442 seems fixed now :)
Still a bug
Description
I am using the OpenApiGenerator via the Gradle plugin in my Kotlin Android project. I am running into an issue where generated enum classes are expecting strings even though the specification specifies integers. The means Moshi is unable to parse enum values in my sever responses.
Enums in my OpenAPI specification file are of this form:
The resulting generated enums are of this form:
This leads to unsuccessful parsing of enums by Moshi. The resulting exception is of this form:
openapi-generator version
classpath "org.openapitools:openapi-generator-gradle-plugin:5.0.0"
OpenAPI declaration file content or url
I am using the following configuration for the Gradle plugin:
This is the contents of 'openapi-generator-config.json':
Suggest a fix
I am considering writing custom adapters somewhere to handle this manually, but it would be very tedious to have to do this for every single enum type in my spec. Is there an easier solution out there? Unfortunately, I don't have much control over the form the enums are in the specification. For reasons on the back end, I think I am stuck with x-enum-varnames.