swagger-api / swagger-api/swagger-codegen

[nodejs-server] NodeJS Server generated code doesn't map parameters with dots in name to a variables with valid format

Open
#9,364 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The problem I found is related with naming of variables mapped from swagger to nodeJS. Just as example, if we have next template:

...
  "paths": {
    "/exampleresources": {
      "get": {
        "operationId": "getExampleresources",
        "parameters": [
           {
            "description": "To obtain the list of resources according to name of category",
            "in": "query",
            "name": "category.name",
            "required": false,
            "type": "string"
          },
        ]
...

When we generate this template with a parameter with dot in name we can find something like this in code:
var category.name = req.swagger.params['category.name'].value;

This is not allowed in nodeJS, and it should be like "var categoryName".

I try server code generated in Java, and it makes this kind of adaptation correctly.

Swagger-codegen version

Swagger editor online live demo

Swagger declaration file content or url
swagger: "2.0"
info:
  description: "This is a sample server Petstore server."
  version: "0.0.0"
  title: "Swagger Petstore Example"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
  description: "Everything about your Pets"
  externalDocs:
    description: "Find out more"
    url: "http://swagger.io"
schemes:
- "https"
- "http"
paths:
  /pet:
    post:
      tags:
      - "pet"
      summary: "Add a new pet to the store"
      description: ""
      operationId: "addPet"
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
      parameters:
      - in: "body"
        name: "body.name"
        description: "Pet object that needs to be added to the store"
        required: true
        schema:
          $ref: "#/definitions/Pet"
      responses:
        405:
          description: "Invalid input"
definitions:
  Pet:
    type: "object"
    required:
    - "name"
    - "photoUrls"
    properties:
      id:
        type: "integer"
        format: "int64"
      name:
        type: "string"
        example: "doggie"
    xml:
      name: "Pet"
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"
Steps to reproduce

Just generate nodejs server and check Pet.js in controllers, there will be a code like this:

var body.name = req.swagger.params['body.name'].value;

It should be

var bodyName = req.swagger.params['body.name'].value;
Suggest a fix/enhancement

This generated name of variables should work like code generated in java by swagger codegen, just remove dot and make uppercase first character after dot:

var body.name => var bodyName

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by generating the Node.js server from the Swagger example and inspect the generated Pet.js controller, where the invalid body.name assignment is reported. Trace that output back to the Node.js server template and check how parameter names with dots are transformed. Done means generated identifiers are valid while req.swagger.params still uses the original parameter name.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.