Open daniele-bettella opened 7 years ago
@daniele-bettella thanks for reporting the issue.
akka-scala generator has better support for enum:
swagger-codegen|master⚡ ⇒ grep -R -i enum modules/swagger-codegen/src/main/resources/akka-scala/*
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache:object EnumsSerializers {
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: def all = Seq[Serializer[_]](){{#models}}{{#model}}{{#hasEnums}}{{#vars}}{{#isEnum}} :+
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: new EnumNameSerializer({{classname}}Enums.{{datatypeWithEnum}}){{/isEnum}}{{/vars}}{{/hasEnums}}{{/model}}{{/models}}
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: private class EnumNameSerializer[E <: Enumeration: ClassTag](enum: E)
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: val EnumerationClass = classOf[E#Value]
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: case (t @ TypeInfo(EnumerationClass, _), json) if isValid(json) => {
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: enum.withName(value)
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: throw new MappingException(s"Can't convert $value to $EnumerationClass")
modules/swagger-codegen/src/main/resources/akka-scala/enumsSerializers.mustache: case JString(value) if enum.values.exists(_.toString == value) => true
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: {{{name}}}: {{^required}}Option[{{/required}}{{^isEnum}}{{datatype}}{{/isEnum}}{{#isEnum}}{{classname}}Enums.{{datatypeWithEnum}}{{/isEnum}}{{^required}}]{{/required}}{{#hasMore}},{{/hasMore}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache:{{#hasEnums}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache:object {{classname}}Enums {
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: {{#isEnum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: type {{datatypeWithEnum}} = {{datatypeWithEnum}}.Value
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: {{/isEnum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: {{#isEnum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: object {{datatypeWithEnum}} extends Enumeration {
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache:{{#_enum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: val {{#fnEnumEntry}}{{.}}{{/fnEnumEntry}} = Value("{{.}}")
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache:{{/_enum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache: {{/isEnum}}
modules/swagger-codegen/src/main/resources/akka-scala/model.mustache:{{/hasEnums}}
Maybe we can reuse these for other scala-related generators.
cc @clasnake
@wing328 Yep. Had a quick look and akka-scala uses the Enumeration class to implement this. I can try reusing this in the finch generator and other scala related generator.
@clasnake @wing328 @daniele-bettella For finch, we'll probably also need to consider compile-time serializers like circe or argonuat.
@daniele-bettella are you working on this? I had the same idea and thought about using this https://github.com/julienrf/enum
Description
The Finch code generator does not support enums. An inline enum generates a normal variable of the desired type; a referenced enum generates an empty case class.
Swagger-codegen version
2.2.2-SNAPSHOT
Swagger declaration file content or url
Suggest a Fix
As far as I know there is no single best implementation choice for enumerations in scala. Possible choices are: