diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index 3da5ff00aa1..21734396695 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -62,10 +62,13 @@ var {{classname}} = function {{classname}}() { var options = {type: "{{httpMethod}}", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ {{#returnType}} @@ -84,6 +87,8 @@ var {{classname}} = function {{classname}}() { } {{/returnType}} }); + + return request; } {{/operation}} {{/operations}} diff --git a/pom.xml b/pom.xml index 267de93280e..a577173927e 100644 --- a/pom.xml +++ b/pom.xml @@ -389,6 +389,18 @@ samples/client/petstore/java/feign + + javascript-client + + + env + javascript + + + + samples/client/petstore/javascript + + scala-client @@ -465,6 +477,7 @@ samples/client/petstore/java/okhttp-gson samples/client/petstore/java/retrofit samples/client/petstore/java/retrofit2 + samples/client/petstore/javascript samples/client/petstore/scala samples/server/petstore/spring-mvc diff --git a/samples/client/petstore/javascript/pom.xml b/samples/client/petstore/javascript/pom.xml new file mode 100644 index 00000000000..82d13ff17ca --- /dev/null +++ b/samples/client/petstore/javascript/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + io.swagger + swagger-petstore-javascript + pom + 1.0-SNAPSHOT + Swagger Petstore - Javascript Client + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + npm-install + pre-integration-test + + exec + + + npm + + install + + + + + mocha + integration-test + + exec + + + mocha + + --recursive + + + + + + + + diff --git a/samples/client/petstore/javascript/src/api/PetApi.js b/samples/client/petstore/javascript/src/api/PetApi.js index 4f77c603b23..fcc769d2f26 100644 --- a/samples/client/petstore/javascript/src/api/PetApi.js +++ b/samples/client/petstore/javascript/src/api/PetApi.js @@ -48,10 +48,13 @@ var PetApi = function PetApi() { var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -60,6 +63,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -94,10 +99,13 @@ var PetApi = function PetApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -106,6 +114,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -142,10 +152,13 @@ var PetApi = function PetApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -160,6 +173,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -196,10 +211,13 @@ var PetApi = function PetApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -214,6 +232,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -256,10 +276,13 @@ var PetApi = function PetApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -274,6 +297,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -322,10 +347,13 @@ var PetApi = function PetApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -334,6 +362,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -379,10 +409,13 @@ var PetApi = function PetApi() { var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -391,6 +424,8 @@ var PetApi = function PetApi() { } }); + + return request; } /** @@ -439,10 +474,13 @@ var PetApi = function PetApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -451,6 +489,8 @@ var PetApi = function PetApi() { } }); + + return request; } diff --git a/samples/client/petstore/javascript/src/api/StoreApi.js b/samples/client/petstore/javascript/src/api/StoreApi.js index 6cfa53744ba..a05bf10f179 100644 --- a/samples/client/petstore/javascript/src/api/StoreApi.js +++ b/samples/client/petstore/javascript/src/api/StoreApi.js @@ -47,10 +47,13 @@ var StoreApi = function StoreApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -64,6 +67,8 @@ var StoreApi = function StoreApi() { } }); + + return request; } /** @@ -98,10 +103,13 @@ var StoreApi = function StoreApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -116,6 +124,8 @@ var StoreApi = function StoreApi() { } }); + + return request; } /** @@ -158,10 +168,13 @@ var StoreApi = function StoreApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -176,6 +189,8 @@ var StoreApi = function StoreApi() { } }); + + return request; } /** @@ -218,10 +233,13 @@ var StoreApi = function StoreApi() { var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -230,6 +248,8 @@ var StoreApi = function StoreApi() { } }); + + return request; } diff --git a/samples/client/petstore/javascript/src/api/UserApi.js b/samples/client/petstore/javascript/src/api/UserApi.js index bfa1b1c16c8..32dcafbfc7a 100644 --- a/samples/client/petstore/javascript/src/api/UserApi.js +++ b/samples/client/petstore/javascript/src/api/UserApi.js @@ -48,10 +48,13 @@ var UserApi = function UserApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -60,6 +63,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -94,10 +99,13 @@ var UserApi = function UserApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -106,6 +114,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -140,10 +150,13 @@ var UserApi = function UserApi() { var options = {type: "POST", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -152,6 +165,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -191,10 +206,13 @@ var UserApi = function UserApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -208,6 +226,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -241,10 +261,13 @@ var UserApi = function UserApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -253,6 +276,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -295,10 +320,13 @@ var UserApi = function UserApi() { var options = {type: "GET", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -313,6 +341,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -356,10 +386,13 @@ var UserApi = function UserApi() { var options = {type: "PUT", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -368,6 +401,8 @@ var UserApi = function UserApi() { } }); + + return request; } /** @@ -410,10 +445,13 @@ var UserApi = function UserApi() { var options = {type: "DELETE", async: true, contentType: "application/json", dataType: "json", data: postBody}; var request = $.ajax(path, options); - //request.fail(function(jqXHR, textStatus, errorThrown){ - // errorHandler(jqXHR, textStatus, errorThrown); - //}); - + + request.fail(function(jqXHR, textStatus, errorThrown){ + if (callback) { + var error = errorThrown || textStatus || jqXHR.statusText || 'error'; + callback(null, textStatus, jqXHR, error); + } + }); request.done(function(response, textStatus, jqXHR){ @@ -422,6 +460,8 @@ var UserApi = function UserApi() { } }); + + return request; } diff --git a/samples/client/petstore/javascript/test/api/PetApiTest.js b/samples/client/petstore/javascript/test/api/PetApiTest.js index 122dde3982f..325cc16b616 100644 --- a/samples/client/petstore/javascript/test/api/PetApiTest.js +++ b/samples/client/petstore/javascript/test/api/PetApiTest.js @@ -33,15 +33,21 @@ var createRandomPet = function() { describe('PetApi', function() { it('should create and get pet', function (done) { var pet = createRandomPet(); - api.addPet(pet); + api.addPet(pet).then(function() { + api.getPetById(pet.id, function(fetched, textStatus, jqXHR, error) { + if (error) throw error; - api.getPetById(pet.id, function(fetched, textStatus, jqXHR) { - expect(textStatus).to.be('success'); - expect(fetched).to.be.ok(); - expect(fetched.id).to.be(pet.id); - expect(fetched.getCategory()).to.be.ok(); - expect(fetched.getCategory().getName()).to.be(pet.getCategory().getName()); - done(); + expect(textStatus).to.be('success'); + expect(fetched).to.be.ok(); + expect(fetched.id).to.be(pet.id); + expect(fetched.getCategory()).to.be.ok(); + expect(fetched.getCategory().getName()).to.be(pet.getCategory().getName()); + + api.deletePet(pet.id); + done(); + }); + }, function(jqXHR, textStatus, errorThrown) { + throw errorThrown || textStatus; }); }); }); diff --git a/samples/client/petstore/javascript/test/api/StoreApiTest.java b/samples/client/petstore/javascript/test/api/StoreApiTest.java new file mode 100644 index 00000000000..6094f2242e1 --- /dev/null +++ b/samples/client/petstore/javascript/test/api/StoreApiTest.java @@ -0,0 +1,75 @@ +package io.swagger.petstore.test; + +import io.swagger.client.ApiException; + +import io.swagger.client.*; +import io.swagger.client.api.*; +import io.swagger.client.auth.*; +import io.swagger.client.model.*; + +import java.util.Map; +import java.text.SimpleDateFormat; + +import org.junit.*; +import static org.junit.Assert.*; + +public class StoreApiTest { + StoreApi api = null; + + @Before + public void setup() { + api = new StoreApi(); + // setup authentication + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); + apiKeyAuth.setApiKey("special-key"); + // set custom date format that is used by the petstore server + api.getApiClient().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); + } + + @Test + public void testGetInventory() throws Exception { + Map inventory = api.getInventory(); + assertTrue(inventory.keySet().size() > 0); + } + + @Test + public void testPlaceOrder() throws Exception { + Order order = createOrder(); + api.placeOrder(order); + + Order fetched = api.getOrderById(String.valueOf(order.getId())); + assertEquals(order.getId(), fetched.getId()); + assertEquals(order.getPetId(), fetched.getPetId()); + assertEquals(order.getQuantity(), fetched.getQuantity()); + } + + @Test + public void testDeleteOrder() throws Exception { + Order order = createOrder(); + api.placeOrder(order); + + Order fetched = api.getOrderById(String.valueOf(order.getId())); + assertEquals(fetched.getId(), order.getId()); + + api.deleteOrder(String.valueOf(order.getId())); + + try { + api.getOrderById(String.valueOf(order.getId())); + // fail("expected an error"); + } catch (ApiException e) { + // ok + } + } + + private Order createOrder() { + Order order = new Order(); + order.setId(new Long(System.currentTimeMillis())); + order.setPetId(new Long(200)); + order.setQuantity(new Integer(13)); + order.setShipDate(new java.util.Date()); + order.setStatus(Order.StatusEnum.PLACED); + order.setComplete(true); + + return order; + } +} diff --git a/samples/client/petstore/javascript/test/api/UserApiTest.java b/samples/client/petstore/javascript/test/api/UserApiTest.java new file mode 100644 index 00000000000..a3f257f568b --- /dev/null +++ b/samples/client/petstore/javascript/test/api/UserApiTest.java @@ -0,0 +1,86 @@ +package io.swagger.petstore.test; + +import io.swagger.client.api.*; +import io.swagger.client.auth.*; +import io.swagger.client.model.*; + +import java.util.Arrays; + +import org.junit.*; +import static org.junit.Assert.*; + +public class UserApiTest { + UserApi api = null; + + @Before + public void setup() { + api = new UserApi(); + // setup authentication + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); + apiKeyAuth.setApiKey("special-key"); + } + + @Test + public void testCreateUser() throws Exception { + User user = createUser(); + + api.createUser(user); + + User fetched = api.getUserByName(user.getUsername()); + assertEquals(user.getId(), fetched.getId()); + } + + @Test + public void testCreateUsersWithArray() throws Exception { + User user1 = createUser(); + user1.setUsername("abc123"); + User user2 = createUser(); + user2.setUsername("123abc"); + + api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})); + + User fetched = api.getUserByName(user1.getUsername()); + assertEquals(user1.getId(), fetched.getId()); + } + + @Test + public void testCreateUsersWithList() throws Exception { + User user1 = createUser(); + user1.setUsername("abc123"); + User user2 = createUser(); + user2.setUsername("123abc"); + + api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})); + + User fetched = api.getUserByName(user1.getUsername()); + assertEquals(user1.getId(), fetched.getId()); + } + + @Test + public void testLoginUser() throws Exception { + User user = createUser(); + api.createUser(user); + + String token = api.loginUser(user.getUsername(), user.getPassword()); + assertTrue(token.startsWith("logged in user session:")); + } + + @Test + public void logoutUser() throws Exception { + api.logoutUser(); + } + + private User createUser() { + User user = new User(); + user.setId(System.currentTimeMillis()); + user.setUsername("fred" + user.getId()); + user.setFirstName("Fred"); + user.setLastName("Meyer"); + user.setEmail("fred@fredmeyer.com"); + user.setPassword("xxXXxx"); + user.setPhone("408-867-5309"); + user.setUserStatus(123); + + return user; + } +} diff --git a/samples/client/petstore/javascript/test/mocha.opts b/samples/client/petstore/javascript/test/mocha.opts index cf80ee74bca..907011807d6 100644 --- a/samples/client/petstore/javascript/test/mocha.opts +++ b/samples/client/petstore/javascript/test/mocha.opts @@ -1 +1 @@ ---timeout 5000 +--timeout 10000 diff --git a/samples/client/petstore/javascript/test/run_tests.html b/samples/client/petstore/javascript/test/run_tests.html index d4b9e6c9f65..c5655275cc5 100644 --- a/samples/client/petstore/javascript/test/run_tests.html +++ b/samples/client/petstore/javascript/test/run_tests.html @@ -14,7 +14,7 @@