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.31k stars 6.45k forks source link

[BUG] [nodejs-express-server] Bug Generating Service with SnakeCase Parameters #12259

Open ColeMurray opened 2 years ago

ColeMurray commented 2 years ago

Bug Report Checklist

Description

When generating a service, nodejs-express-server incorrectly names properties that have underscores in the name.

Expected: userId Actual: userUnderscoreid

openapi-generator version

5.4.0

OpenAPI declaration file content or url

openapi: 3.0.3
info:
  description: Service
  title: Service
  version: "1.0"
servers:
- description: dev server
  url: http://localhost:8080
security:
- bearerAuth: []
paths:
  /v1/users/{user_id}:
    delete:
      operationId: deleteUser
      parameters:
      - description: Identifier of user to delete
        in: path
        name: user_id
        required: true
        schema:
          type: string
      responses:
        "200":
          content: {}
          description: Delete result
Generation Details

openapi-generator generate -i api-spec.yaml --package-name service -g nodejs-express-server -o ~/workplace/service

UsersService.ts

/**
* Delete a user
*
* userUnderscoreid String Identifier of user to return
* no response value expected for this operation
* */
const deleteUser = ({ userUnderscoreid }) => new Promise(
  async (resolve, reject) => {
    try {
      resolve(Service.successResponse({
        userUnderscoreid,
      }));
    } catch (e) {
      reject(Service.rejectResponse(
        e.message || 'Invalid input',
        e.status || 405,
      ));
    }
  },
);
Steps to reproduce

See above

Related issues/PRs

N/A

Suggest a fix

Utilize camelize when creating property names.

The root cause appears to be here, where underscore is not included in the pattern, where it is in the normal default.: https://github.com/OpenAPITools/openapi-generator/blob/b6a8037f62a5e4244236b65518c9f5e6fd02f27e/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java#L414

https://github.com/OpenAPITools/openapi-generator/blob/b6a8037f62a5e4244236b65518c9f5e6fd02f27e/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L5359

Which then causes underscore to be renamed by special characters in this block here: https://github.com/OpenAPITools/openapi-generator/blob/b6a8037f62a5e4244236b65518c9f5e6fd02f27e/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L1484

ColeMurray commented 2 years ago

@wing328, it looks like you're the original author of the line in question (and most of the template, thanks 🙌 ). Can you clarify if there is a reason that underscore is not included in the pattern here? If not, fix seems to be adding back underscore / removing the override all together.