OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.54k stars 6.51k forks source link

enum_class_templates,0-based index #16783

Open kailaisi opened 1 year ago

kailaisi commented 1 year ago

I want use my own templement to generate kotin enum class: {{#gson}} @SerializedName(value = {{#lambda.doublequote}}{{{@index}}}{{/lambda.doublequote}}) {{/gson}}

I want the SerializedName value to be an increasing string starting from 0。But the @index not work。 In the doc:https://openapi-generator.tech/docs/templating . I find that : If you'd like a 1-based index in your array traversal, you can use {{-index}}。 But what should i do if i want a 0-based index?

wing328 commented 1 year ago

there's a PR to improve kotlin enum implementation. would that meet your requirement?

https://github.com/OpenAPITools/openapi-generator/pull/16333

kailaisi commented 1 year ago

@wing328 no,the pr is not my requirement 。 for the swagger.json like :

openapi: "3.0.2"

info:
  version: "1.0.0"
  title: Testing Kotlin generation

# We only care about schemas, but this prevents codegen error: "attribute paths is missing"
paths:
  /dummy:
    get:
      responses:
        '200':
          description: OK

components:
  schemas:

    StatusColorEnum:
      description: >
        RGB.
      type: string
      enum:
        - RED
        - YELLOW
        - GREEN

I user my own templment to generate the value:

{{#gson}}
@SerializedName(value = {{#lambda.doublequote}}{{{-index}}}{{/lambda.doublequote}})
{{/gson}}

the result is:

enum class StatusColorEnum(val value: kotlin.String) {

    @SerializedName(value = "1")
    RED("RED"),

    @SerializedName(value = "2")
    YELLOW("YELLOW"),

    @SerializedName(value = "3")
    GREEN("GREEN");

I wanted:

enum class StatusColorEnum(val value: kotlin.String) {

    @SerializedName(value = "0")
    RED("RED"),

    @SerializedName(value = "1")
    YELLOW("YELLOW"),

    @SerializedName(value = "2")
    GREEN("GREEN");

what should i do if i want the value is from 0 but not 1.