diff --git a/pom.xml b/pom.xml index 642f353..6bb2df1 100644 --- a/pom.xml +++ b/pom.xml @@ -125,12 +125,12 @@ com.github.kongchen swagger-maven-plugin - 2.3.2 + 3.1.3 - com.wordnik + io.swagger swagger-annotations - 1.3.10 + 1.5.7 org.reflections diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSource.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSource.java index 8dafb3f..7cddd57 100644 --- a/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSource.java +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSource.java @@ -12,8 +12,10 @@ public AntApiSource(Project project) { } @SuppressWarnings("unused") - public AntApiSourceInfo createApiInfo() { - return new AntApiSourceInfo(project); + public AntInfo createInfo() { + AntInfo antInfo = new AntInfo(project); + setInfo(antInfo); + return antInfo; } @SuppressWarnings("unused") @@ -71,11 +73,36 @@ public WriteBack createUseOutputFlatStructure() { return new WriteBack(project, this, "useOutputFlatStructure"); } + @SuppressWarnings("unused") + public WriteBack createSpringmvc() { + return new WriteBack(project, this, "springmvc"); + } + + @SuppressWarnings("unused") + public WriteBack createSchemes() { + return new WriteBack(project, this, "schemes"); + } + + @SuppressWarnings("unused") + public WriteBack createHost() { + return new WriteBack(project, this, "host"); + } + + @SuppressWarnings("unused") + public WriteBack createTemplatePath() { + return new WriteBack(project, this, "templatePath"); + } + + @SuppressWarnings("unused") + public AntSecurityDefinitions createSecurityDefinitions() { + AntSecurityDefinitions antSecurityDefinitions = new AntSecurityDefinitions(project); + setSecurityDefinitions(antSecurityDefinitions); + return antSecurityDefinitions; + } + @Override public String toString() { return "locations = " + getLocations() + "\n" + - "apiInfo = " + getApiInfo() + "\n" + - "apiVersion = " + getApiVersion() + "\n" + - "basePath = " + getBasePath(); + "basePath = " + getBasePath(); } } \ No newline at end of file diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSourceInfo.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSourceInfo.java deleted file mode 100644 index e1e9e66..0000000 --- a/src/main/java/com/github/cstroe/swagger/docgen/task/AntApiSourceInfo.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.github.cstroe.swagger.docgen.task; - -import com.github.kongchen.swagger.docgen.mavenplugin.ApiSourceInfo; -import org.apache.tools.ant.Project; - -public class AntApiSourceInfo extends ApiSourceInfo { - - private final Project project; - - public AntApiSourceInfo(Project project) { - this.project = project; - } - - @SuppressWarnings("unused") - public WriteBack createTitle() { - return new WriteBack(project, this, "title"); - } - - @SuppressWarnings("unused") - public WriteBack createDescription() { - return new WriteBack(project, this, "description"); - } - - @SuppressWarnings("unused") - public WriteBack createTermsOfServiceUrl() { - return new WriteBack(project, this, "termsOfServiceUrl"); - } - - @SuppressWarnings("unused") - public WriteBack createContact() { - return new WriteBack(project, this, "contact"); - } - - @SuppressWarnings("unused") - public WriteBack createLicense() { - return new WriteBack(project, this, "license"); - } - - @SuppressWarnings("unused") - public WriteBack createLicenseUrl() { - return new WriteBack(project, this, "licenceUrl"); - } -} diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfo.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfo.java new file mode 100644 index 0000000..d6d69af --- /dev/null +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfo.java @@ -0,0 +1,48 @@ +package com.github.cstroe.swagger.docgen.task; + +import io.swagger.models.Info; +import org.apache.tools.ant.Project; + +public class AntInfo extends Info { + + private final Project project; + + public AntInfo(Project project) { + this.project = project; + } + + @SuppressWarnings("unused") + public WriteBack createTitle() { + return new WriteBack(project, this, "title"); + } + + @SuppressWarnings("unused") + public WriteBack createVersion() { + return new WriteBack(project, this, "version"); + } + + @SuppressWarnings("unused") + public WriteBack createDescription() { + return new WriteBack(project, this, "description"); + } + + @SuppressWarnings("unused") + public WriteBack createTermsOfService() { + return new WriteBack(project, this, "termsOfService"); + } + + @SuppressWarnings("unused") + public AntInfoContact createContact() { + AntInfoContact antInfoContact = new AntInfoContact(project); + setContact(antInfoContact); + return antInfoContact; + } + + @SuppressWarnings("unused") + public AntInfoLicense createLicense() { + AntInfoLicense antInfoLicense = new AntInfoLicense(project); + setLicense(antInfoLicense); + return antInfoLicense; + } + +} diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoContact.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoContact.java new file mode 100644 index 0000000..3664d72 --- /dev/null +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoContact.java @@ -0,0 +1,28 @@ +package com.github.cstroe.swagger.docgen.task; + +import io.swagger.models.Contact; +import org.apache.tools.ant.Project; + +public class AntInfoContact extends Contact { + + private final Project project; + + public AntInfoContact(Project project) { + this.project = project; + } + + @SuppressWarnings("unused") + public WriteBack createName() { + return new WriteBack(project, this, "name"); + } + + @SuppressWarnings("unused") + public WriteBack createUrl() { + return new WriteBack(project, this, "url"); + } + + @SuppressWarnings("unused") + public WriteBack createEmail() { + return new WriteBack(project, this, "email"); + } +} diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoLicense.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoLicense.java new file mode 100644 index 0000000..ee1e8e9 --- /dev/null +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntInfoLicense.java @@ -0,0 +1,23 @@ +package com.github.cstroe.swagger.docgen.task; + +import io.swagger.models.License; +import org.apache.tools.ant.Project; + +public class AntInfoLicense extends License { + + private final Project project; + + public AntInfoLicense(Project project) { + this.project = project; + } + + @SuppressWarnings("unused") + public WriteBack createUrl() { + return new WriteBack(project, this, "url"); + } + + @SuppressWarnings("unused") + public WriteBack createName() { + return new WriteBack(project, this, "name"); + } +} diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinition.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinition.java new file mode 100644 index 0000000..56190e2 --- /dev/null +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinition.java @@ -0,0 +1,40 @@ +package com.github.cstroe.swagger.docgen.task; + +import com.github.kongchen.swagger.docgen.mavenplugin.SecurityDefinition; +import org.apache.tools.ant.Project; + +/** + * AntSecurityDefinition + *

+ * + * @author $Author$ + * @version $Revision$ + *

Copyright (c) 2003 - 2016 CGI IT Czech Republic s.r.o.

+ */ +public class AntSecurityDefinition extends SecurityDefinition { + private final Project project; + + public AntSecurityDefinition(Project project) { + this.project = project; + } + + @SuppressWarnings("unused") + public WriteBack createName() { + return new WriteBack(project, this, "name"); + } + + @SuppressWarnings("unused") + public WriteBack createType() { + return new WriteBack(project, this, "type"); + } + + @SuppressWarnings("unused") + public WriteBack createDescription() { + return new WriteBack(project, this, "description"); + } + + @SuppressWarnings("unused") + public WriteBack createJson() { + return new WriteBack(project, this, "json"); + } +} diff --git a/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinitions.java b/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinitions.java new file mode 100644 index 0000000..a165ebe --- /dev/null +++ b/src/main/java/com/github/cstroe/swagger/docgen/task/AntSecurityDefinitions.java @@ -0,0 +1,29 @@ +package com.github.cstroe.swagger.docgen.task; + +import com.github.kongchen.swagger.docgen.mavenplugin.SecurityDefinition; +import org.apache.tools.ant.Project; + +import java.util.ArrayList; + +/** + * AntSecurityDefinitions + *

+ * + * @author $Author$ + * @version $Revision$ + *

Copyright (c) 2003 - 2016 CGI IT Czech Republic s.r.o.

+ */ +public class AntSecurityDefinitions extends ArrayList { + private final Project project; + + public AntSecurityDefinitions(Project project) { + this.project = project; + } + + @SuppressWarnings("unused") + public AntSecurityDefinition createSecurityDefinition() { + AntSecurityDefinition antSecurityDefinition = new AntSecurityDefinition(project); + add(antSecurityDefinition); + return antSecurityDefinition; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/PetData.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/PetData.java new file mode 100644 index 0000000..d23953d --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/PetData.java @@ -0,0 +1,166 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.data; + +import com.github.kongchen.swagger.sample.wordnik.model.Category; +import com.github.kongchen.swagger.sample.wordnik.model.Pet; +import com.github.kongchen.swagger.sample.wordnik.model.Tag; + +import java.util.List; +import java.util.ArrayList; + +public class PetData { + static List pets = new ArrayList(); + static List categories = new ArrayList(); + + static { + categories.add(createCategory(1, "Dogs")); + categories.add(createCategory(2, "Cats")); + categories.add(createCategory(3, "Rabbits")); + categories.add(createCategory(4, "Lions")); + + pets.add(createPet(1, categories.get(1), "Cat 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(2, categories.get(1), "Cat 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "available")); + pets.add(createPet(3, categories.get(1), "Cat 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "pending")); + + pets.add(createPet(4, categories.get(0), "Dog 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(5, categories.get(0), "Dog 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "sold")); + pets.add(createPet(6, categories.get(0), "Dog 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "pending")); + + pets.add(createPet(7, categories.get(3), "Lion 1", new String[] { + "url1", "url2" }, new String[] { "tag1", "tag2" }, "available")); + pets.add(createPet(8, categories.get(3), "Lion 2", new String[] { + "url1", "url2" }, new String[] { "tag2", "tag3" }, "available")); + pets.add(createPet(9, categories.get(3), "Lion 3", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "available")); + + pets.add(createPet(10, categories.get(2), "Rabbit 1", new String[] { + "url1", "url2" }, new String[] { "tag3", "tag4" }, "available")); + } + + public Pet getPetbyId(long petId) { + for (Pet pet : pets) { + if (pet.getId() == petId) { + return pet; + } + } + return null; + } + + public void deletePet(long petId) { + if(pets.size() > 0) { + for (int i = pets.size(); i >= 0; i++) { + Pet pet = pets.get(i); + if(pet.getId() == petId) { + pets.remove(i); + } + } + } + } + + public List findPetByStatus(String status) { + String[] statues = status.split(","); + List result = new java.util.ArrayList(); + for (Pet pet : pets) { + for (String s : statues) { + if (s.equals(pet.getStatus())) { + result.add(pet); + } + } + } + return result; + } + + public List findPetByTags(String tags) { + String[] tagList = tags.split(","); + List result = new java.util.ArrayList(); + for (Pet pet : pets) { + if (null != pet.getTags()) { + for (Tag tag : pet.getTags()) { + for (String tagListString : tagList) { + if (tagListString.equals(tag.getName())) + result.add(pet); + } + } + } + } + return result; + } + + public Pet addPet(Pet pet) { + if(pet.getId() == 0) { + long maxId = 0; + for (int i = pets.size() - 1; i >= 0; i--) { + if(pets.get(i).getId() > maxId) { + maxId = pets.get(i).getId(); + } + } + pet.setId(maxId + 1); + } + if (pets.size() > 0) { + for (int i = pets.size() - 1; i >= 0; i--) { + if (pets.get(i).getId() == pet.getId()) { + pets.remove(i); + } + } + } + pets.add(pet); + return pet; + } + + static Pet createPet(long id, Category cat, String name, String[] urls, + String[] tags, String status) { + Pet pet = new Pet(); + pet.setId(id); + pet.setCategory(cat); + pet.setName(name); + if (null != urls) { + List urlObjs = new ArrayList(); + for (String urlString : urls) { + urlObjs.add(urlString); + } + pet.setPhotoUrls(urlObjs); + } + List tagObjs = new java.util.ArrayList(); + int i = 0; + if (null != tags) { + for (String tagString : tags) { + i = i + 1; + Tag tag = new Tag(); + tag.setId(i); + tag.setName(tagString); + tagObjs.add(tag); + } + } + pet.setTags(tagObjs); + pet.setStatus(status); + return pet; + } + + static Category createCategory(long id, String name) { + Category category = new Category(); + category.setId(id); + category.setName(name); + return category; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/StoreData.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/StoreData.java new file mode 100644 index 0000000..a1c89a4 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/StoreData.java @@ -0,0 +1,82 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.data; + +import com.github.kongchen.swagger.sample.wordnik.model.Order; + +import java.util.Date; +import java.util.List; +import java.util.ArrayList; + +public class StoreData { + static List orders = new ArrayList(); + + static { + orders.add(createOrder(1, 1, 2, new Date(), "placed")); + orders.add(createOrder(2, 1, 2, new Date(), "delivered")); + orders.add(createOrder(3, 2, 2, new Date(), "placed")); + orders.add(createOrder(4, 2, 2, new Date(), "delivered")); + orders.add(createOrder(5, 3, 2, new Date(), "placed")); + orders.add(createOrder(11, 3, 2, new Date(), "placed")); + orders.add(createOrder(12, 3, 2, new Date(), "placed")); + orders.add(createOrder(13, 3, 2, new Date(), "placed")); + orders.add(createOrder(14, 3, 2, new Date(), "placed")); + orders.add(createOrder(15, 3, 2, new Date(), "placed")); + } + + public Order findOrderById(long orderId) { + for (Order order : orders) { + if (order.getId() == orderId) { + return order; + } + } + return null; + } + + public Order placeOrder(Order order) { + if (orders.size() > 0) { + for (int i = orders.size() - 1; i >= 0; i--) { + if (orders.get(i).getId() == order.getId()) { + orders.remove(i); + } + } + } + orders.add(order); + return order; + } + + public void deleteOrder(long orderId) { + if (orders.size() > 0) { + for (int i = orders.size() - 1; i >= 0; i--) { + if (orders.get(i).getId() == orderId) { + orders.remove(i); + } + } + } + } + + private static Order createOrder(long id, long petId, int quantity, + Date shipDate, String status) { + Order order = new Order(); + order.setId(id); + order.setPetId(petId); + order.setQuantity(quantity); + order.setShipDate(shipDate); + order.setStatus(status); + return order; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/UserData.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/UserData.java new file mode 100644 index 0000000..f1925ac --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/data/UserData.java @@ -0,0 +1,96 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.data; + +import com.github.kongchen.swagger.sample.wordnik.model.User; + +import java.util.List; +import java.util.ArrayList; + +public class UserData { + static List users = new ArrayList(); + + static { + users.add(createUser(1, "user1", "first name 1", "last name 1", + "email1@test.com", "123-456-7890", 1)); + users.add(createUser(2, "user2", "first name 2", "last name 2", + "email2@test.com", "123-456-7890", 2)); + users.add(createUser(3, "user3", "first name 3", "last name 3", + "email3@test.com", "123-456-7890", 3)); + users.add(createUser(4, "user4", "first name 4", "last name 4", + "email4@test.com", "123-456-7890", 1)); + users.add(createUser(5, "user5", "first name 5", "last name 5", + "email5@test.com", "123-456-7890", 2)); + users.add(createUser(6, "user6", "first name 6", "last name 6", + "email6@test.com", "123-456-7890", 3)); + users.add(createUser(7, "user7", "first name 7", "last name 7", + "email7@test.com", "123-456-7890", 1)); + users.add(createUser(8, "user8", "first name 8", "last name 8", + "email8@test.com", "123-456-7890", 2)); + users.add(createUser(9, "user9", "first name 9", "last name 9", + "email9@test.com", "123-456-7890", 3)); + users.add(createUser(10, "user10", "first name 10", "last name 10", + "email10@test.com", "123-456-7890", 1)); + users.add(createUser(11, "user?10", "first name ?10", "last name ?10", + "email101@test.com", "123-456-7890", 1)); + + } + + public User findUserByName(String username) { + for (User user : users) { + if (user.getUsername().equals(username)) { + return user; + } + } + return null; + } + + public void addUser(User user) { + if (users.size() > 0) { + for (int i = users.size() - 1; i >= 0; i--) { + if (users.get(i).getUsername().equals(user.getUsername())) { + users.remove(i); + } + } + } + users.add(user); + } + + public void removeUser(String username) { + if (users.size() > 0) { + for (int i = users.size() - 1; i >= 0; i--) { + if (users.get(i).getUsername().equals(username)) { + users.remove(i); + } + } + } + } + + private static User createUser(long id, String username, String firstName, + String lastName, String email, String phone, int userStatus) { + User user = new User(); + user.setId(id); + user.setUsername(username); + user.setFirstName(firstName); + user.setLastName(lastName); + user.setEmail(email); + user.setPassword("XXXXXXXXXXX"); + user.setPhone(phone); + user.setUserStatus(userStatus); + return user; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/ApiException.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/ApiException.java new file mode 100644 index 0000000..a4c5689 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/ApiException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.exception; + +public class ApiException extends Exception{ + private int code; + public ApiException (int code, String msg) { + super(msg); + this.code = code; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/BadRequestException.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/BadRequestException.java new file mode 100644 index 0000000..c1e9de0 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/BadRequestException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.exception; + +public class BadRequestException extends ApiException{ + private int code; + public BadRequestException (int code, String msg) { + super(code, msg); + this.code = code; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/NotFoundException.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/NotFoundException.java new file mode 100644 index 0000000..6006095 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/exception/NotFoundException.java @@ -0,0 +1,25 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.exception; + +public class NotFoundException extends ApiException { + private int code; + public NotFoundException (int code, String msg) { + super(code, msg); + this.code = code; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/ApiResponse.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/ApiResponse.java new file mode 100644 index 0000000..1ccf7af --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/ApiResponse.java @@ -0,0 +1,84 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import javax.xml.bind.annotation.XmlTransient; + +@javax.xml.bind.annotation.XmlRootElement +public class ApiResponse { + public static final int ERROR = 1; + public static final int WARNING = 2; + public static final int INFO = 3; + public static final int OK = 4; + public static final int TOO_BUSY = 5; + + int code; + String type; + String message; + + public ApiResponse(){} + + public ApiResponse(int code, String message){ + this.code = code; + switch(code){ + case ERROR: + setType("error"); + break; + case WARNING: + setType("warning"); + break; + case INFO: + setType("info"); + break; + case OK: + setType("ok"); + break; + case TOO_BUSY: + setType("too busy"); + break; + default: + setType("unknown"); + break; + } + this.message = message; + } + + @XmlTransient + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Category.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Category.java new file mode 100644 index 0000000..938dde7 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Category.java @@ -0,0 +1,43 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Category") +public class Category { + private long id; + private String name; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Order.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Order.java new file mode 100644 index 0000000..93909ec --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Order.java @@ -0,0 +1,87 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import io.swagger.annotations.*; + +import java.util.Date; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Order") +public class Order { + private long id; + private long petId; + private int quantity; + private Date shipDate; + private String status; + private boolean complete; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public boolean isComplete() { + return complete; + } + + public void setComplete(boolean complete) { + this.complete = complete; + } + + @XmlElement(name = "petId") + public long getPetId() { + return petId; + } + + public void setPetId(long petId) { + this.petId = petId; + } + + @XmlElement(name = "quantity") + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + @XmlElement(name = "status") + @ApiModelProperty(value = "Order Status", allowableValues = "placed, approved, delivered") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + @XmlElement(name = "shipDate") + public Date getShipDate() { + return shipDate; + } + + public void setShipDate(Date shipDate) { + this.shipDate = shipDate; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Pet.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Pet.java new file mode 100644 index 0000000..da809d6 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Pet.java @@ -0,0 +1,92 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import io.swagger.annotations.*; + +import java.util.List; +import java.util.ArrayList; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Pet") +public class Pet { + private long id; + private Category category; + private String name; + private List photoUrls = new ArrayList(); + private List tags = new ArrayList(); + private String status; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "category") + public Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } + + @XmlElement(name = "name") + @ApiModelProperty(example = "doggie", required = true) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlElementWrapper(name = "photoUrls") + @XmlElement(name = "photoUrl", required = true) + public List getPhotoUrls() { + return photoUrls; + } + + public void setPhotoUrls(List photoUrls) { + this.photoUrls = photoUrls; + } + + @XmlElementWrapper(name = "tags") + @XmlElement(name = "tag") + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + @XmlElement(name = "status") + @ApiModelProperty(value = "pet status in the store", allowableValues = "available,pending,sold") + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Tag.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Tag.java new file mode 100644 index 0000000..41a9245 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/Tag.java @@ -0,0 +1,43 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "Tag") +public class Tag { + private long id; + private String name; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/User.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/User.java new file mode 100644 index 0000000..aef0d24 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/model/User.java @@ -0,0 +1,106 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.model; + +import io.swagger.annotations.*; + +import javax.xml.bind.annotation.*; + +@XmlRootElement(name = "User") +public class User { + private long id; + private String username; + private String firstName; + private String lastName; + private String email; + private String password; + private String phone; + private int userStatus; + + @XmlElement(name = "id") + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @XmlElement(name = "firstName") + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + @XmlElement(name = "username") + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @XmlElement(name = "lastName") + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @XmlElement(name = "email") + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @XmlElement(name = "password") + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @XmlElement(name = "phone") + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + @XmlElement(name = "userStatus") + @ApiModelProperty(value = "User Status", allowableValues = "1-registered,2-active,3-closed") + public int getUserStatus() { + return userStatus; + } + + public void setUserStatus(int userStatus) { + this.userStatus = userStatus; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/JavaRestResourceUtil.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/JavaRestResourceUtil.java new file mode 100644 index 0000000..7f8ceaf --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/JavaRestResourceUtil.java @@ -0,0 +1,100 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.resource; + +import java.text.SimpleDateFormat; + +import java.util.Date; + +public class JavaRestResourceUtil { + public int getInt(int minVal, int maxVal, int defaultValue, String inputString) { + int output = defaultValue; + try { + output = Integer.parseInt(inputString); + } + catch (Exception e){ + output = defaultValue; + } + + if (output < minVal) output = minVal; + if (maxVal == -1) { + if (output < minVal) output = minVal; + } + else if (output > maxVal) output = maxVal; + return output; + } + + public long getLong(long minVal, long maxVal, long defaultValue, String inputString) { + long output = defaultValue; + try { + output = Long.parseLong(inputString); + } + catch (Exception e){ + output = defaultValue; + } + + if (output < minVal) output = minVal; + if (maxVal == -1) { if (output < minVal) output = minVal; } + else if (output > maxVal) output = maxVal; + return output; + } + + public double getDouble(double minVal, double maxVal, double defaultValue, String inputString) { + double output = defaultValue; + try { + output = Double.parseDouble(inputString); + } + catch (Exception e){ + output = defaultValue; + } + + if (output < minVal) output = minVal; + if (maxVal == -1) { + if (output < minVal) output = minVal; + } + else if (output > maxVal) output = maxVal; + return output; + } + + public boolean getBoolean(boolean defaultValue, String booleanString) { + boolean output = defaultValue; + if (booleanString == null) output = defaultValue; + + // treat "", "YES" as "true" + if ("".equals(booleanString)) output = true; + else if ("YES".equalsIgnoreCase(booleanString)) output = true; + else if ("NO".equalsIgnoreCase(booleanString)) output = false; + else { + try { + output = Boolean.parseBoolean(booleanString); + } + catch (Exception e){ + output = defaultValue; + } + } + return output; + } + + public Date getDate(Date defaultValue, String dateString){ + try { + return new SimpleDateFormat("yyyy-MM-dd").parse(dateString); + } + catch(Exception e) { + return defaultValue; + } + } +} \ No newline at end of file diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetResource.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetResource.java new file mode 100644 index 0000000..c3f19f9 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetResource.java @@ -0,0 +1,134 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.resource; + +import com.github.kongchen.swagger.sample.wordnik.data.PetData; +import io.swagger.annotations.*; +import com.github.kongchen.swagger.sample.wordnik.model.Pet; +import io.swagger.annotations.ApiResponse; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.*; + +@Path("/pet") +@Api(value = "/pet", description = "Operations about pets", authorizations = { + @Authorization(value = "petstore_auth", + scopes = { + @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"), + @AuthorizationScope(scope = "read:pets", description = "read your pets") + }) +}) +@Produces({"application/json", "application/xml"}) +public class PetResource { + static PetData petData = new PetData(); + static JavaRestResourceUtil ru = new JavaRestResourceUtil(); + + @GET + @Path("/{petId}") + @ApiOperation(value = "Find pet by ID", + notes = "Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions", + response = Pet.class, + authorizations = @Authorization(value = "api_key") + ) + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"), + @ApiResponse(code = 404, message = "Pet not found") }) + public Response getPetById( + @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true) @PathParam("petId") Long petId) + throws com.github.kongchen.swagger.sample.wordnik.exception.NotFoundException { + Pet pet = petData.getPetbyId(petId); + if (null != pet) { + return Response.ok().entity(pet).build(); + } else { + throw new com.github.kongchen.swagger.sample.wordnik.exception.NotFoundException(404, "Pet not found"); + } + } + + @DELETE + @Path("/{petId}") + @ApiOperation(value = "Deletes a pet") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid pet value")}) + public Response deletePet( + @ApiParam() @HeaderParam("api_key") String apiKey, + @ApiParam(value = "Pet id to delete", required = true)@PathParam("petId") Long petId) { + petData.deletePet(petId); + return Response.ok().build(); + } + + @POST + @Consumes({"application/json", "application/xml"}) + @ApiOperation(value = "Add a new pet to the store") + @ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") }) + public Response addPet( + @ApiParam(value = "Pet object that needs to be added to the store", required = true) Pet pet) { + Pet updatedPet = petData.addPet(pet); + return Response.ok().entity(updatedPet).build(); + } + + @PUT + @Consumes({"application/json", "application/xml"}) + @ApiOperation(value = "Update an existing pet") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"), + @ApiResponse(code = 404, message = "Pet not found"), + @ApiResponse(code = 405, message = "Validation exception") }) + public Response updatePet( + @ApiParam(value = "Pet object that needs to be added to the store", required = true) Pet pet) { + Pet updatedPet = petData.addPet(pet); + return Response.ok().entity(updatedPet).build(); + } + + @GET + @Path("/findByStatus") + @ApiOperation(value = "Finds Pets by status", + notes = "Multiple status values can be provided with comma seperated strings", + response = Pet.class, + responseContainer = "List") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid status value") }) + public Response findPetsByStatus( + @ApiParam(value = "Status values that need to be considered for filter", required = true, defaultValue = "available", allowableValues = "available,pending,sold", allowMultiple = true) @QueryParam("status") String status) { + return Response.ok(petData.findPetByStatus(status)).build(); + } + + @GET + @Path("/findByTags") + @ApiOperation(value = "Finds Pets by tags", + notes = "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", + response = Pet.class, + responseContainer = "List") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid tag value") }) + @Deprecated + public Response findPetsByTags( + @ApiParam(value = "Tags to filter by", required = true, allowMultiple = true) @QueryParam("tags") String tags) { + return Response.ok(petData.findPetByTags(tags)).build(); + } + + @POST + @Path("/{petId}") + @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) + @ApiOperation(value = "Updates a pet in the store with form data", + consumes = MediaType.APPLICATION_FORM_URLENCODED) + @ApiResponses(value = { + @ApiResponse(code = 405, message = "Invalid input")}) + public Response updatePetWithForm ( + @ApiParam(value = "ID of pet that needs to be updated", required = true)@PathParam("petId") String petId, + @ApiParam(value = "Updated name of the pet", required = false)@FormParam("name") String name, + @ApiParam(value = "Updated status of the pet", required = false)@FormParam("status") String status) { + System.out.println(name); + System.out.println(status); + return Response.ok().entity(new com.github.kongchen.swagger.sample.wordnik.model.ApiResponse(200, "SUCCESS")).build(); + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetStoreResource.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetStoreResource.java new file mode 100644 index 0000000..47b20aa --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/PetStoreResource.java @@ -0,0 +1,74 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.resource; + +import io.swagger.annotations.*; +import com.github.kongchen.swagger.sample.wordnik.data.StoreData; +import com.github.kongchen.swagger.sample.wordnik.model.Order; +import com.github.kongchen.swagger.sample.wordnik.exception.NotFoundException; + +import javax.ws.rs.core.Response; +import javax.ws.rs.*; + +@Path("/store") +@Api(value="/store" , description = "Operations about store") +@Produces({"application/json", "application/xml"}) +public class PetStoreResource { + static StoreData storeData = new StoreData(); + static JavaRestResourceUtil ru = new JavaRestResourceUtil(); + + @GET + @Path("/order/{orderId}") + @ApiOperation(value = "Find purchase order by ID", + notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", + response = Order.class) + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"), + @ApiResponse(code = 404, message = "Order not found") }) + public Response getOrderById( + @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true) @PathParam("orderId") String orderId) + throws NotFoundException { + Order order = storeData.findOrderById(ru.getLong(0, 10000, 0, orderId)); + if (null != order) { + return Response.ok().entity(order).build(); + } else { + throw new NotFoundException(404, "Order not found"); + } + } + + @POST + @Path("/order") + @ApiOperation(value = "Place an order for a pet") + @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") }) + public Order placeOrder( + @ApiParam(value = "order placed for purchasing the pet", + required = true) Order order) { + storeData.placeOrder(order); + return storeData.placeOrder(order); + } + + @DELETE + @Path("/order/{orderId}") + @ApiOperation(value = "Delete purchase order by ID", + notes = "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"), + @ApiResponse(code = 404, message = "Order not found") }) + public Response deleteOrder( + @ApiParam(value = "ID of the order that needs to be deleted", allowableValues = "range[1,infinity]", required = true) @PathParam("orderId") String orderId) { + storeData.deleteOrder(ru.getLong(0, 10000, 0, orderId)); + return Response.ok().entity("").build(); + } +} diff --git a/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/UserResource.java b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/UserResource.java new file mode 100644 index 0000000..4c4f1a9 --- /dev/null +++ b/src/test/java/com/github/kongchen/swagger/sample/wordnik/resource/UserResource.java @@ -0,0 +1,134 @@ +/** + * Copyright 2014 Reverb Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.kongchen.swagger.sample.wordnik.resource; + +import io.swagger.annotations.*; +import com.github.kongchen.swagger.sample.wordnik.data.UserData; +import com.github.kongchen.swagger.sample.wordnik.model.User; +import com.github.kongchen.swagger.sample.wordnik.exception.ApiException; + +import javax.ws.rs.core.Response; +import javax.ws.rs.*; + +@Path("/user") +@Api(value="/user", description = "Operations about user") +@Produces({"application/json", "application/xml"}) +public class UserResource { + static UserData userData = new UserData(); + + @POST + @ApiOperation(value = "Create user", + notes = "This can only be done by the logged in user.", + position = 1) + public Response createUser( + @ApiParam(value = "Created user object", required = true) User user) { + userData.addUser(user); + return Response.ok().entity("").build(); + } + + @POST + @Path("/createWithArray") + @ApiOperation(value = "Creates list of users with given input array", + position = 2) + public Response createUsersWithArrayInput(@ApiParam(value = "List of user object", required = true) User[] users) { + for (User user : users) { + userData.addUser(user); + } + return Response.ok().entity("").build(); + } + + @POST + @Path("/createWithList") + @ApiOperation(value = "Creates list of users with given input array", + position = 3) + public Response createUsersWithListInput(@ApiParam(value = "List of user object", required = true) java.util.List users) { + for (User user : users) { + userData.addUser(user); + } + return Response.ok().entity("").build(); + } + + @PUT + @Path("/{username}") + @ApiOperation(value = "Updated user", + notes = "This can only be done by the logged in user.", + position = 4) + @ApiResponses(value = { + @ApiResponse(code = 400, message = "Invalid user supplied"), + @ApiResponse(code = 404, message = "User not found") }) + public Response updateUser( + @ApiParam(value = "name that need to be deleted", required = true) @PathParam("username") String username, + @ApiParam(value = "Updated user object", required = true) User user) { + userData.addUser(user); + return Response.ok().entity("").build(); + } + + @DELETE + @Path("/{username}") + @ApiOperation(value = "Delete user", + notes = "This can only be done by the logged in user.", + position = 5) + @ApiResponses(value = { + @ApiResponse(code = 400, message = "Invalid username supplied"), + @ApiResponse(code = 404, message = "User not found") }) + public Response deleteUser( + @ApiParam(value = "The name that needs to be deleted", required = true) @PathParam("username") String username) { + userData.removeUser(username); + return Response.ok().entity("").build(); + } + + @GET + @Path("/{username}") + @ApiOperation(value = "Get user by user name", + response = User.class, + position = 0) + @ApiResponses(value = { + @ApiResponse(code = 400, message = "Invalid username supplied"), + @ApiResponse(code = 404, message = "User not found") }) + public Response getUserByName( + @ApiParam(value = "The name that needs to be fetched. Use user1 for testing. ", required = true) @PathParam("username") String username) + throws ApiException { + User user = userData.findUserByName(username); + if (null != user) { + return Response.ok().entity(user).build(); + } else { + throw new com.github.kongchen.swagger.sample.wordnik.exception.NotFoundException(404, "User not found"); + } + } + + @GET + @Path("/login") + @ApiOperation(value = "Logs user into the system", + response = String.class, + position = 6) + @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid username/password supplied") }) + public Response loginUser( + @ApiParam(value = "The user name for login", required = true) @QueryParam("username") String username, + @ApiParam(value = "The password for login in clear text", required = true) @QueryParam("password") String password) { + return Response.ok() + .entity("logged in user session:" + System.currentTimeMillis()) + .build(); + } + + @GET + @Path("/logout") + @ApiOperation(value = "Logs out current logged in user session", + position = 7) + public Response logoutUser() { + return Response.ok().entity("").build(); + } +} diff --git a/src/test/resources/securityDefinition.json b/src/test/resources/securityDefinition.json new file mode 100644 index 0000000..05357e6 --- /dev/null +++ b/src/test/resources/securityDefinition.json @@ -0,0 +1,16 @@ +{ + "api_key": { + "type": "apiKey", + "name": "api_key", + "in": "header" + }, + "petstore_auth": { + "type": "oauth2", + "authorizationUrl": "http://swagger.io/api/oauth/dialog", + "flow": "implicit", + "scopes": { + "write:pets": "modify pets in your account", + "read:pets": "read your pets" + } + } +} \ No newline at end of file diff --git a/src/test/resources/swagger-task-01.xml b/src/test/resources/swagger-task-01.xml index b010cf7..bb8092f 100644 --- a/src/test/resources/swagger-task-01.xml +++ b/src/test/resources/swagger-task-01.xml @@ -21,11 +21,38 @@ - + false com.github.kongchen.swagger.sample.wordnik.resource - v1 - http://petstore.swagger.wordnik.com/api - + http,https + petstore.swagger.wordnik.com + /api + + Swagger Maven Plugin Sample + v1 + This is a sample for swagger-maven-plugin + http://www.github.com/kongchen/swagger-maven-plugin + + kongchen@gmail.com + Kong Chen + http://kongch.com + + + http://www.apache.org/licenses/LICENSE-2.0.html + Apache 2.0 + + + + ${basedir}/templates/strapdown.html.hbs + ${basedir}/generated/document.html + generated/swagger-ui + + + /securityDefinition.json + + diff --git a/src/test/resources/templates/markdown.hbs b/src/test/resources/templates/markdown.hbs new file mode 100644 index 0000000..edff982 --- /dev/null +++ b/src/test/resources/templates/markdown.hbs @@ -0,0 +1,107 @@ +#{{#info}}{{title}} + + +## {{join schemes " | "}}://{{host}}{{basePath}} + + +{{description}} + +{{#contact}} +[**Contact the developer**](mailto:{{email}}) +{{/contact}} + +**Version** {{version}} + +[**Terms of Service**]({{termsOfService}}) + +{{#license}}[**{{name}}**]({{url}}){{/license}} + +{{/info}} + +{{#if consumes}}**Consumes:** {{join consumes ", "}}{{/if}} + +{{#if produces}}**Produces:** {{join produces ", "}}{{/if}} + +{{#if securityDefinitions}} +# Security Definitions +{{/if}} +{{> security}} + +# APIs + +{{#each paths}} +## {{@key}} +{{#this}} +{{#get}} +### GET +{{> operation}} +{{/get}} + +{{#put}} +### PUT +{{> operation}} +{{/put}} + +{{#post}} +### POST + +{{> operation}} + +{{/post}} + +{{#delete}} +### DELETE +{{> operation}} +{{/delete}} + +{{#option}} +### OPTION +{{> operation}} +{{/option}} + +{{#patch}} +### PATCH +{{> operation}} +{{/patch}} + +{{#head}} +### HEAD +{{> operation}} +{{/head}} + +{{/this}} +{{/each}} + +# Definitions +{{#each definitions}} +## {{@key}} + + + + + + + + + + {{#each this.properties}} + + + + + + + + {{/each}} +
nametyperequireddescriptionexample
{{@key}} + {{#ifeq type "array"}} + {{#items.$ref}} + {{type}}[{{basename items.$ref}}] + {{/items.$ref}} + {{^items.$ref}}{{type}}[{{items.type}}]{{/items.$ref}} + {{else}} + {{#$ref}}{{basename $ref}}{{/$ref}} + {{^$ref}}{{type}}{{#format}} ({{format}}){{/format}}{{/$ref}} + {{/ifeq}} + {{#required}}required{{/required}}{{^required}}optional{{/required}}{{#description}}{{{description}}}{{/description}}{{^description}}-{{/description}}{{example}}
+{{/each}} diff --git a/src/test/resources/templates/operation.hbs b/src/test/resources/templates/operation.hbs new file mode 100644 index 0000000..226b9e1 --- /dev/null +++ b/src/test/resources/templates/operation.hbs @@ -0,0 +1,73 @@ +{{#deprecated}}-deprecated-{{/deprecated}} +{{summary}} + +{{description}} + +{{#if externalDocs.url}}{{externalDocs.description}}. [See external documents for more details]({{externalDocs.url}}) +{{/if}} + +{{#if security}} +#### Security +{{/if}} + +{{#security}} +{{#each this}} +* {{@key}} +{{#this}} * {{this}} +{{/this}} +{{/each}} +{{/security}} + +#### Request + +{{#if consumes}} +**Content-Type: ** {{join consumes ", "}}{{/if}} + +##### Parameters +{{#if parameters}} + + + + + + + + + +{{/if}} + +{{#parameters}} + + + + + + +{{#ifeq in "body"}} + +{{else}} + {{#ifeq type "array"}} + + {{else}} + + {{/ifeq}} +{{/ifeq}} + +{{/parameters}} +{{#if parameters}} +
NameLocated inRequiredDescriptionDefaultSchema
{{name}}{{in}}{{#if required}}yes{{else}}no{{/if}}{{description}}{{#if pattern}} (**Pattern**: `{{pattern}}`){{/if}} - + {{#ifeq schema.type "array"}}Array[{{basename schema.items.$ref}}]{{/ifeq}} + {{#schema.$ref}}{{basename schema.$ref}} {{/schema.$ref}} + Array[{{items.type}}] ({{collectionFormat}}){{type}} {{#format}}({{format}}){{/format}}
+{{/if}} + + +#### Response + +{{#if produces}}**Content-Type: ** {{join produces ", "}}{{/if}} + + +| Status Code | Reason | Response Model | +|-------------|-------------|----------------| +{{#each responses}}| {{@key}} | {{description}} | {{#schema.$ref}}{{basename schema.$ref}}{{/schema.$ref}}{{#ifeq schema.type "array"}}Array[{{basename schema.items.$ref}}]{{/ifeq}}{{^schema}} - {{/schema}}| +{{/each}} \ No newline at end of file diff --git a/src/test/resources/templates/security.hbs b/src/test/resources/templates/security.hbs new file mode 100644 index 0000000..04f86e8 --- /dev/null +++ b/src/test/resources/templates/security.hbs @@ -0,0 +1,88 @@ +{{#each securityDefinitions}} +### {{@key}} +{{#this}} +{{#ifeq type "oauth2"}} + + + + + +{{#if description}} + + + + +{{/if}} +{{#if authorizationUrl}} + + + + +{{/if}} +{{#if flow}} + + + + +{{/if}} +{{#if tokenUrl}} + + + + +{{/if}} +{{#if scopes}} + + +{{#each scopes}} + + + + +{{/each}} + +{{/if}} +
type{{type}}
description{{description}}
authorizationUrl{{authorizationUrl}}
flow{{flow}}
tokenUrl{{tokenUrl}}
scopes{{@key}}{{this}}
+{{/ifeq}} +{{#ifeq type "apiKey"}} + + + + + +{{#if description}} + + + + +{{/if}} +{{#if name}} + + + + +{{/if}} +{{#if in}} + + + + +{{/if}} +
type{{type}}
description{{description}}
name{{name}}
in{{in}}
+{{/ifeq}} +{{#ifeq type "basic"}} + + + + + +{{#if description}} + + + + +{{/if}} +
type{{type}}
description{{description}}
+{{/ifeq}} +{{/this}} +{{/each}} \ No newline at end of file diff --git a/src/test/resources/templates/strapdown.html.hbs b/src/test/resources/templates/strapdown.html.hbs new file mode 100644 index 0000000..ec02669 --- /dev/null +++ b/src/test/resources/templates/strapdown.html.hbs @@ -0,0 +1,10 @@ + + +API Document + + +{{>markdown}} + + + + \ No newline at end of file