Swagger-codegen: [NODE-SERVER] Integer stub value not generated if format not defined

Created on 9 Dec 2016  路  14Comments  路  Source: swagger-api/swagger-codegen

Description

Integer swagger field definitions not generating value if format not defined. Should set value to an integer value (1 or 0).

Swagger-codegen version

2.2.1

Swagger declaration file content or url

The following definition does not generate an integer value:

  "id":{
    "readOnly":true,
    "description":"Internal id.  This field is read only.",
    "type":"integer"
  },

Adding "format":"int64" to swagger definition makes codegen set the value.

Command line used for generation

java -jar swagger-codegen-cli.jar generate -i swagger.json -l nodejs-server -o stub

Steps to reproduce

Create a swagger endpoint with "type":"integer" field. Generate stub using command above.

Related issues

None found.

Suggest a Fix

"type":"integer" should be sufficient for codegen to set the field with a stub value of either 1 or 0.

Bug Nodejs help wanted

All 14 comments

@crazyfrozenpenguin do you mind pulling the latest and build the JAR locally to give it another try as the latest master uses a newer version of Swagger Parser?

@wing328, no, this is still an issue if the definition does not include a format.

@crazyfrozenpenguin thanks for performing more tests to confirm.

Please add the format as a workaround for the time being while we (the community) look into fixing the issue.

I am fixing this

I am attempting to reproduce this error with the PetStore YAML example. I have added an integer parameter to a path without specifying the format. PetsService.js never sets such an integer to anything.
exports.showPetById = function(args, res, next) {
/**
* parameters expected in the args:
* petId (String)
* coolness (Integer)
**/
Which part of the produced output sets the Integer value to 0 or 1 if you choose an Integer format so I may reproduce the issue?

@fultonm, that is the problem, that it never sets the value to anything. Its an integer and as such it should be enough to default to int32 value (123).

The rule should be something like:

type == integer ? (format && format == int64 ? 123456789 : 123) : 123

In the PetsService.js the value coming in args is a parameter with its value presumably being set in the query or path on the client side. Especially if the field is required, it is sure the be the correct type checked by swagger-tools.

You mentioned if you specify the format of the integer to be int32 or int64 that it will initialize the variable? In which file do you see the initialization happening?

I run it on my own swagger file. Let me have a look if it is reproducible in the PetService.js. Give me a few moments.

So, edit:

modules/swagger-codegen/src/test/resources/2_0/petstore.yaml

And remove format field from field definition:

definitions:
  Order:
    title: Pet Order
    description: An order for a pets from the pet store
    type: object
    properties:
      id:
        type: integer
#        format: int64
      petId:
        type: integer
#        format: int64
      quantity:
        type: integer
#        format: int32
      shipDate:
        type: string
        format: date-time
      status:
        type: string
        description: Order Status
        enum:

Open StoreService.js and verify that fields do not have an integer set:

    var examples = {};
  examples['application/json'] = {
  "petId" : "",
  "quantity" : "",
  "id" : "",
  "shipDate" : "2000-01-23T04:56:07.000+00:00",
  "complete" : true,
  "status" : "aeiou"
};

Is this clear now?

Yes, thank you!

12/16 edit: Still working. Making good progress.

@wing328 I'm ready to submit the pull request. Which swagger-codegen branch should I submit to?

Thanks for the PR (https://github.com/swagger-api/swagger-codegen/pull/4436) from @fultonm

@crazyfrozenpenguin please pull the latest master to give it a try.

Looks good. Thanks @fultonm.

Was this page helpful?
0 / 5 - 0 ratings