Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property name with dashes cause incorrect typescript conversion #1969

Open
skofgar opened this issue Jan 5, 2024 · 2 comments · Fixed by hey-api/openapi-ts#25
Open

Property name with dashes cause incorrect typescript conversion #1969

skofgar opened this issue Jan 5, 2024 · 2 comments · Fixed by hey-api/openapi-ts#25
Assignees

Comments

@skofgar
Copy link

skofgar commented Jan 5, 2024

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?

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 29, 2024

@skofgar this issue is fixed in our fork, would love for you to use it!

@skofgar
Copy link
Author

skofgar commented Mar 4, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants