swagger-api / swagger-api/swagger-codegen

[Nodejs-server] Bug generating server - reference to service files

Open
#9,040 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

When a nodejs-server code is generated, the generated code has incorrect reference to dependencies. Once the reference is corrected, the code works as is. (So should be an easy fix)

Swagger-codegen version

swagger-codegen-cli-2.3.1.jar

Swagger declaration file content or url

Note: This can be any working swagger 2.0 file. This is just an example

swagger: '2.0'
info:
  title: User API
  version: '1.0'
  description: Manage users
host: 'localhost:8080'
paths:
  /users:
    get:
      summary: List User
      operationId: findUsers
      responses:
        '200':
          description: ''
          schema:
            type: array
            items:
              $ref: '#/definitions/user-output'
      tags:
        - user
basePath: /v1
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json
definitions:
  user-common:
    title: User Common
    description: The properties that are shared amongst all versions of the User model.
    type: object
    properties:
      name:
        type: string
        description: Name of the user
      email:
        type: string
        description: email address
      type:
        type: string
        enum:
          - primary
          - secondary
        description: Whether Primary or Secondary user type
    required:
      - name
      - email
      - type
  user-output:
    title: User Output
    description: The properties that are included when fetching a list of Users.
    allOf:
      - type: object
        properties:
          created:
            type: string
            format: date-time
          id:
            type: integer
        required:
          - created
          - id
      - $ref: '#/definitions/user-common'

tags:
  - name: user
    description: User related APIs
Command line used for generation

java -jar C:\swagger-codegen-cli-2.3.1.jar generate -l nodejs-server -o .\src -i .\specs\api.oas2.yaml

Steps to reproduce
  1. Generate nodejs-server code using the command above and any properly created yaml
  2. Go to /src and run npm start
  3. Npm install will fail as version in package.json file is unsupported <== Lower priority issue
  4. Update the version in package.json file to 1.0.0 instead of 1.0
  5. Run npm start again. This time npm install will work, but the code will fail with the following error message:
    Error: Cannot find module '../service/UserService'
  6. To fix the issue, go to controllers/User.js and modify the import reference for UserService.js to the right file ('./UserService')
Related issues/PRs
Suggest a fix/enhancement

Fix the reference or create a new '/service' folder and move appropriate files into that folder while code generation.

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

Generate the nodejs-server output from the example command, then inspect src/controllers/User.js and the generated service file to confirm the incorrect reference. Search the generator templates for the corresponding UserService import and verify that npm start succeeds after the package.json version adjustment.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.