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.49k stars 6.5k forks source link

Bypass Sanitizer or override it #15349

Open PierreTraversFamileo opened 1 year ago

PierreTraversFamileo commented 1 year ago

Is your feature request related to a problem? Please describe.

hi, everyone, i'm using openapi-generator for generate TS services and models

I m using Symfony 4 with Doctrine and API Platform to build the backend of my application. I generated entities using php bin/console make:entity. Each class has an id field like this one

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=CategoryRepository::class)
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;
    ...

I generated the api doc file php bin/console api:openapi:export --output=swagger.json Then I use OpenAPI Generator to generate the client part for the frontend npx openapi-generator-cli generate -i swagger.json -g typescript-angular --enable-post-process-file -o build The resulting model file categoryJsonld.ts containes 2 id fields

 export interface CategoryJsonld { 
    readonly context?: string | any | null;
    readonly id?: string;
    readonly type?: string;
   id?: number;
    ... 

I found why

because of the sanitazer in AbstractTypeScriptClientCodegen.java

name = sanitizeName(name, "[^\\w$]");

Solution ?

i we can add a parameter to bypass this sanitize or to override it that would be great !

example :

--additional-properties=notSanitize =true

output

 export interface CategoryJsonld { 
    readonly context?: string | any | null;
    readonly '@id'?: string;
    readonly type?: string;
    id?: number;
    ... 

i found a topic related !#13999 best regard, Pierre

RichardBradley commented 8 months ago

Same here -- I'm using the typescript-rxjs generator and it is changing "-" in some of my property names to "_", which means that my models don't match the spec, which defeats the point of using open-api entirely.

I'm amazed that this isn't overrideable.

The relevant code appears to be here: https://github.com/OpenAPITools/openapi-generator/blob/783e68c7acbbdcbb2282d167d1644b069f12d486/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L6481