From 0d9cef52a5801c9847cb8cb57e0503258c1d6bca Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Wed, 25 Jul 2018 09:18:03 +0200 Subject: [PATCH] Fixes #266 Collections are always initialized --- .../raml2code/interpreters/PojoBuilder.java | 5 + .../raml2code/github/Issue266RulesTest.java | 34 ++ .../raml2code/plugin/TestConfig.java | 3 + .../raml2code/rules/TestPojoConfig.java | 4 + .../resources/ramls/github/issue-266.raml | 26 ++ .../Issue266-1Spring4ControllerStub.java.txt | 310 +++++++++++++++++ .../Issue266-2Spring4ControllerStub.java.txt | 311 ++++++++++++++++++ 7 files changed, 693 insertions(+) create mode 100644 src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue266RulesTest.java create mode 100644 src/test/resources/ramls/github/issue-266.raml create mode 100644 src/test/resources/validations/github/Issue266-1Spring4ControllerStub.java.txt create mode 100644 src/test/resources/validations/github/Issue266-2Spring4ControllerStub.java.txt diff --git a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/PojoBuilder.java b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/PojoBuilder.java index e79e1e2e..00daab27 100644 --- a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/PojoBuilder.java +++ b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/PojoBuilder.java @@ -14,6 +14,7 @@ import java.math.BigDecimal; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashMap; @@ -216,6 +217,10 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV jExpression = JExpr._new(narrowedListClass); } + if (resolveType(Collection.class.getName()).isAssignableFrom(resolvedType) && !Config.getPojoConfig().isInitializeCollections()) { + jExpression = null; + } + // lets ignore this if parent contains it and we will use parent's in // the constructor JFieldVar parentField = parentContainsField(this.pojo, name); diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue266RulesTest.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue266RulesTest.java new file mode 100644 index 00000000..d795c8fa --- /dev/null +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue266RulesTest.java @@ -0,0 +1,34 @@ +package com.phoenixnap.oss.ramlplugin.raml2code.github; + +import org.junit.Test; + +import com.phoenixnap.oss.ramlplugin.raml2code.plugin.TestConfig; +import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase; +import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule; + +/** + * @author aleksandars + * @since 2.0.4 + */ +public class Issue266RulesTest extends GitHubAbstractRuleTestBase { + + @Test + public void verify_collection_initialization_false() throws Exception { + TestConfig.setInitializeCollections(false); + loadRaml("issue-266.raml"); + rule = new Spring4ControllerDecoratorRule(); + rule.apply(getControllerMetadata(), jCodeModel); + verifyGeneratedCode("Issue266-1Spring4ControllerStub"); + TestConfig.setInitializeCollections(true); + } + + @Test + public void verify_collection_initialization_true() throws Exception { + TestConfig.setInitializeCollections(true); + loadRaml("issue-266.raml"); + rule = new Spring4ControllerDecoratorRule(); + rule.apply(getControllerMetadata(), jCodeModel); + verifyGeneratedCode("Issue266-2Spring4ControllerStub"); + TestConfig.setInitializeCollections(true); + } +} diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java index 6b081f64..99df746a 100644 --- a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/plugin/TestConfig.java @@ -59,4 +59,7 @@ public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation Config.setDontGenerateForAnnotation(dontGenerateForAnnotation); } + public static void setInitializeCollections(boolean initializeCollections) { + ((TestPojoConfig) Config.getPojoConfig()).setInitializeCollections(initializeCollections); + } } diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/rules/TestPojoConfig.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/rules/TestPojoConfig.java index 32330565..bac1a61f 100644 --- a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/rules/TestPojoConfig.java +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/rules/TestPojoConfig.java @@ -27,4 +27,8 @@ public void setDateType(String dateType) { public void setTimeType(String timeType) { this.timeType = timeType; } + + public void setInitializeCollections(boolean initializeCollections) { + this.initializeCollections = initializeCollections; + } } \ No newline at end of file diff --git a/src/test/resources/ramls/github/issue-266.raml b/src/test/resources/ramls/github/issue-266.raml new file mode 100644 index 00000000..dde0ed4d --- /dev/null +++ b/src/test/resources/ramls/github/issue-266.raml @@ -0,0 +1,26 @@ +#%RAML 1.0 +title: Collections are always initialized #266 +version: 1.0 + +types: + Address: + type: object + properties: + city: string + street: string + houseNumber: string + Student: + type: object + properties: + firstName: string + lastName: string + addresses: Address[] + +/students: + get: + description: Get all + responses: + 200: + body: + application/json: + type: Student[] \ No newline at end of file diff --git a/src/test/resources/validations/github/Issue266-1Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue266-1Spring4ControllerStub.java.txt new file mode 100644 index 00000000..0743a27a --- /dev/null +++ b/src/test/resources/validations/github/Issue266-1Spring4ControllerStub.java.txt @@ -0,0 +1,310 @@ +-----------------------------------com.gen.test.model.Address.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import javax.validation.constraints.NotNull; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class Address implements Serializable +{ + + protected String city; + protected String street; + protected String houseNumber; + + /** + * Creates a new Address. + * + */ + public Address() { + super(); + } + + /** + * Creates a new Address. + * + */ + public Address(String city, String street, String houseNumber) { + super(); + this.city = city; + this.street = street; + this.houseNumber = houseNumber; + } + + /** + * Returns the city. + * + * @return + * city + */ + @NotNull + public String getCity() { + return city; + } + + /** + * Set the city. + * + * @param city + * the new city + */ + public void setCity(String city) { + this.city = city; + } + + /** + * Returns the street. + * + * @return + * street + */ + @NotNull + public String getStreet() { + return street; + } + + /** + * Set the street. + * + * @param street + * the new street + */ + public void setStreet(String street) { + this.street = street; + } + + /** + * Returns the houseNumber. + * + * @return + * houseNumber + */ + @NotNull + public String getHouseNumber() { + return houseNumber; + } + + /** + * Set the houseNumber. + * + * @param houseNumber + * the new houseNumber + */ + public void setHouseNumber(String houseNumber) { + this.houseNumber = houseNumber; + } + + public int hashCode() { + return new HashCodeBuilder().append(city).append(street).append(houseNumber).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Address otherObject = ((Address) other); + return new EqualsBuilder().append(city, otherObject.city).append(street, otherObject.street).append(houseNumber, otherObject.houseNumber).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("city", city).append("street", street).append("houseNumber", houseNumber).toString(); + } + +} +-----------------------------------com.gen.test.model.Student.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import java.util.List; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class Student implements Serializable +{ + + protected String firstName; + protected String lastName; + protected List
addresses; + + /** + * Creates a new Student. + * + */ + public Student() { + super(); + } + + /** + * Creates a new Student. + * + */ + public Student(String firstName, String lastName, List
addresses) { + super(); + this.firstName = firstName; + this.lastName = lastName; + this.addresses = addresses; + } + + /** + * Returns the firstName. + * + * @return + * firstName + */ + @NotNull + public String getFirstName() { + return firstName; + } + + /** + * Set the firstName. + * + * @param firstName + * the new firstName + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * Returns the lastName. + * + * @return + * lastName + */ + @NotNull + public String getLastName() { + return lastName; + } + + /** + * Set the lastName. + * + * @param lastName + * the new lastName + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } + + /** + * Returns the addresses. + * + * @return + * addresses + */ + @NotNull + @Valid + public List
getAddresses() { + return addresses; + } + + /** + * Set the addresses. + * + * @param addresses + * the new addresses + */ + public void setAddresses(List
addresses) { + this.addresses = addresses; + } + + public int hashCode() { + return new HashCodeBuilder().append(firstName).append(lastName).append(addresses).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Student otherObject = ((Student) other); + return new EqualsBuilder().append(firstName, otherObject.firstName).append(lastName, otherObject.lastName).append(addresses, otherObject.addresses).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("firstName", firstName).append("lastName", lastName).append("addresses", addresses).toString(); + } + +} +-----------------------------------com.gen.test.StudentController.java----------------------------------- + +package com.gen.test; + +import java.util.List; +import com.gen.test.model.Student; +import org.springframework.http.ResponseEntity; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +public interface StudentController { + + + /** + * Get all + * + */ + public ResponseEntity> getStudents(); + +} +-----------------------------------com.gen.test.StudentControllerDecorator.java----------------------------------- + +package com.gen.test; + +import java.util.List; +import com.gen.test.model.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +@RestController +@RequestMapping("/api/students") +@Validated +public class StudentControllerDecorator + implements StudentController +{ + + @Autowired + private StudentController studentControllerDelegate; + + /** + * Get all + * + */ + @RequestMapping(value = "", method = RequestMethod.GET) + public ResponseEntity> getStudents() { + return this.studentControllerDelegate.getStudents(); + } + +} diff --git a/src/test/resources/validations/github/Issue266-2Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue266-2Spring4ControllerStub.java.txt new file mode 100644 index 00000000..7693c420 --- /dev/null +++ b/src/test/resources/validations/github/Issue266-2Spring4ControllerStub.java.txt @@ -0,0 +1,311 @@ +-----------------------------------com.gen.test.model.Address.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import javax.validation.constraints.NotNull; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class Address implements Serializable +{ + + protected String city; + protected String street; + protected String houseNumber; + + /** + * Creates a new Address. + * + */ + public Address() { + super(); + } + + /** + * Creates a new Address. + * + */ + public Address(String city, String street, String houseNumber) { + super(); + this.city = city; + this.street = street; + this.houseNumber = houseNumber; + } + + /** + * Returns the city. + * + * @return + * city + */ + @NotNull + public String getCity() { + return city; + } + + /** + * Set the city. + * + * @param city + * the new city + */ + public void setCity(String city) { + this.city = city; + } + + /** + * Returns the street. + * + * @return + * street + */ + @NotNull + public String getStreet() { + return street; + } + + /** + * Set the street. + * + * @param street + * the new street + */ + public void setStreet(String street) { + this.street = street; + } + + /** + * Returns the houseNumber. + * + * @return + * houseNumber + */ + @NotNull + public String getHouseNumber() { + return houseNumber; + } + + /** + * Set the houseNumber. + * + * @param houseNumber + * the new houseNumber + */ + public void setHouseNumber(String houseNumber) { + this.houseNumber = houseNumber; + } + + public int hashCode() { + return new HashCodeBuilder().append(city).append(street).append(houseNumber).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Address otherObject = ((Address) other); + return new EqualsBuilder().append(city, otherObject.city).append(street, otherObject.street).append(houseNumber, otherObject.houseNumber).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("city", city).append("street", street).append("houseNumber", houseNumber).toString(); + } + +} +-----------------------------------com.gen.test.model.Student.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class Student implements Serializable +{ + + protected String firstName; + protected String lastName; + protected List
addresses = new ArrayList
(); + + /** + * Creates a new Student. + * + */ + public Student() { + super(); + } + + /** + * Creates a new Student. + * + */ + public Student(String firstName, String lastName, List
addresses) { + super(); + this.firstName = firstName; + this.lastName = lastName; + this.addresses = addresses; + } + + /** + * Returns the firstName. + * + * @return + * firstName + */ + @NotNull + public String getFirstName() { + return firstName; + } + + /** + * Set the firstName. + * + * @param firstName + * the new firstName + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * Returns the lastName. + * + * @return + * lastName + */ + @NotNull + public String getLastName() { + return lastName; + } + + /** + * Set the lastName. + * + * @param lastName + * the new lastName + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } + + /** + * Returns the addresses. + * + * @return + * addresses + */ + @NotNull + @Valid + public List
getAddresses() { + return addresses; + } + + /** + * Set the addresses. + * + * @param addresses + * the new addresses + */ + public void setAddresses(List
addresses) { + this.addresses = addresses; + } + + public int hashCode() { + return new HashCodeBuilder().append(firstName).append(lastName).append(addresses).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Student otherObject = ((Student) other); + return new EqualsBuilder().append(firstName, otherObject.firstName).append(lastName, otherObject.lastName).append(addresses, otherObject.addresses).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("firstName", firstName).append("lastName", lastName).append("addresses", addresses).toString(); + } + +} +-----------------------------------com.gen.test.StudentController.java----------------------------------- + +package com.gen.test; + +import java.util.List; +import com.gen.test.model.Student; +import org.springframework.http.ResponseEntity; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +public interface StudentController { + + + /** + * Get all + * + */ + public ResponseEntity> getStudents(); + +} +-----------------------------------com.gen.test.StudentControllerDecorator.java----------------------------------- + +package com.gen.test; + +import java.util.List; +import com.gen.test.model.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +@RestController +@RequestMapping("/api/students") +@Validated +public class StudentControllerDecorator + implements StudentController +{ + + @Autowired + private StudentController studentControllerDelegate; + + /** + * Get all + * + */ + @RequestMapping(value = "", method = RequestMethod.GET) + public ResponseEntity> getStudents() { + return this.studentControllerDelegate.getStudents(); + } + +}