openapiopenapi-generatoropenapi-generator-cli

Why is openapi-generator renaming ReadOnly to VarReadOnly and how can I make it stop doing that?


We use openapi-generator, which has recently been updated to run version 7 and require JRE class version 55 (instead of previously 52)

Now when it generates our API client code, all of our usages of properties named ReadOnly have been renamed VarReadOnly. This breaks a lot of things for us.

For reference, this is the command I send to the CLI:

openapi-generator-cli.cmd generate 
    --reserved-words-mappings readonly=readonly 
    -g csharp 
    -i http://localhost:5111/swagger/v1/swagger.json 
    --global-property skipFormModel=false -t '.\Templates\out'       
    --additional properties=targetFramework='net6.0',
        packageName=OurApi.Api.Client,useDateTimeOffset=true`

Note that I have specified --reserved-words-mappings readonly=readonly, and it still doesn't work

and this is a sample of the OpenApi doc:

  "BankAPIModel": {
    "type": "object",
    "properties": {
      "readOnly": { //<-- this will be output as VarReadOnly
        "type": "boolean"
      },
      "bankInfo": {
        "$ref": "#/components/schemas/Bank"
      },
      "pendingchangeid": {
        "type": "integer",
        "format": "int32"
      }
    },
    "additionalProperties": false
  },

And this is the resultant output

enter image description here

What can I do to enforce openapi-generator to treat this parameter/property as it's originally named?


Solution

  • The C#-generator of openapi-generator had an intentional breaking change in version 7.0.0 to handle property names which collide with reserved keywords of the language (and readonly is a keyword in C#). I do not see any documented way around this feature.

    release notes: https://github.com/OpenAPITools/openapi-generator/releases/tag/v7.0.0

    I believe this is the correct PR: https://github.com/OpenAPITools/openapi-generator/pull/13681

    Note that readOnly and readonly are considered equivalent by the generator when it comes to checking if a property-name is a reserved keyword: https://github.com/OpenAPITools/openapi-generator/pull/13681/commits/f91bd4974a9a8384e6870a24f0b97f0621817760