From c7e5c305a937a7dcd86b79b2b62703f4d66bf015 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Tue, 17 Nov 2015 18:46:33 -0800 Subject: [PATCH] fixes #1581, rebuilt server --- .../io/swagger/codegen/DefaultGenerator.java | 24 +- .../languages/NodeJSServerCodegen.java | 41 +- .../main/resources/nodejs/controller.mustache | 12 +- .../src/main/resources/nodejs/index.mustache | 6 +- .../main/resources/nodejs/package.mustache | 3 +- .../main/resources/nodejs/service.mustache | 18 +- .../main/resources/nodejs/swagger.mustache | 44 +- samples/server/petstore/nodejs/README.md | 24 + .../server/petstore/nodejs/api/swagger.json | 867 ------------------ .../server/petstore/nodejs/api/swagger.yaml | 667 ++++++++++++++ .../server/petstore/nodejs/controllers/Pet.js | 101 +- .../petstore/nodejs/controllers/PetService.js | 104 ++- .../petstore/nodejs/controllers/Store.js | 47 +- .../nodejs/controllers/StoreService.js | 66 +- .../petstore/nodejs/controllers/User.js | 97 +- .../nodejs/controllers/UserService.js | 91 +- samples/server/petstore/nodejs/index.js | 6 +- samples/server/petstore/nodejs/package.json | 3 +- 18 files changed, 969 insertions(+), 1252 deletions(-) create mode 100644 samples/server/petstore/nodejs/README.md delete mode 100644 samples/server/petstore/nodejs/api/swagger.json create mode 100644 samples/server/petstore/nodejs/api/swagger.yaml diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 5fa165f0788..3623361387e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -1,19 +1,8 @@ package io.swagger.codegen; -import static org.apache.commons.lang3.StringUtils.capitalize; -import static org.apache.commons.lang3.StringUtils.isNotEmpty; - import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Template; -import io.swagger.models.ComposedModel; -import io.swagger.models.Contact; -import io.swagger.models.Info; -import io.swagger.models.License; -import io.swagger.models.Model; -import io.swagger.models.Operation; -import io.swagger.models.Path; -import io.swagger.models.SecurityRequirement; -import io.swagger.models.Swagger; +import io.swagger.models.*; import io.swagger.models.auth.OAuth2Definition; import io.swagger.models.auth.SecuritySchemeDefinition; import io.swagger.models.parameters.Parameter; @@ -23,14 +12,12 @@ import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Reader; +import java.io.*; import java.util.*; +import static org.apache.commons.lang3.StringUtils.capitalize; +import static org.apache.commons.lang3.StringUtils.isNotEmpty; + public class DefaultGenerator extends AbstractGenerator implements Generator { Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class); @@ -321,6 +308,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { if (swagger.getHost() != null) { bundle.put("host", swagger.getHost()); } + bundle.put("swagger", this.swagger); bundle.put("basePath", basePath); bundle.put("scheme", scheme); bundle.put("contextPath", contextPath); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java index 664530c33d8..4dee5f6f2df 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java @@ -1,27 +1,16 @@ package io.swagger.codegen.languages; -import io.swagger.codegen.CodegenConfig; -import io.swagger.codegen.CodegenOperation; -import io.swagger.codegen.CodegenParameter; -import io.swagger.codegen.CodegenResponse; -import io.swagger.codegen.CodegenType; -import io.swagger.codegen.DefaultCodegen; -import io.swagger.codegen.SupportingFile; - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.fasterxml.jackson.core.JsonProcessingException; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; +import io.swagger.codegen.*; +import io.swagger.models.Swagger; +import io.swagger.util.Yaml; + +import java.io.File; +import java.util.*; +import java.util.Map.Entry; public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig { protected String apiVersion = "1.0.0"; @@ -87,7 +76,7 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig // ); supportingFiles.add(new SupportingFile("swagger.mustache", "api", - "swagger.json") + "swagger.yaml") ); supportingFiles.add(new SupportingFile("index.mustache", "", @@ -97,6 +86,10 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig "", "package.json") ); + supportingFiles.add(new SupportingFile("README.mustache", + "", + "README.md") + ); if (System.getProperty("noservice") == null) { apiTemplateFiles.put( "service.mustache", // the template to use @@ -242,6 +235,14 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig @Override public Map postProcessSupportingFileData(Map objs) { + Swagger swagger = (Swagger)objs.get("swagger"); + if(swagger != null) { + try { + objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(swagger)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + } for (Map operations : getOperations(objs)) { @SuppressWarnings("unchecked") List ops = (List) operations.get("operation"); diff --git a/modules/swagger-codegen/src/main/resources/nodejs/controller.mustache b/modules/swagger-codegen/src/main/resources/nodejs/controller.mustache index 85d1f3211a1..0f0c35bc0ee 100644 --- a/modules/swagger-codegen/src/main/resources/nodejs/controller.mustache +++ b/modules/swagger-codegen/src/main/resources/nodejs/controller.mustache @@ -9,17 +9,7 @@ var {{classname}} = require('./{{classname}}Service'); {{#operation}} module.exports.{{nickname}} = function {{nickname}} (req, res, next) { - {{#allParams}}var {{paramName}} = req.swagger.params['{{baseName}}'].value; - {{/allParams}} - - var result = {{classname}}.{{nickname}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + {{classname}}.{{nickname}}(req.swagger.params, res, next); }; {{/operation}} {{/operations}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/nodejs/index.mustache b/modules/swagger-codegen/src/main/resources/nodejs/index.mustache index 419b3fb5e63..397661d4d52 100644 --- a/modules/swagger-codegen/src/main/resources/nodejs/index.mustache +++ b/modules/swagger-codegen/src/main/resources/nodejs/index.mustache @@ -3,7 +3,8 @@ var app = require('connect')(); var http = require('http'); var swaggerTools = require('swagger-tools'); - +var jsyaml = require('js-yaml'); +var fs = require('fs'); var serverPort = {{serverPort}}; // swaggerRouter configuration @@ -14,7 +15,8 @@ var options = { }; // The Swagger document (require it, build it programmatically, fetch it from a URL, ...) -var swaggerDoc = require('./api/swagger.json'); +var spec = fs.readFileSync('./api/swagger.yaml', 'utf8'); +var swaggerDoc = jsyaml.safeLoad(spec); // Initialize the Swagger middleware swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) { diff --git a/modules/swagger-codegen/src/main/resources/nodejs/package.mustache b/modules/swagger-codegen/src/main/resources/nodejs/package.mustache index 89db4c0b91e..86aa21af635 100644 --- a/modules/swagger-codegen/src/main/resources/nodejs/package.mustache +++ b/modules/swagger-codegen/src/main/resources/nodejs/package.mustache @@ -10,6 +10,7 @@ "private": true, "dependencies": { "connect": "^3.2.0", - "swagger-tools": "0.8.*" + "js-yaml": "^3.3.0", + "swagger-tools": "0.9.*" } } diff --git a/modules/swagger-codegen/src/main/resources/nodejs/service.mustache b/modules/swagger-codegen/src/main/resources/nodejs/service.mustache index d7c2ade5079..5073d249275 100644 --- a/modules/swagger-codegen/src/main/resources/nodejs/service.mustache +++ b/modules/swagger-codegen/src/main/resources/nodejs/service.mustache @@ -2,17 +2,27 @@ {{#operations}} {{#operation}} -exports.{{nickname}} = function({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { +exports.{{nickname}} = function(args, res, next) { + /** + * parameters expected in the args: + {{#allParams}}* {{paramName}} ({{dataType}}) + {{/allParams}}**/ - var examples = {}; +var examples = {}; {{#examples}} examples['{{contentType}}'] = {{{example}}}; {{/examples}} {{#returnType}} - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } {{/returnType}} + {{^returnType}}res.end();{{/returnType}} } {{/operation}} {{/operations}} diff --git a/modules/swagger-codegen/src/main/resources/nodejs/swagger.mustache b/modules/swagger-codegen/src/main/resources/nodejs/swagger.mustache index 1d0f7d78162..51560926bba 100644 --- a/modules/swagger-codegen/src/main/resources/nodejs/swagger.mustache +++ b/modules/swagger-codegen/src/main/resources/nodejs/swagger.mustache @@ -1,43 +1 @@ -{ - "swagger": "2.0", - "info": { - "title": "{{appName}}", - "description": "{{{appDescription}}}", - "version": "{{apiVersion}}" - }, -{{#apiInfo}} - "produces": ["application/json"], - "host": "localhost:{{serverPort}}", - "basePath": "{{contextPath}}", - "paths": { -{{#apis}} -{{#operations}} - {{#operationsByPath}} - "{{{path}}}": { - {{#operation}} - "{{httpMethod}}": { - "summary": "{{summary}}", - "description":"{{notes}}", - "x-swagger-router-controller": "{{classname}}", - "tags": ["{{baseName}}"], - "operationId": "{{operationId}}",{{#hasParams}} - "parameters": [ - {{#allParams}}{{{jsonSchema}}}{{#hasMore}},{{/hasMore}} - {{/allParams}} - ],{{/hasParams}} - "responses": { - {{#responses}}"{{code}}": {{{jsonSchema}}} - {{#hasMore}},{{/hasMore}} - {{/responses}} - } - } {{#hasMore}},{{/hasMore}} - {{/operation}} - } {{#hasMore}},{{/hasMore}} - {{/operationsByPath}} -{{/operations}} -{{/apis}} -{{/apiInfo}} - }, "definitions": { - {{#models}}{{#model}}"{{classVarName}}": {{{modelJson}}}{{#hasMoreModels}},{{/hasMoreModels}}{{/model}}{{/models}} - } -} +{{{swagger-yaml}}} \ No newline at end of file diff --git a/samples/server/petstore/nodejs/README.md b/samples/server/petstore/nodejs/README.md new file mode 100644 index 00000000000..edeea0005c0 --- /dev/null +++ b/samples/server/petstore/nodejs/README.md @@ -0,0 +1,24 @@ +# Swagger generated server + +## Overview +This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [swagger-spec](https://github.com/swagger-api/swagger-spec) from a remote server, you can easily generate a server stub. This is an example of building a node.js server. + +This example uses the [expressjs](http://expressjs.com/) framework. To see how to make this your own, look here: + +[README](https://github.com/swagger-api/swagger-codegen/README.md) + +### Running the server +To run the server, follow these simple steps: + +``` +npm install +node . +``` + +To view the Swagger UI interface: + +``` +open http://localhost:8080/docs +``` + +This project leverages the mega-awesome [swagger-tools](https://github.com/apigee-127/swagger-tools) middleware which does most all the work. \ No newline at end of file diff --git a/samples/server/petstore/nodejs/api/swagger.json b/samples/server/petstore/nodejs/api/swagger.json deleted file mode 100644 index 4c0ad6ea42f..00000000000 --- a/samples/server/petstore/nodejs/api/swagger.json +++ /dev/null @@ -1,867 +0,0 @@ -{ - "swagger": "2.0", - "info": { - "title": "Swagger Petstore", - "description": "This is a sample server Petstore server. You can find out more about Swagger at http://swagger.io or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters", - "version": "1.0.0" - }, - "produces": ["application/json"], - "host": "localhost:8080", - "basePath": "/v2", - "paths": { - - "/user/createWithList": { - - "post": { - "summary": "Creates list of users with given input array", - "description":"", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "createUsersWithListInput", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "List of user object", - "required" : false, - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/User" - } - } -} - - ], - "responses": { - "default": { - "description" : "successful operation" -} - - - } - } - - } , - - "/user/createWithArray": { - - "post": { - "summary": "Creates list of users with given input array", - "description":"", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "createUsersWithArrayInput", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "List of user object", - "required" : false, - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/User" - } - } -} - - ], - "responses": { - "default": { - "description" : "successful operation" -} - - - } - } - - } , - - "/user/{username}": { - - "get": { - "summary": "Get user by user name", - "description":"", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "getUserByName", - "parameters": [ - { - "name" : "username", - "in" : "path", - "description" : "The name that needs to be fetched. Use user1 for testing. ", - "required" : true, - "type" : "string" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/User" - }, - "examples" : { - "application/json" : { - "id" : 1, - "username" : "johnp", - "firstName" : "John", - "lastName" : "Public", - "email" : "johnp@swagger.io", - "password" : "-secret-", - "phone" : "0123456789", - "userStatus" : 0 - } - } -} - , - "400": { - "description" : "Invalid username supplied" -} - , - "404": { - "description" : "User not found" -} - - - } - } , - - "put": { - "summary": "Updated user", - "description":"This can only be done by the logged in user.", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "updateUser", - "parameters": [ - { - "name" : "username", - "in" : "path", - "description" : "name that need to be deleted", - "required" : true, - "type" : "string" -}, - { - "in" : "body", - "name" : "body", - "description" : "Updated user object", - "required" : false, - "schema" : { - "$ref" : "#/definitions/User" - } -} - - ], - "responses": { - "400": { - "description" : "Invalid user supplied" -} - , - "404": { - "description" : "User not found" -} - - - } - } , - - "delete": { - "summary": "Delete user", - "description":"This can only be done by the logged in user.", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "deleteUser", - "parameters": [ - { - "name" : "username", - "in" : "path", - "description" : "The name that needs to be deleted", - "required" : true, - "type" : "string" -} - - ], - "responses": { - "400": { - "description" : "Invalid username supplied" -} - , - "404": { - "description" : "User not found" -} - - - } - } - - } , - - "/user": { - - "post": { - "summary": "Create user", - "description":"This can only be done by the logged in user.", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "createUser", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "Created user object", - "required" : false, - "schema" : { - "$ref" : "#/definitions/User" - } -} - - ], - "responses": { - "default": { - "description" : "successful operation" -} - - - } - } - - } , - - "/user/logout": { - - "get": { - "summary": "Logs out current logged in user session", - "description":"", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "logoutUser", - "responses": { - "default": { - "description" : "successful operation" -} - - - } - } - - } , - - "/user/login": { - - "get": { - "summary": "Logs user into the system", - "description":"", - "x-swagger-router-controller": "User", - "tags": ["User"], - "operationId": "loginUser", - "parameters": [ - { - "name" : "username", - "in" : "query", - "description" : "The user name for login", - "required" : false, - "type" : "string" -}, - { - "name" : "password", - "in" : "query", - "description" : "The password for login in clear text", - "required" : false, - "type" : "string" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "type" : "string" - } -} - , - "400": { - "description" : "Invalid username/password supplied" -} - - - } - } - - } , - - - "/pet": { - - "put": { - "summary": "Update an existing pet", - "description":"", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "updatePet", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "Pet object that needs to be added to the store", - "required" : false, - "schema" : { - "$ref" : "#/definitions/Pet" - } -} - - ], - "responses": { - "400": { - "description" : "Invalid ID supplied" -} - , - "404": { - "description" : "Pet not found" -} - , - "405": { - "description" : "Validation exception" -} - - - } - } , - - "post": { - "summary": "Add a new pet to the store", - "description":"", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "addPet", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "Pet object that needs to be added to the store", - "required" : false, - "schema" : { - "$ref" : "#/definitions/Pet" - } -} - - ], - "responses": { - "405": { - "description" : "Invalid input" -} - - - } - } - - } , - - "/pet/{petId}/uploadImage": { - - "post": { - "summary": "uploads an image", - "description":"", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "uploadFile", - "parameters": [ - { - "name" : "petId", - "in" : "path", - "description" : "ID of pet to update", - "required" : true, - "type" : "integer", - "format" : "int64" -}, - { - "name" : "additionalMetadata", - "in" : "formData", - "description" : "Additional data to pass to server", - "required" : false, - "type" : "string" -}, - { - "name" : "file", - "in" : "formData", - "description" : "file to upload", - "required" : false, - "type" : "file" -} - - ], - "responses": { - "default": { - "description" : "successful operation" -} - - - } - } - - } , - - "/pet/findByStatus": { - - "get": { - "summary": "Finds Pets by status", - "description":"Multiple status values can be provided with comma seperated strings", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "findPetsByStatus", - "parameters": [ - { - "name" : "status", - "in" : "query", - "description" : "Status values that need to be considered for filter", - "required" : false, - "type" : "array", - "collectionFormat" : "multi", - "default" : "available" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/Pet" - } - } -} - , - "400": { - "description" : "Invalid status value" -} - - - } - } - - } , - - "/pet/findByTags": { - - "get": { - "summary": "Finds Pets by tags", - "description":"Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "findPetsByTags", - "parameters": [ - { - "name" : "tags", - "in" : "query", - "description" : "Tags to filter by", - "required" : false, - "type" : "array", - "collectionFormat" : "multi" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "type" : "array", - "items" : { - "$ref" : "#/definitions/Pet" - } - } -} - , - "400": { - "description" : "Invalid tag value" -} - - - } - } - - } , - - "/pet/{petId}": { - - "get": { - "summary": "Find pet by ID", - "description":"Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "getPetById", - "parameters": [ - { - "name" : "petId", - "in" : "path", - "description" : "ID of pet that needs to be fetched", - "required" : true, - "type" : "integer", - "format" : "int64" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Pet" - } -} - , - "400": { - "description" : "Invalid ID supplied" -} - , - "404": { - "description" : "Pet not found" -} - - - } - } , - - "post": { - "summary": "Updates a pet in the store with form data", - "description":"", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "updatePetWithForm", - "parameters": [ - { - "name" : "petId", - "in" : "path", - "description" : "ID of pet that needs to be updated", - "required" : true, - "type" : "string" -}, - { - "name" : "name", - "in" : "formData", - "description" : "Updated name of the pet", - "required" : false, - "type" : "string" -}, - { - "name" : "status", - "in" : "formData", - "description" : "Updated status of the pet", - "required" : false, - "type" : "string" -} - - ], - "responses": { - "405": { - "description" : "Invalid input" -} - - - } - } , - - "delete": { - "summary": "Deletes a pet", - "description":"", - "x-swagger-router-controller": "Pet", - "tags": ["Pet"], - "operationId": "deletePet", - "parameters": [ - { - "name" : "petId", - "in" : "path", - "description" : "Pet id to delete", - "required" : true, - "type" : "integer", - "format" : "int64" -}, - { - "name" : "api_key", - "in" : "header", - "description" : "", - "required" : false, - "type" : "string" -} - - ], - "responses": { - "400": { - "description" : "Invalid pet value" -} - - - } - } - - } , - - - "/store/order/{orderId}": { - - "get": { - "summary": "Find purchase order by ID", - "description":"For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", - "x-swagger-router-controller": "Store", - "tags": ["Store"], - "operationId": "getOrderById", - "parameters": [ - { - "name" : "orderId", - "in" : "path", - "description" : "ID of pet that needs to be fetched", - "required" : true, - "type" : "string" -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Order" - } -} - , - "400": { - "description" : "Invalid ID supplied" -} - , - "404": { - "description" : "Order not found" -} - - - } - } , - - "delete": { - "summary": "Delete purchase order by ID", - "description":"For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", - "x-swagger-router-controller": "Store", - "tags": ["Store"], - "operationId": "deleteOrder", - "parameters": [ - { - "name" : "orderId", - "in" : "path", - "description" : "ID of the order that needs to be deleted", - "required" : true, - "type" : "string" -} - - ], - "responses": { - "400": { - "description" : "Invalid ID supplied" -} - , - "404": { - "description" : "Order not found" -} - - - } - } - - } , - - "/store/order": { - - "post": { - "summary": "Place an order for a pet", - "description":"", - "x-swagger-router-controller": "Store", - "tags": ["Store"], - "operationId": "placeOrder", - "parameters": [ - { - "in" : "body", - "name" : "body", - "description" : "order placed for purchasing the pet", - "required" : false, - "schema" : { - "$ref" : "#/definitions/Order" - } -} - - ], - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "$ref" : "#/definitions/Order" - } -} - , - "400": { - "description" : "Invalid Order" -} - - - } - } - - } , - - "/store/inventory": { - - "get": { - "summary": "Returns pet inventories by status", - "description":"Returns a map of status codes to quantities", - "x-swagger-router-controller": "Store", - "tags": ["Store"], - "operationId": "getInventory", - "responses": { - "200": { - "description" : "successful operation", - "schema" : { - "type" : "object", - "additionalProperties" : { - "type" : "integer", - "format" : "int32" - } - } -} - - - } - } - - } - - }, "definitions": { - "User": { - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "username" : { - "type" : "string" - }, - "firstName" : { - "type" : "string" - }, - "lastName" : { - "type" : "string" - }, - "email" : { - "type" : "string" - }, - "password" : { - "type" : "string" - }, - "phone" : { - "type" : "string" - }, - "userStatus" : { - "type" : "integer", - "format" : "int32", - "description" : "User Status" - } - }, - "xml" : { - "name" : "User" - } -},"Category": { - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "name" : { - "type" : "string" - } - }, - "xml" : { - "name" : "Category" - } -},"Pet": { - "required" : [ "name", "photoUrls" ], - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "category" : { - "$ref" : "#/definitions/Category" - }, - "name" : { - "type" : "string", - "example" : "doggie" - }, - "photoUrls" : { - "type" : "array", - "xml" : { - "name" : "photoUrl", - "wrapped" : true - }, - "items" : { - "type" : "string" - } - }, - "tags" : { - "type" : "array", - "xml" : { - "name" : "tag", - "wrapped" : true - }, - "items" : { - "$ref" : "#/definitions/Tag" - } - }, - "status" : { - "type" : "string", - "description" : "pet status in the store", - "enum" : [ "available", "pending", "sold" ] - } - }, - "xml" : { - "name" : "Pet" - } -},"Tag": { - "properties" : { - "id" : { - "type" : "integer", - "format" : "int64" - }, - "name" : { - "type" : "string" - } - }, - "xml" : { - "name" : "Tag" - } -},"Order": { - "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" : [ "placed", "approved", "delivered" ] - }, - "complete" : { - "type" : "boolean" - } - }, - "xml" : { - "name" : "Order" - } -} - } -} diff --git a/samples/server/petstore/nodejs/api/swagger.yaml b/samples/server/petstore/nodejs/api/swagger.yaml new file mode 100644 index 00000000000..49a3405de09 --- /dev/null +++ b/samples/server/petstore/nodejs/api/swagger.yaml @@ -0,0 +1,667 @@ +--- +swagger: "2.0" +info: + description: "This is a sample server Petstore server. You can find out more about\ + \ Swagger at http://swagger.io or on irc.freenode.net,\ + \ #swagger. For this sample, you can use the api key \"special-key\" to test\ + \ the authorization filters" + version: "1.0.0" + title: "Swagger Petstore" + termsOfService: "http://swagger.io/terms/" + contact: + email: "apiteam@swagger.io" + license: + name: "Apache 2.0" + url: "http://www.apache.org/licenses/LICENSE-2.0.html" +host: "petstore.swagger.io" +basePath: "/v2" +schemes: +- "http" +paths: + /pet: + post: + tags: + - "pet" + summary: "Add a new pet to the store" + description: "" + operationId: "addPet" + consumes: + - "application/json" + - "application/xml" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "Pet object that needs to be added to the store" + required: false + schema: + $ref: "#/definitions/Pet" + responses: + 405: + description: "Invalid input" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + put: + tags: + - "pet" + summary: "Update an existing pet" + description: "" + operationId: "updatePet" + consumes: + - "application/json" + - "application/xml" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "Pet object that needs to be added to the store" + required: false + schema: + $ref: "#/definitions/Pet" + responses: + 400: + description: "Invalid ID supplied" + 404: + description: "Pet not found" + 405: + description: "Validation exception" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + /pet/findByStatus: + get: + tags: + - "pet" + summary: "Finds Pets by status" + description: "Multiple status values can be provided with comma seperated strings" + operationId: "findPetsByStatus" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "status" + in: "query" + description: "Status values that need to be considered for filter" + required: false + type: "array" + items: + type: "string" + collectionFormat: "multi" + default: "available" + responses: + 200: + description: "successful operation" + schema: + type: "array" + items: + $ref: "#/definitions/Pet" + 400: + description: "Invalid status value" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + /pet/findByTags: + get: + tags: + - "pet" + summary: "Finds Pets by tags" + description: "Muliple tags can be provided with comma seperated strings. Use\ + \ tag1, tag2, tag3 for testing." + operationId: "findPetsByTags" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "tags" + in: "query" + description: "Tags to filter by" + required: false + type: "array" + items: + type: "string" + collectionFormat: "multi" + responses: + 200: + description: "successful operation" + schema: + type: "array" + items: + $ref: "#/definitions/Pet" + 400: + description: "Invalid tag value" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + /pet/{petId}: + get: + tags: + - "pet" + summary: "Find pet by ID" + description: "Returns a pet when ID < 10. ID > 10 or nonintegers will simulate\ + \ API error conditions" + operationId: "getPetById" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "petId" + in: "path" + description: "ID of pet that needs to be fetched" + required: true + type: "integer" + format: "int64" + responses: + 200: + description: "successful operation" + schema: + $ref: "#/definitions/Pet" + 400: + description: "Invalid ID supplied" + 404: + description: "Pet not found" + security: + - api_key: [] + - petstore_auth: + - "write:pets" + - "read:pets" + post: + tags: + - "pet" + summary: "Updates a pet in the store with form data" + description: "" + operationId: "updatePetWithForm" + consumes: + - "application/x-www-form-urlencoded" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "petId" + in: "path" + description: "ID of pet that needs to be updated" + required: true + type: "string" + - name: "name" + in: "formData" + description: "Updated name of the pet" + required: false + type: "string" + - name: "status" + in: "formData" + description: "Updated status of the pet" + required: false + type: "string" + responses: + 405: + description: "Invalid input" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + delete: + tags: + - "pet" + summary: "Deletes a pet" + description: "" + operationId: "deletePet" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "api_key" + in: "header" + description: "" + required: false + type: "string" + - name: "petId" + in: "path" + description: "Pet id to delete" + required: true + type: "integer" + format: "int64" + responses: + 400: + description: "Invalid pet value" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + /pet/{petId}/uploadImage: + post: + tags: + - "pet" + summary: "uploads an image" + description: "" + operationId: "uploadFile" + consumes: + - "multipart/form-data" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "petId" + in: "path" + description: "ID of pet to update" + required: true + type: "integer" + format: "int64" + - name: "additionalMetadata" + in: "formData" + description: "Additional data to pass to server" + required: false + type: "string" + - name: "file" + in: "formData" + description: "file to upload" + required: false + type: "file" + responses: + default: + description: "successful operation" + security: + - petstore_auth: + - "write:pets" + - "read:pets" + /store/inventory: + get: + tags: + - "store" + summary: "Returns pet inventories by status" + description: "Returns a map of status codes to quantities" + operationId: "getInventory" + produces: + - "application/json" + - "application/xml" + parameters: [] + responses: + 200: + description: "successful operation" + schema: + type: "object" + additionalProperties: + type: "integer" + format: "int32" + security: + - api_key: [] + /store/order: + post: + tags: + - "store" + summary: "Place an order for a pet" + description: "" + operationId: "placeOrder" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "order placed for purchasing the pet" + required: false + schema: + $ref: "#/definitions/Order" + responses: + 200: + description: "successful operation" + schema: + $ref: "#/definitions/Order" + 400: + description: "Invalid Order" + /store/order/{orderId}: + get: + tags: + - "store" + summary: "Find purchase order by ID" + description: "For valid response try integer IDs with value <= 5 or > 10. Other\ + \ values will generated exceptions" + operationId: "getOrderById" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "orderId" + in: "path" + description: "ID of pet that needs to be fetched" + required: true + type: "string" + responses: + 200: + description: "successful operation" + schema: + $ref: "#/definitions/Order" + 400: + description: "Invalid ID supplied" + 404: + description: "Order not found" + delete: + tags: + - "store" + summary: "Delete purchase order by ID" + description: "For valid response try integer IDs with value < 1000. Anything\ + \ above 1000 or nonintegers will generate API errors" + operationId: "deleteOrder" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "orderId" + in: "path" + description: "ID of the order that needs to be deleted" + required: true + type: "string" + responses: + 400: + description: "Invalid ID supplied" + 404: + description: "Order not found" + /user: + post: + tags: + - "user" + summary: "Create user" + description: "This can only be done by the logged in user." + operationId: "createUser" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "Created user object" + required: false + schema: + $ref: "#/definitions/User" + responses: + default: + description: "successful operation" + /user/createWithArray: + post: + tags: + - "user" + summary: "Creates list of users with given input array" + description: "" + operationId: "createUsersWithArrayInput" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "List of user object" + required: false + schema: + type: "array" + items: + $ref: "#/definitions/User" + responses: + default: + description: "successful operation" + /user/createWithList: + post: + tags: + - "user" + summary: "Creates list of users with given input array" + description: "" + operationId: "createUsersWithListInput" + produces: + - "application/json" + - "application/xml" + parameters: + - in: "body" + name: "body" + description: "List of user object" + required: false + schema: + type: "array" + items: + $ref: "#/definitions/User" + responses: + default: + description: "successful operation" + /user/login: + get: + tags: + - "user" + summary: "Logs user into the system" + description: "" + operationId: "loginUser" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "username" + in: "query" + description: "The user name for login" + required: false + type: "string" + - name: "password" + in: "query" + description: "The password for login in clear text" + required: false + type: "string" + responses: + 200: + description: "successful operation" + schema: + type: "string" + 400: + description: "Invalid username/password supplied" + /user/logout: + get: + tags: + - "user" + summary: "Logs out current logged in user session" + description: "" + operationId: "logoutUser" + produces: + - "application/json" + - "application/xml" + parameters: [] + responses: + default: + description: "successful operation" + /user/{username}: + get: + tags: + - "user" + summary: "Get user by user name" + description: "" + operationId: "getUserByName" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "username" + in: "path" + description: "The name that needs to be fetched. Use user1 for testing. " + required: true + type: "string" + responses: + 200: + description: "successful operation" + schema: + $ref: "#/definitions/User" + examples: + application/json: + id: 1 + username: "johnp" + firstName: "John" + lastName: "Public" + email: "johnp@swagger.io" + password: "-secret-" + phone: "0123456789" + userStatus: 0 + 400: + description: "Invalid username supplied" + 404: + description: "User not found" + put: + tags: + - "user" + summary: "Updated user" + description: "This can only be done by the logged in user." + operationId: "updateUser" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "username" + in: "path" + description: "name that need to be deleted" + required: true + type: "string" + - in: "body" + name: "body" + description: "Updated user object" + required: false + schema: + $ref: "#/definitions/User" + responses: + 400: + description: "Invalid user supplied" + 404: + description: "User not found" + delete: + tags: + - "user" + summary: "Delete user" + description: "This can only be done by the logged in user." + operationId: "deleteUser" + produces: + - "application/json" + - "application/xml" + parameters: + - name: "username" + in: "path" + description: "The name that needs to be deleted" + required: true + type: "string" + responses: + 400: + description: "Invalid username supplied" + 404: + description: "User not found" +securityDefinitions: + api_key: + type: "apiKey" + name: "api_key" + in: "header" + petstore_auth: + type: "oauth2" + authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog" + flow: "implicit" + scopes: + write:pets: "modify pets in your account" + read:pets: "read your pets" +definitions: + User: + properties: + id: + type: "integer" + format: "int64" + username: + type: "string" + firstName: + type: "string" + lastName: + type: "string" + email: + type: "string" + password: + type: "string" + phone: + type: "string" + userStatus: + type: "integer" + format: "int32" + description: "User Status" + xml: + name: "User" + Category: + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + xml: + name: "Category" + Pet: + required: + - "name" + - "photoUrls" + properties: + id: + type: "integer" + format: "int64" + category: + $ref: "#/definitions/Category" + name: + type: "string" + example: "doggie" + photoUrls: + type: "array" + xml: + name: "photoUrl" + wrapped: true + items: + type: "string" + tags: + type: "array" + xml: + name: "tag" + wrapped: true + items: + $ref: "#/definitions/Tag" + status: + type: "string" + description: "pet status in the store" + enum: + - "available" + - "pending" + - "sold" + xml: + name: "Pet" + Tag: + properties: + id: + type: "integer" + format: "int64" + name: + type: "string" + xml: + name: "Tag" + Order: + 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: + - "placed" + - "approved" + - "delivered" + complete: + type: "boolean" + xml: + name: "Order" diff --git a/samples/server/petstore/nodejs/controllers/Pet.js b/samples/server/petstore/nodejs/controllers/Pet.js index a268c8fa700..d272cb8240f 100644 --- a/samples/server/petstore/nodejs/controllers/Pet.js +++ b/samples/server/petstore/nodejs/controllers/Pet.js @@ -7,118 +7,33 @@ var Pet = require('./PetService'); module.exports.updatePet = function updatePet (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = Pet.updatePet(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.updatePet(req.swagger.params, res, next); }; module.exports.addPet = function addPet (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = Pet.addPet(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.addPet(req.swagger.params, res, next); }; module.exports.findPetsByStatus = function findPetsByStatus (req, res, next) { - var status = req.swagger.params['status'].value; - - - var result = Pet.findPetsByStatus(status); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.findPetsByStatus(req.swagger.params, res, next); }; module.exports.findPetsByTags = function findPetsByTags (req, res, next) { - var tags = req.swagger.params['tags'].value; - - - var result = Pet.findPetsByTags(tags); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.findPetsByTags(req.swagger.params, res, next); }; module.exports.getPetById = function getPetById (req, res, next) { - var petId = req.swagger.params['petId'].value; - - - var result = Pet.getPetById(petId); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.getPetById(req.swagger.params, res, next); }; module.exports.updatePetWithForm = function updatePetWithForm (req, res, next) { - var petId = req.swagger.params['petId'].value; - var name = req.swagger.params['name'].value; - var status = req.swagger.params['status'].value; - - - var result = Pet.updatePetWithForm(petId, name, status); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.updatePetWithForm(req.swagger.params, res, next); }; module.exports.deletePet = function deletePet (req, res, next) { - var petId = req.swagger.params['petId'].value; - var apiKey = req.swagger.params['api_key'].value; - - - var result = Pet.deletePet(petId, apiKey); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.deletePet(req.swagger.params, res, next); }; module.exports.uploadFile = function uploadFile (req, res, next) { - var petId = req.swagger.params['petId'].value; - var additionalMetadata = req.swagger.params['additionalMetadata'].value; - var file = req.swagger.params['file'].value; - - - var result = Pet.uploadFile(petId, additionalMetadata, file); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Pet.uploadFile(req.swagger.params, res, next); }; diff --git a/samples/server/petstore/nodejs/controllers/PetService.js b/samples/server/petstore/nodejs/controllers/PetService.js index f238fa410ac..97a1d3ede62 100644 --- a/samples/server/petstore/nodejs/controllers/PetService.js +++ b/samples/server/petstore/nodejs/controllers/PetService.js @@ -1,22 +1,36 @@ 'use strict'; -exports.updatePet = function(body) { +exports.updatePet = function(args, res, next) { + /** + * parameters expected in the args: + * body (Pet) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.addPet = function(body) { +exports.addPet = function(args, res, next) { + /** + * parameters expected in the args: + * body (Pet) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.findPetsByStatus = function(status) { +exports.findPetsByStatus = function(args, res, next) { + /** + * parameters expected in the args: + * status (List) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = [ { "tags" : [ { @@ -35,13 +49,23 @@ exports.findPetsByStatus = function(status) { - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.findPetsByTags = function(tags) { +exports.findPetsByTags = function(args, res, next) { + /** + * parameters expected in the args: + * tags (List) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = [ { "tags" : [ { @@ -60,13 +84,23 @@ exports.findPetsByTags = function(tags) { - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.getPetById = function(petId) { +exports.getPetById = function(args, res, next) { + /** + * parameters expected in the args: + * petId (Long) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = { "tags" : [ { @@ -85,28 +119,54 @@ exports.getPetById = function(petId) { - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.updatePetWithForm = function(petId, name, status) { +exports.updatePetWithForm = function(args, res, next) { + /** + * parameters expected in the args: + * petId (String) + * name (String) + * status (String) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.deletePet = function(petId, apiKey) { +exports.deletePet = function(args, res, next) { + /** + * parameters expected in the args: + * petId (Long) + * apiKey (String) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.uploadFile = function(petId, additionalMetadata, file) { +exports.uploadFile = function(args, res, next) { + /** + * parameters expected in the args: + * petId (Long) + * additionalMetadata (String) + * file (file) + **/ - var examples = {}; +var examples = {}; + res.end(); } diff --git a/samples/server/petstore/nodejs/controllers/Store.js b/samples/server/petstore/nodejs/controllers/Store.js index 7059f9afa61..8a07dc76ed3 100644 --- a/samples/server/petstore/nodejs/controllers/Store.js +++ b/samples/server/petstore/nodejs/controllers/Store.js @@ -7,56 +7,17 @@ var Store = require('./StoreService'); module.exports.getInventory = function getInventory (req, res, next) { - - - var result = Store.getInventory(); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Store.getInventory(req.swagger.params, res, next); }; module.exports.placeOrder = function placeOrder (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = Store.placeOrder(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Store.placeOrder(req.swagger.params, res, next); }; module.exports.getOrderById = function getOrderById (req, res, next) { - var orderId = req.swagger.params['orderId'].value; - - - var result = Store.getOrderById(orderId); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Store.getOrderById(req.swagger.params, res, next); }; module.exports.deleteOrder = function deleteOrder (req, res, next) { - var orderId = req.swagger.params['orderId'].value; - - - var result = Store.deleteOrder(orderId); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + Store.deleteOrder(req.swagger.params, res, next); }; diff --git a/samples/server/petstore/nodejs/controllers/StoreService.js b/samples/server/petstore/nodejs/controllers/StoreService.js index e00a6aee9b9..3bb37b6c01f 100644 --- a/samples/server/petstore/nodejs/controllers/StoreService.js +++ b/samples/server/petstore/nodejs/controllers/StoreService.js @@ -1,8 +1,11 @@ 'use strict'; -exports.getInventory = function() { +exports.getInventory = function(args, res, next) { + /** + * parameters expected in the args: + **/ - var examples = {}; +var examples = {}; examples['application/json'] = { "key" : 123 @@ -10,13 +13,23 @@ exports.getInventory = function() { - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.placeOrder = function(body) { +exports.placeOrder = function(args, res, next) { + /** + * parameters expected in the args: + * body (Order) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = { "id" : 123456789, @@ -24,18 +37,28 @@ exports.placeOrder = function(body) { "complete" : true, "status" : "aeiou", "quantity" : 123, - "shipDate" : "2015-10-20T06:12:23.907+0000" + "shipDate" : "2015-11-18T02:43:54.540+0000" }; - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.getOrderById = function(orderId) { +exports.getOrderById = function(args, res, next) { + /** + * parameters expected in the args: + * orderId (String) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = { "id" : 123456789, @@ -43,19 +66,30 @@ exports.getOrderById = function(orderId) { "complete" : true, "status" : "aeiou", "quantity" : 123, - "shipDate" : "2015-10-20T06:12:23.911+0000" + "shipDate" : "2015-11-18T02:43:54.544+0000" }; - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.deleteOrder = function(orderId) { +exports.deleteOrder = function(args, res, next) { + /** + * parameters expected in the args: + * orderId (String) + **/ - var examples = {}; +var examples = {}; + res.end(); } diff --git a/samples/server/petstore/nodejs/controllers/User.js b/samples/server/petstore/nodejs/controllers/User.js index d89f34c9c3f..d2ce361da52 100644 --- a/samples/server/petstore/nodejs/controllers/User.js +++ b/samples/server/petstore/nodejs/controllers/User.js @@ -7,114 +7,33 @@ var User = require('./UserService'); module.exports.createUser = function createUser (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = User.createUser(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.createUser(req.swagger.params, res, next); }; module.exports.createUsersWithArrayInput = function createUsersWithArrayInput (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = User.createUsersWithArrayInput(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.createUsersWithArrayInput(req.swagger.params, res, next); }; module.exports.createUsersWithListInput = function createUsersWithListInput (req, res, next) { - var body = req.swagger.params['body'].value; - - - var result = User.createUsersWithListInput(body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.createUsersWithListInput(req.swagger.params, res, next); }; module.exports.loginUser = function loginUser (req, res, next) { - var username = req.swagger.params['username'].value; - var password = req.swagger.params['password'].value; - - - var result = User.loginUser(username, password); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.loginUser(req.swagger.params, res, next); }; module.exports.logoutUser = function logoutUser (req, res, next) { - - - var result = User.logoutUser(); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.logoutUser(req.swagger.params, res, next); }; module.exports.getUserByName = function getUserByName (req, res, next) { - var username = req.swagger.params['username'].value; - - - var result = User.getUserByName(username); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.getUserByName(req.swagger.params, res, next); }; module.exports.updateUser = function updateUser (req, res, next) { - var username = req.swagger.params['username'].value; - var body = req.swagger.params['body'].value; - - - var result = User.updateUser(username, body); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.updateUser(req.swagger.params, res, next); }; module.exports.deleteUser = function deleteUser (req, res, next) { - var username = req.swagger.params['username'].value; - - - var result = User.deleteUser(username); - - if(typeof result !== 'undefined') { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result || {}, null, 2)); - } - else - res.end(); + User.deleteUser(req.swagger.params, res, next); }; diff --git a/samples/server/petstore/nodejs/controllers/UserService.js b/samples/server/petstore/nodejs/controllers/UserService.js index 30aab42ada9..71f34f35bfb 100644 --- a/samples/server/petstore/nodejs/controllers/UserService.js +++ b/samples/server/petstore/nodejs/controllers/UserService.js @@ -1,48 +1,82 @@ 'use strict'; -exports.createUser = function(body) { +exports.createUser = function(args, res, next) { + /** + * parameters expected in the args: + * body (User) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.createUsersWithArrayInput = function(body) { +exports.createUsersWithArrayInput = function(args, res, next) { + /** + * parameters expected in the args: + * body (List) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.createUsersWithListInput = function(body) { +exports.createUsersWithListInput = function(args, res, next) { + /** + * parameters expected in the args: + * body (List) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.loginUser = function(username, password) { +exports.loginUser = function(args, res, next) { + /** + * parameters expected in the args: + * username (String) + * password (String) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = "aeiou"; - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.logoutUser = function() { +exports.logoutUser = function(args, res, next) { + /** + * parameters expected in the args: + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.getUserByName = function(username) { +exports.getUserByName = function(args, res, next) { + /** + * parameters expected in the args: + * username (String) + **/ - var examples = {}; +var examples = {}; examples['application/json'] = { "id" : 1, @@ -57,21 +91,38 @@ exports.getUserByName = function(username) { - if(Object.keys(examples).length > 0) - return examples[Object.keys(examples)[0]]; + if(Object.keys(examples).length > 0) { + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); + } + else { + res.end(); + } + } -exports.updateUser = function(username, body) { +exports.updateUser = function(args, res, next) { + /** + * parameters expected in the args: + * username (String) + * body (User) + **/ - var examples = {}; +var examples = {}; + res.end(); } -exports.deleteUser = function(username) { +exports.deleteUser = function(args, res, next) { + /** + * parameters expected in the args: + * username (String) + **/ - var examples = {}; +var examples = {}; + res.end(); } diff --git a/samples/server/petstore/nodejs/index.js b/samples/server/petstore/nodejs/index.js index c922cc7c1e7..aaa873a128a 100644 --- a/samples/server/petstore/nodejs/index.js +++ b/samples/server/petstore/nodejs/index.js @@ -3,7 +3,8 @@ var app = require('connect')(); var http = require('http'); var swaggerTools = require('swagger-tools'); - +var jsyaml = require('js-yaml'); +var fs = require('fs'); var serverPort = 8080; // swaggerRouter configuration @@ -14,7 +15,8 @@ var options = { }; // The Swagger document (require it, build it programmatically, fetch it from a URL, ...) -var swaggerDoc = require('./api/swagger.json'); +var spec = fs.readFileSync('./api/swagger.yaml', 'utf8'); +var swaggerDoc = jsyaml.safeLoad(spec); // Initialize the Swagger middleware swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) { diff --git a/samples/server/petstore/nodejs/package.json b/samples/server/petstore/nodejs/package.json index 35390feefda..8befd2669d2 100644 --- a/samples/server/petstore/nodejs/package.json +++ b/samples/server/petstore/nodejs/package.json @@ -10,6 +10,7 @@ "private": true, "dependencies": { "connect": "^3.2.0", - "swagger-tools": "0.8.*" + "js-yaml": "^3.3.0", + "swagger-tools": "0.9.*" } }