openapi-typescript-codegen
openapi-typescript-codegen copied to clipboard
Property name with dashes cause incorrect typescript conversion
Describe the bug
I'm encountering a weird problem where property names with -
a dash are not converted to camel case.
Sample 1
Looking at a very basic yaml:
openapi: 3.1.0
info:
version: '1.0.0'
title: Simple API
paths: {}
definitions:
Model:
type: object
properties:
api-version:
type: string
enum:
- "3.0"
title: 'Api-Version'
default: "3.0"
Gets converted to:
pnpm openapi -i sample.yml -o testDir
export type Model = {
'api-version'?: Model.'api-version';
};
export namespace Model {
export enum 'api-version' {
_3_0 = '3.0',
}
}
Typescript doesn't allow dashes in property names, but OpenAPI does (see the 'Components Object' section). The enum api-version
is therefore invalid and cannot be used.
Note: the problem seems to be the
enum
section. If I remove it, the code is perfectly fine.
Comparison: Testing with Swagger Codegen
When testing this with the swagger-codegen tool it converts the model to:
swagger-codegen generate -i sample2.yml -l typescript-fetch
Sample 2
openapi: 3.1.0
info:
version: '1.0.0'
title: Simple API
paths:
/samplePath:
post:
parameters:
- name: optionalParam
in: query
required: false
schema:
type: string
- name: mandatoryParam
in: query
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
responses:
'200':
description: Successful operation
components:
schemas:
Model:
type: object
properties:
to:
type: string
from:
type: string
nullable: true
textType:
type: string
nullable: true
api-version:
type: string
enum:
- "3.0"
title: "Api-Version"
default: "3.0"
will result in:
/**
*
* @export
* @interface Model
*/
export interface Model {
/**
*
* @type {any}
* @memberof Model
*/
to?: any;
/**
*
* @type {any}
* @memberof Model
*/
from?: any;
/**
*
* @type {any}
* @memberof Model
*/
textType?: any;
/**
*
* @type {any}
* @memberof Model
*/
apiVersion?: any;
}
Swagger just obliterates the type here... 🤷, but at least the code is not invalid.
I'm not in control of the swagger/openapi definition file, so unfortunately I can't get it changed. Is there something else I can do short from manually fixing the generated file?
@skofgar this issue is fixed in our fork, would love for you to use it!
@skofgar this issue is fixed in our fork, would love for you to use it!
thanks for letting me know. I'll take a look