OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generated Kotlin compilation error `Identifier expected` when comments include certain strings or characters in brackets

Open
#17,907 3 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

Description

When the OpenAPI yaml from which a (multiplatform) Kotlin project is generated contains any descriptions that have certain strings and/or symbols in brackets, the compilation of the Kotlin code fails with the error Identifier expected, since the code ends up containing invalid KDoc.

openapi-generator version

7.3.0

OpenAPI declaration file content or url

This public OpenAPI definition (YAML) reproduces the problem, when the openapi-generator Gradle plugin is used to generate Kotlin (Multiplatform) client code from it.

A specific problematic snippet from the YAML file:

        totalCost:
          type: number
          description: "Total cost of the charging session in Euro [€]."
          format: double
Generation Details

Generate a Kotlin Multiplatform client library with a build.gradle.kts file configured as in this example on GitHub.

Steps to reproduce

Then try to build the generated code with ./gradlew clean build test (you probably have to specify the subdirectory containing the generated code under build, using the -p parameter as well)

You should see errors like this:

> Task :compileCommonMainKotlinMetadata FAILED
e: file:///[path omitted]/generated/model/ChargingSession.kt:36:65 Identifier expected
e: file:///[path omitted]/generated/model/GetChargingSession.kt:36:65 Identifier expected
e: file:///[path omitted]/generated/model/GetChargingSessionResponseChargingSession.kt:35:65 Identifier expected
e: file:///[path omitted]/generated/model/PatchChargingSessionDto.kt:32:65 Identifier expected
e: file:///[path omitted]/generated/model/ReplaceChargingSessionDto.kt:32:65 Identifier expected
e: file:///[path omitted]/generated/model/ReportChargingSession.kt:33:65 Identifier expected
Related issues/PRs

This issue appears to be very similar to this one that was reported in the protobuf project earlier, and has since been fixed in that project.

Suggest a fix

Perhaps the relevant parts of this merged PR in the protobuf project could be ported to the openapi-generator project to fix it here as well.

But without even having looked at that fix in detail yet, one solution that comes to mind would be a filter or encoder for API descriptions that could for instance escape brackets and other KDoc-sensitive symbols such as brackets. Alternatively, brackets could be replaced by parentheses or other appropriate alternative symbols that KDoc wouldn't choke on.

This probably fits in the more general security best practice to always encode and escape received data context-specifically before outputting it.

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 by generating Kotlin Multiplatform code from the linked OpenAPI YAML using the Gradle plugin, then inspect the generated model files around the descriptions containing bracketed symbols. Run ./gradlew clean build test in the generated project; done means those descriptions produce valid KDoc and the generated code compiles without Identifier expected errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
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.