OpenAPITools / OpenAPITools/openapi-generator

[BUG][scala-sttp] Error when compiling with an Enum "none"

Open
#18,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If a spec has an enum with the value "none", and the enum is used in a model which has optional fields, then the Enum.None conflicts with the builtin scala.None.

openapi-generator version

7.4.0

OpenAPI declaration file content or url

https://gist.github.com/erik-hasse/5f568c43fdd8765a9a24fa907c12d0c3

Generation Details
openapi-generator generate \
  -g scala-sttp \
  -i https://gist.githubusercontent.com/erik-hasse/5f568c43fdd8765a9a24fa907c12d0c3/raw/b2b9310b8a2b95254ce418d9c06831f5383136fe/openapi.yaml 
Steps to reproduce

After generating the client, run sbt compile. This results in the following message:

[info] welcome to sbt 1.6.1 (Homebrew Java 11.0.22)
[info] loading project definition from /Users/Erik/projects/enum-example/project
[info] loading settings for project enum-example from build.sbt ...
[info] set current project to openapi-client (in build file:/Users/Erik/projects/enum-example/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] compiling 6 Scala sources to /Users/Erik/projects/enum-example/target/scala-2.13/classes ...
[error] /Users/Erik/projects/enum-example/src/main/scala/org/openapitools/client/model/Message.scala:18:25: type mismatch;
[error]  found   : org.openapitools.client.model.MyEnum.Value
[error]  required: Option[Int]
[error]   optVal: Option[Int] = None
[error]                         ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 2 s, completed Mar 15, 2024, 4:48:27 PM

The problem comes from the wildcard import import org.openapitools.client.model.MyEnum._ overriding the built-in None.

Related issues/PRs

Related issues which recommended the wildcard import that's causing the problem here:
https://github.com/OpenAPITools/openapi-generator/issues/14545
https://github.com/OpenAPITools/openapi-generator/issues/8827

Suggest a fix

There are three options I see:

  1. Avoid the wildcard import and either use MyEnum.Value as the type annotation (replacing https://github.com/OpenAPITools/openapi-generator/pull/14546)
  2. Add None as a reserved word. This may also requires a custom reserved word mapping since the backticks don't sufficiently differentiate it from the builtin.
  3. Always explicitly refer to scala.None in the template.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/scala-sttp/model.mustache and modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java, then generate the linked OpenAPI declaration with the shown command. Run sbt compile on the generated client and verify that an enum value named "none" no longer conflicts with scala.None when optional model fields are present.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.