Skip to content

[Scala] Add enums to scala-http4s-server #21320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,16 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
// model oneOf as sealed trait

CodegenModel cModel = model.getModel();
cModel.getVendorExtensions().put("x-isSealedTrait", !cModel.oneOf.isEmpty());

if (!cModel.oneOf.isEmpty()) {
cModel.getVendorExtensions().put("x-isSealedTrait", true);
}
else if (cModel.isEnum) {
cModel.getVendorExtensions().put("x-isEnum", true);

} else {
cModel.getVendorExtensions().put("x-another", true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR. what exactly is another?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this type to describe types that are not traits or enums. This is necessary to get into this branch in the mustache templates and generate case classes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will x-unsupported works better in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that "unsupported" is the right name for simple types, which generated to scala case classes. I think "another" better describes all types except traits and enums

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's go with what you've right now. we can always fine tune the extension naming later.

}

if (cModel.discriminator != null) {
cModel.getVendorExtensions().put("x-use-discr", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }
import {{.}}
{{/imports}}

{{#models}}
{{#model}}
{{#vendorExtensions.x-isEnum}}
import {{modelPackage}}.{{classname}}.{{classname}}
{{/vendorExtensions.x-isEnum}}
{{/model}}
{{/models}}
{{#models}}
{{#model}}
/**
Expand Down Expand Up @@ -82,7 +89,19 @@ object {{classname}} {
}
{{/vendorExtensions.x-isSealedTrait}}

{{^vendorExtensions.x-isSealedTrait}}
{{#vendorExtensions.x-isEnum}}
object {{classname}} extends Enumeration {
type {{classname}} = Value
{{#allowableValues}}
{{#values}}
val {{.}} = Value
{{/values}}
{{/allowableValues}}
implicit val decoder: Decoder[{{classname}}.Value] = Decoder.decodeEnumeration({{classname}})
implicit val encoder: Encoder[{{classname}}.Value] = Encoder.encodeEnumeration({{classname}})
}
{{/vendorExtensions.x-isEnum}}
{{#vendorExtensions.x-another}}
case class {{classname}}(
{{#vars}}
{{name}}: {{^required}}Option[{{{vendorExtensions.x-type}}}]{{/required}}{{#required}}{{{vendorExtensions.x-type}}}{{/required}}{{^-last}},{{/-last}}
Expand All @@ -92,7 +111,7 @@ object {{classname}} {
implicit val encoder{{classname}}: Encoder[{{classname}}] = deriveEncoder[{{classname}}].mapJson(_.dropNullValues)
implicit val decoder{{classname}}: Decoder[{{classname}}] = deriveDecoder[{{classname}}]
}
{{/vendorExtensions.x-isSealedTrait}}
{{/vendorExtensions.x-another}}

{{/model}}
{{/models}}
Loading