swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.03k stars 6.03k forks source link

[JavaScript] Read and parse 'models' section correctly #1984

Closed tomduhourq closed 7 years ago

tomduhourq commented 8 years ago

I currently have the following Api Spec

{
  "apiVersion": "0.1",
  "swaggerVersion": "1.2",
  "basePath": "/",
  "resourcePath": "/binnacle",
  "produces": [
    "text/plain; charset=UTF-8"
  ],
  "apis": [
    {
      "path": "/binnacle/checkoutStep",
      "operations": [
        {
          "method": "POST",
          "summary": "Logs when a user passes through a checkout step",
          "notes": "",
          "type": "string",
          "nickname": "checkoutStep",
          "produces": [
            "text/plain"
          ],
          "consumes": [
            "application/json",
            "application/vnd.custom.inputParams+json"
          ],
          "parameters": [
            {
              "name": "body",
              "description": "The information provided when changing a checkout step.",
              "required": true,
              "type": "InputParamCheckoutStep",
              "paramType": "body",
              "allowMultiple": false
            }
          ],
          "responseMessages": [
            {
              "code": 200,
              "message": "Thanks for using this API!"
            },
            {
              "code": 400,
              "message": "A message describing the missing arguments"
            }
          ]
        }
      ]
    }
  ],
  "models": {
    "InputParamCheckoutStep": {
      "id": "InputParamCheckoutStep",
      "description": "Input Parameters representing a checkout option",
      "required": [
        "userId",
        "chkStep",
        "chkOption",
        "googleId"
      ],
      "properties": {
        "userId": {
          "type": "string",
          "description": "User Identifier"
        },
        "source": {
          "$ref": "EventSource",
          "description": "Event Source"
        },
        "chkStep": {
          "type": "integer",
          "format": "int32",
          "description": "Checkout Step"
        },
        "chkOption": {
          "type": "string",
          "description": "Checkout Option"
        },
        "googleId": {
          "type": "string",
          "description": "Google Id"
        }
      }
    }
  }
}

When generating my js service I currently have no field specification of the model InputParamCheckoutSpec, only its name as a parameter in the generated NodeJs code, like this:

exports.checkoutStep = function(body) {
  var examples = {};
  if(Object.keys(examples).length > 0)
    return examples[Object.keys(examples)[0]];

}

I am using the latest version like this:

java -jar jars/swagger-codegen-cli-2.1.4.jar generate \
-i http://localhost:8080/api-docs/binnacle \
-l nodejs \
-o dirHere

What I would like to have are the fields required on the endpoints, such as chkStep and googleId on the generated code.

fehguy commented 8 years ago

Well if that's your entire spec, I can say that the parser isn't going to like it.

source has an unqualified and non-existent reference. What does EventSource look like? source has a $ref with a description, which is not allowed. $ref must live alone on a single line.

You are also using an old swagger-spec version. Can you just convert it using the -l swagger to 2.0 and work with that?

tomduhourq commented 8 years ago

source is an Enumerator. Got the new swagger.json with the command you passed over with no success. Here is the Binnacle.js file method:

module.exports.checkoutStep = function checkoutStep (req, res, next) {
  var body = req.swagger.params['body'].value;

  var result = Binnacle.checkoutStep(body);

  if(typeof result !== 'undefined') {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify(result || {}, null, 2));
  }
  else
    res.end();
};

Does the codegen have any trouble with Enums?

wing328 commented 8 years ago

@tomduhourq please pull the latest master (which has better enum support for several lanaguages) to give it a try.

In your spec, I don't see parameter or property defined as enum though.

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L105

wing328 commented 7 years ago

Closing this as there's no update.