From ef8cc286a451be9cb437e043be7fb0493bf2dd86 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Tue, 17 Jul 2018 11:44:04 +0200 Subject: [PATCH] Fixes #265 @Valid annotations added on same getter in both parent and child objects lead to exception --- .../raml2code/interpreters/PojoBuilder.java | 13 +- .../raml2code/github/Issue265RulesTest.java | 21 ++ .../resources/ramls/github/issue-265.raml | 23 ++ .../Issue208Spring4ControllerStub.java.txt | 2 - ...ssue265Spring4ControllerDecorator.java.txt | 327 ++++++++++++++++++ 5 files changed, 373 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue265RulesTest.java create mode 100644 src/test/resources/ramls/github/issue-265.raml create mode 100644 src/test/resources/validations/github/Issue265Spring4ControllerDecorator.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 653c470c..e79e1e2e 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 @@ -22,7 +22,6 @@ import java.util.Objects; import java.util.Set; -import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.collections.MapUtils; import org.raml.v2.api.model.v10.datamodel.DateTimeTypeDeclaration; import org.raml.v2.api.model.v10.datamodel.TypeDeclaration; @@ -31,6 +30,7 @@ import org.springframework.util.StringUtils; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; @@ -227,16 +227,7 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV // validation if (Config.getPojoConfig().isIncludeJsr303Annotations() && validations != null) { - // check if field is complex object so we can mark it with - // @Valid - boolean isPOJO = type.startsWith(this.pojo._package().name() + "."); - if (!isPOJO && resolvedType.getClass().getName().equals("com.sun.codemodel.JNarrowedClass") - && resolvedType.getTypeParameters().size() == 1) { - JClass typeClass = resolvedType.getTypeParameters().get(0); - isPOJO = typeClass.fullName().startsWith(this.pojo._package().name() + "."); - } - - validations.annotateFieldJSR303(getterMethod, isPOJO); + validations.annotateFieldJSR303(getterMethod, false); } return this; diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue265RulesTest.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue265RulesTest.java new file mode 100644 index 00000000..d1e6f438 --- /dev/null +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue265RulesTest.java @@ -0,0 +1,21 @@ +package com.phoenixnap.oss.ramlplugin.raml2code.github; + +import org.junit.Test; + +import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase; +import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule; + +/** + * @author aleksandars + * @since 2.0.3 + */ +public class Issue265RulesTest extends GitHubAbstractRuleTestBase { + + @Test + public void testDontGenerateForAnnotation() throws Exception { + loadRaml("issue-265.raml"); + rule = new Spring4ControllerDecoratorRule(); + rule.apply(getControllerMetadata(), jCodeModel); + verifyGeneratedCode("Issue265Spring4ControllerDecorator"); + } +} diff --git a/src/test/resources/ramls/github/issue-265.raml b/src/test/resources/ramls/github/issue-265.raml new file mode 100644 index 00000000..7bb95b3b --- /dev/null +++ b/src/test/resources/ramls/github/issue-265.raml @@ -0,0 +1,23 @@ +#%RAML 1.0 + +title: Validation Bug Test + +types: + Address: + type: object + properties: + city: string + street: string + houseNo: string + Parent: + type: object + properties: + address: Address + Child: + type: Parent + +/parent: + post: + body: + application/json: + type: Child \ No newline at end of file diff --git a/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt index 99391324..1a49ede4 100644 --- a/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt @@ -204,7 +204,6 @@ 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; @@ -250,7 +249,6 @@ public class ProductUpdate * @return * groups */ - @Valid public List getGroups() { if (groups == null) { return new ArrayList(); diff --git a/src/test/resources/validations/github/Issue265Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue265Spring4ControllerDecorator.java.txt new file mode 100644 index 00000000..f0cc75e4 --- /dev/null +++ b/src/test/resources/validations/github/Issue265Spring4ControllerDecorator.java.txt @@ -0,0 +1,327 @@ +-----------------------------------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 houseNo; + + /** + * Creates a new Address. + * + */ + public Address() { + super(); + } + + /** + * Creates a new Address. + * + */ + public Address(String city, String street, String houseNo) { + super(); + this.city = city; + this.street = street; + this.houseNo = houseNo; + } + + /** + * 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 houseNo. + * + * @return + * houseNo + */ + @NotNull + public String getHouseNo() { + return houseNo; + } + + /** + * Set the houseNo. + * + * @param houseNo + * the new houseNo + */ + public void setHouseNo(String houseNo) { + this.houseNo = houseNo; + } + + public int hashCode() { + return new HashCodeBuilder().append(city).append(street).append(houseNo).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(houseNo, otherObject.houseNo).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("city", city).append("street", street).append("houseNo", houseNo).toString(); + } + +} +-----------------------------------com.gen.test.model.Child.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 Child + extends Parent + implements Serializable +{ + + + /** + * Creates a new Child. + * + */ + public Child() { + super(); + } + + /** + * Returns the address. + * + * @return + * address + */ + @NotNull + public Address getAddress() { + return address; + } + + public int hashCode() { + return new HashCodeBuilder().appendSuper(super.hashCode()).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Child otherObject = ((Child) other); + return new EqualsBuilder().appendSuper(super.equals(otherObject)).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).appendSuper(super.toString()).toString(); + } + +} +-----------------------------------com.gen.test.model.Parent.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +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 Parent implements Serializable +{ + + protected Address address; + + /** + * Creates a new Parent. + * + */ + public Parent() { + super(); + } + + /** + * Creates a new Parent. + * + */ + public Parent(Address address) { + super(); + this.address = address; + } + + /** + * Returns the address. + * + * @return + * address + */ + @NotNull + @Valid + public Address getAddress() { + return address; + } + + /** + * Set the address. + * + * @param address + * the new address + */ + public void setAddress(Address address) { + this.address = address; + } + + public int hashCode() { + return new HashCodeBuilder().append(address).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Parent otherObject = ((Parent) other); + return new EqualsBuilder().append(address, otherObject.address).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("address", address).toString(); + } + +} +-----------------------------------com.gen.test.ParentController.java----------------------------------- + +package com.gen.test; + +import javax.validation.Valid; +import com.gen.test.model.Child; +import org.springframework.http.ResponseEntity; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +public interface ParentController { + + + /** + * No description + * + */ + public ResponseEntity createChild( + @Valid + Child child); + +} +-----------------------------------com.gen.test.ParentControllerDecorator.java----------------------------------- + +package com.gen.test; + +import javax.validation.Valid; +import com.gen.test.model.Child; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestBody; +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/parent") +@Validated +public class ParentControllerDecorator + implements ParentController +{ + + @Autowired + private ParentController parentControllerDelegate; + + /** + * No description + * + */ + @RequestMapping(value = "", method = RequestMethod.POST) + public ResponseEntity createChild( + @Valid + @RequestBody + Child child) { + return this.parentControllerDelegate.createChild(child); + } + +}