3

I am developing a small web application with .NET Core backend and React (TS support) frontend.

For API definition/documentation I am using OpenAPI and have problems with code generation for the client. Bellow is a snippet of the yaml definitions (from swagger.json) that I use to generate client interfaces with Swagger Editor

crmObiskiPartnerjevResponse:
      type: object
      properties:
        id:
          type: integer
          format: int32
        PoslovniPartner:
          type: integer
          format: int32
          nullable: true
        DatumObiska:
          type: string
          format: date-time
          nullable: true
        NamenObiskaId:
          type: integer
          format: int32
          nullable: true
        Uporabnik:
          type: integer
          format: int32
          nullable: true
        Opomba:
          type: string
          nullable: true
        NamenObiskaNaziv:
          type: string
          nullable: true
        UporabnikNaziv:
          type: string
          nullable: true
        PoslovniPartnerNaziv:
          type: string
          nullable: true
      nullable: true

The problem is that when generating client code for typescript, Swagger editor messes up naming of properties. In this example it converts first letter to small cap (PoslovniPartner -> poslovniPartner). Any ideas would how to solve this would be much appreciated.

/**
 * ECE_crmWebAPI
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: v1
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

export interface CrmObiskiPartnerjevResponse { 
    id?: number;
    poslovniPartner?: number;
    datumObiska?: Date;
    namenObiskaId?: number;
    uporabnik?: number;
    opomba?: string;
    namenObiskaNaziv?: string;
    uporabnikNaziv?: string;
    poslovniPartnerNaziv?: string;
}

1 Answer 1

6

Please have a look at the Codegen FAQ: Codegen - FAQ

The JSON response failed to deserialize properly into the object due to change in variable naming (snake_case to camelCase). Is there any way to keep the original naming?

Yes, please use the following option when generating TypeScript clients:

modelPropertyNaming
    Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name (Default: camelCase)

So, please set modelPropertyNaming to original

6
  • Thank you @MarkusMeyer for fast reply, I will try it out. Commented Dec 31, 2019 at 11:28
  • Please mark the answer as accepted if it worked! That gives credit to the user who helped you. Commented Jan 8, 2020 at 11:15
  • @Markus Where do you set this option? Is it part of the openapi-generator CLI or an option somewhere in the openapi.yaml file?
    – GreenGiant
    Commented Nov 2, 2020 at 18:33
  • 1
    Use this java -jar openapi-generator-cli-4.1.3.jar generate -i C:\Users\ASUS\Desktop\OpenAPICodegen\swagger.json -g typescript-angular -o C:\Users\ASUS\Desktop\OpenAPICodegen\dist --additional-properties modelPropertyNaming=origional Commented Oct 25, 2021 at 11:34
  • 1
    Another other way is to pass a JSON file with additional properties -c ./swagger-codegen-options.json where the file looks like `` Commented Jul 13, 2022 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.