OpenAPITools / OpenAPITools/openapi-generator

[BUG] getters and setters in scala codegen are incorrectly escaped

Open
#15,884 1 comment 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?
Description

For a member that's a reserved word, several properties passed to the mustache template contain backticks.

for example, properties from debugModels:

{
        "baseName" : "type",
        "getter" : "get`Type`",
        "setter" : "set`Type`",
        "name" : "`type`",
        "defaultValueWithParam" : " = data.type;",
        ...,
}
openapi-generator version

Master

Generation Details

This occurs using any of the scala-based generators where the model contains a keyword

Steps to reproduce
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/12226

Which incorrectly tags this as specific to just scala-sttp and enums

Suggest a fix

The properties getter, setter, nameInCamelCase and nameInSnakeCase should be based on the unquoted form of the member name.

The root problem is that DefaultCodegen uses toVarName

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L3636-L3641

also used here:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L3842

But that happens after escaping:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java#L291

The quickest fix would be to override getterAndSetterCapitalize in AbstractScalaCodegen to not perform escaping

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

Read DefaultCodegen.java at the referenced toVarName usages and AbstractScalaCodegen.java around getterAndSetterCapitalize. Reproduce generation for a Scala model containing a keyword, then verify that getter, setter, nameInCamelCase, and nameInSnakeCase use unquoted member names across the Scala-based generators.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.