Skip to content

[BUG][scala-sttp] enum is not imported correctly #8827

@aneksamun

Description

@aneksamun

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

Having a contract with not nested enum like StandAloneEnum in example below, and a type which is referencing the enum:

    StandAloneEnum:
      type: string
      enum:
        - on-my-own
    TypeWhichUsesEnum:
      type: object
      properties:
        standAloneEnum:
          $ref: '#/components/schemas/StandAloneEnum'

Leads to the compile error where compiler cannot find a generated enum.
TypeWhichUsesEnum.scala:16: not found: type StandAloneEnum

case class TypeWhichUsesEnum(
  standAloneEnum: Option[StandAloneEnum] = None // Because this enum is not imported properly
)
openapi-generator version

Occurs in 5.0.1

Generation Details
openapi-generator-cli generate \
  -i name_of_file.yaml \
  -g scala-sttp \
  --additional-properties sttpClientVersion=2.2.0,mainPackage=test.api \
  -o test-api
Related issues/PRs

It has relation to a recent contribution in #7432

Suggest a fix.

There are 2 ways of fixing the problem:

  • Do a proper enum import like this
import openapi.codegen.model.StandAloneEnum._

case class TypeWhichUsesEnum(
  standAloneEnum: Option[StandAloneEnum] = None
)

It will import type StandAloneEnum from the generated code file and it will make compiler happy.

object StandAloneEnum extends Enumeration {
    type StandAloneEnum = StandAloneEnum.Value
    val OnMyOwn = Value("on-my-own")
}
  • Or use {some_enum_name}.Value like this
case class TypeWhichUsesEnum(
  standAloneEnum: Option[StandAloneEnum.Value] = None
)

It also will make compiler happy.

P.S. it might be conflicting with enum nesting functionality: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/scala-sttp/model.mustache#L24

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions