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

Conversation

KatyaValik
Copy link
Contributor

@KatyaValik KatyaValik commented May 23, 2025

There is an example of correct openAPI specification:

info:
  title: TestApi
  version: latest

components:
  schemas:
    EnumType:
      type: string
      enum:
        - Fix
        - Max
paths:
  /test/method:
    post:
      responses:
        '200':
          description: "success answer"
          content:
            application/json:
              schema:
                type: object
                required:
                  - constraint
                properties:
                  constraint:
                    $ref: '#/components/schemas/EnumType'

For given OpenAPI specification generated not valid type for enum EnumType - empty case class:

case class EnumType()

I added enum handling for scala-http4s-server.
Trying to fix #9987 for scala-http4s-server.

@KatyaValik KatyaValik marked this pull request as draft May 23, 2025 13:45
@KatyaValik KatyaValik force-pushed the scala-http4s-server-enums-fix branch from 47c1ab8 to 1cb9c2f Compare May 26, 2025 06:59
@KatyaValik KatyaValik marked this pull request as ready for review May 26, 2025 07:01
@KatyaValik KatyaValik changed the title [Scala] Add enums to scala-htt4s-server [Scala] Add enums to scala-http4s-server May 26, 2025
@KatyaValik KatyaValik force-pushed the scala-http4s-server-enums-fix branch from 1cb9c2f to 3e46ec6 Compare May 27, 2025 13:04
@wing328
Copy link
Member

wing328 commented May 27, 2025

thanks for the PR

cc
@clasnake (2017/07), @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04) @Fish86 (2023/06)

@wing328
Copy link
Member

wing328 commented May 27, 2025

https://github.com/OpenAPITools/openapi-generator/actions/runs/15276044149/job/42973797933?pr=21320

please follow step 3 to update the samples so that the CI can verify the change

@KatyaValik KatyaValik force-pushed the scala-http4s-server-enums-fix branch 2 times, most recently from 393f94d to de1c5c3 Compare May 28, 2025 12:11
@KatyaValik KatyaValik force-pushed the scala-http4s-server-enums-fix branch from de1c5c3 to 3225064 Compare June 2, 2025 12:47
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.

@wing328
Copy link
Member

wing328 commented Jun 16, 2025

tested locally and the result is good

$ sbt compile
[info] welcome to sbt 1.10.11 (Eclipse Adoptium Java 17.0.10)
[info] loading settings for project global-plugins from gpg.sbt...
[info] loading global plugins from C:\Users\wing3\.sbt\1.0\plugins
[info] loading project definition from C:\Users\wing3\AppData\Local\Temp\enumserver\project
[info] loading settings for project enumserver from build.sbt...
[info] set current project to enumserver (in build file:/C:/Users/wing3/AppData/Local/Temp/enumserver/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 1 Scala source to C:\Users\wing3\AppData\Local\Temp\enumserver\target\scala-2.13\classes ...
[info] done compiling
[info] compiling 1 Scala source to C:\Users\wing3\AppData\Local\Temp\enumserver\target\scala-2.13\classes ...
[info] done compiling
[success] Total time: 11 s, completed Jun 16, 2025, 11:36:45 AM

here is what the change looks like before and after applying this fix

diff --git a/src/main/scala/org/openapitools/models/types.scala b/src/main/scala/org/openapitools/models/types.scala
index 8247885..2075ca8 100644
--- a/src/main/scala/org/openapitools/models/types.scala
+++ b/src/main/scala/org/openapitools/models/types.scala
@@ -8,15 +8,17 @@ import io.circe.{ Decoder, Encoder }
 import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }


+import org.openapitools.models.EnumType.EnumType
 /**
 *
 */

-case class EnumType(
-)
-object EnumType {
-  implicit val encoderEnumType: Encoder[EnumType] = deriveEncoder[EnumType].mapJson(_.dropNullValues)
-  implicit val decoderEnumType: Decoder[EnumType] = deriveDecoder[EnumType]
+object EnumType extends Enumeration {
+    type EnumType = Value
+    val Fix = Value
+    val Max = Value
+    implicit val decoder: Decoder[EnumType.Value] = Decoder.decodeEnumeration(EnumType)
+    implicit val encoder: Encoder[EnumType.Value] = Encoder.encodeEnumeration(EnumType)
 }

 /**

@wing328 wing328 merged commit fc29daa into OpenAPITools:master Jun 16, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG][SCALA-AKKA] Fix scala-akka enum generation
3 participants