swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17k stars 6.03k forks source link

[GO] Generator creates identifiers starting with a number #12421

Open TobiPeterG opened 3 months ago

TobiPeterG commented 3 months ago
Description

When calling the code generation with my yaml, identifiers are created that start with a number. However, the go compiler interprets them wrongly. Example code:

/*
 * API Documentation
 *
 * Source of truth and network automation platform
 *
 * API version: 2.2.5 (2.2)
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
package nautobot

type PortTypeChoices string

// List of PortTypeChoices
const (
    8P8C__PortTypeChoices PortTypeChoices = "8p8c"
    8P6C__PortTypeChoices PortTypeChoices = "8p6c"
    8P4C__PortTypeChoices PortTypeChoices = "8p4c"
    8P2C__PortTypeChoices PortTypeChoices = "8p2c"
    6P6C__PortTypeChoices PortTypeChoices = "6p6c"
    6P4C__PortTypeChoices PortTypeChoices = "6p4c"
    6P2C__PortTypeChoices PortTypeChoices = "6p2c"
    4P4C__PortTypeChoices PortTypeChoices = "4p4c"
    4P2C__PortTypeChoices PortTypeChoices = "4p2c"
    GG45_PortTypeChoices PortTypeChoices = "gg45"
    TERA_4P_PortTypeChoices PortTypeChoices = "tera-4p"
    TERA_2P_PortTypeChoices PortTypeChoices = "tera-2p"
    TERA_1P_PortTypeChoices PortTypeChoices = "tera-1p"
    110PUNCH__PortTypeChoices PortTypeChoices = "110-punch"
    BNC_PortTypeChoices PortTypeChoices = "bnc"
    F_PortTypeChoices PortTypeChoices = "f"
    N_PortTypeChoices PortTypeChoices = "n"
    MRJ21_PortTypeChoices PortTypeChoices = "mrj21"
    FC_PortTypeChoices PortTypeChoices = "fc"
    LC_PortTypeChoices PortTypeChoices = "lc"
    LC_PC_PortTypeChoices PortTypeChoices = "lc-pc"
    LC_UPC_PortTypeChoices PortTypeChoices = "lc-upc"
    LC_APC_PortTypeChoices PortTypeChoices = "lc-apc"
    LSH_PortTypeChoices PortTypeChoices = "lsh"
    LSH_PC_PortTypeChoices PortTypeChoices = "lsh-pc"
    LSH_UPC_PortTypeChoices PortTypeChoices = "lsh-upc"
    LSH_APC_PortTypeChoices PortTypeChoices = "lsh-apc"
    LX5_PortTypeChoices PortTypeChoices = "lx5"
    LX5_PC_PortTypeChoices PortTypeChoices = "lx5-pc"
    LX5_UPC_PortTypeChoices PortTypeChoices = "lx5-upc"
    LX5_APC_PortTypeChoices PortTypeChoices = "lx5-apc"
    MPO_PortTypeChoices PortTypeChoices = "mpo"
    MTRJ_PortTypeChoices PortTypeChoices = "mtrj"
    SC_PortTypeChoices PortTypeChoices = "sc"
    SC_PC_PortTypeChoices PortTypeChoices = "sc-pc"
    SC_UPC_PortTypeChoices PortTypeChoices = "sc-upc"
    SC_APC_PortTypeChoices PortTypeChoices = "sc-apc"
    ST_PortTypeChoices PortTypeChoices = "st"
    CS_PortTypeChoices PortTypeChoices = "cs"
    SN_PortTypeChoices PortTypeChoices = "sn"
    SMA_905_PortTypeChoices PortTypeChoices = "sma-905"
    SMA_906_PortTypeChoices PortTypeChoices = "sma-906"
    URM_P2_PortTypeChoices PortTypeChoices = "urm-p2"
    URM_P4_PortTypeChoices PortTypeChoices = "urm-p4"
    URM_P8_PortTypeChoices PortTypeChoices = "urm-p8"
    SPLICE_PortTypeChoices PortTypeChoices = "splice"
    OTHER_PortTypeChoices PortTypeChoices = "other"
)

E.g. the 8P is interpreted as exponent, even though it's the type. These are the errors I see with this file:

oapi-1      | ./model_port_type_choices.go:15:2: syntax error: unexpected literal 8P8, expected name
oapi-1      | ./model_port_type_choices.go:15:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:16:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:17:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:18:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:19:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:20:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:21:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:22:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:23:3: 'P' exponent requires hexadecimal mantissa
oapi-1      | ./model_port_type_choices.go:28:5: 'P' exponent requires hexadecimal mantissa
Swagger-codegen version

3.0.58

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i swagger.yaml -l go -o /client -DpackageName=nautobot

Steps to reproduce

clone this repo/go to this branch: https://github.com/TobiPeterG/go-nautobot/tree/switch-swagger-generator find swagger.yaml in api folder generate code using swagger

Suggest a fix/enhancement

Identifiers starting with a number could be prefixed.

chrisamti commented 4 days ago

Still an issue with version 3.0.63

And type replicas is int32 and swagger code creates

1__Replicas Replicas = "1"

which is wrong...

// List of Replicas const ( 1Replicas Replicas = "1" 2Replicas Replicas = "2" 3__Replicas Replicas = "3" )