From f4a98d4ea8855c95989ac93bc3745e2b31d353e8 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Tue, 17 Apr 2018 14:57:13 +0200 Subject: [PATCH 1/7] Fixes #248 Inherited properties not respected --- pom.xml | 2 +- .../raml2code/interpreters/PojoBuilder.java | 85 ++++-- .../interpreters/RamlTypeValidations.java | 16 +- .../raml2code/github/Issue248RulesTest.java | 21 ++ .../resources/ramls/github/issue-248.raml | 36 +++ ...ssue248Spring4ControllerDecorator.java.txt | 269 ++++++++++++++++++ 6 files changed, 395 insertions(+), 34 deletions(-) create mode 100644 src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue248RulesTest.java create mode 100644 src/test/resources/ramls/github/issue-248.raml create mode 100644 src/test/resources/validations/github/Issue248Spring4ControllerDecorator.java.txt diff --git a/pom.xml b/pom.xml index 70462944..95b91507 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 1.6.2 3.3.9 0.5.1 - 1.0.18 + 1.0.20-SNAPSHOT 1.1.6.RELEASE 2.6 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 ff70d303..bcb0724a 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 @@ -150,22 +150,23 @@ private void withDefaultConstructor(String className) { defaultConstructorBody.invoke("super"); } - private boolean parentContainsField(JClass pojo, String name) { + private JFieldVar parentContainsField(JClass pojo, String name) { if (pojo != null && !pojo.fullName().equals(Object.class.getName())) { JClass parent = pojo._extends(); if (parent != null) { // Our parent has a parent, lets check if it has it - if (parentContainsField(parent, name)) { - return true; + JFieldVar parentField = parentContainsField(parent, name); + if (parentField != null) { + return parentField; } else { if (parent instanceof JDefinedClass) { - return ((JDefinedClass) parent).fields().containsKey(name); + return ((JDefinedClass) parent).fields().get(name); } } } } - return false; + return null; } public PojoBuilder withField(String name, String type, String comment, RamlTypeValidations validations, @@ -188,12 +189,6 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV // skip import } - // lets ignore this if parent contains it and we will use parent's in - // the constructor - if (parentContainsField(this.pojo, name)) { - return this; - } - JExpression jExpression = null; if (StringUtils.hasText(typeDeclaration.defaultValue())) { if (resolvedType.name().equals(Integer.class.getSimpleName())) { @@ -219,21 +214,35 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV jExpression = JExpr._new(narrowedListClass); } - // Add private variable - JFieldVar field = this.pojo.field(JMod.PRIVATE, resolvedType, toJavaName(name), jExpression); - if (Config.getPojoConfig().isIncludeJsr303Annotations() && validations != null) { + // lets ignore this if parent contains it and we will use parent's in + // the constructor + JFieldVar parentField = parentContainsField(this.pojo, name); + if (parentField != null) { + + // Add get method + JMethod getterMethod = generateGetterMethod(parentField, name, jExpression); + + // 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() + "."); + } - // 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(field, isPOJO); + return this; } + // Add protected variable + JFieldVar field = this.pojo.field(JMod.PROTECTED, resolvedType, toJavaName(name), jExpression); + if (resolvedType.name().equals(Date.class.getSimpleName())) { JAnnotationUse jAnnotationUse = field.annotate(JsonFormat.class); String format = null; @@ -249,10 +258,19 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV String fieldName = NamingHelper.convertToClassName(name); // Add get method - JMethod getter = this.pojo.method(JMod.PUBLIC, field.type(), "get" + fieldName); - getter.body()._return(field); - getter.javadoc().add("Returns the " + name + "."); - getter.javadoc().addReturn().add(field.name()); + JMethod getterMethod = generateGetterMethod(field, name, null); + 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); + } // Add set method JMethod setter = this.pojo.method(JMod.PUBLIC, this.pojoModel.VOID, "set" + fieldName); @@ -273,6 +291,23 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV return this; } + private JMethod generateGetterMethod(JFieldVar field, String fieldName, JExpression defaultValue) { + + String javaName = NamingHelper.convertToClassName(fieldName); + + // Add get method + JMethod getter = this.pojo.method(JMod.PUBLIC, field.type(), "get" + javaName); + if (defaultValue != null) { + JBlock body = getter.body(); + body._if(field.eq(JExpr._null()))._then()._return(defaultValue); + } + getter.body()._return(field); + getter.javadoc().add("Returns the " + fieldName + "."); + getter.javadoc().addReturn().add(field.name()); + + return getter; + } + /** * Adds a constructor with all the fields in the POJO. If no fields are * present it will not create an empty constructor because default diff --git a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/RamlTypeValidations.java b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/RamlTypeValidations.java index f1633217..9761e9ad 100644 --- a/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/RamlTypeValidations.java +++ b/src/main/java/com/phoenixnap/oss/ramlplugin/raml2code/interpreters/RamlTypeValidations.java @@ -10,7 +10,7 @@ import org.springframework.util.StringUtils; import com.sun.codemodel.JAnnotationUse; -import com.sun.codemodel.JFieldVar; +import com.sun.codemodel.JMethod; /* * Copyright 2002-2017 the original author or authors. @@ -83,16 +83,16 @@ public boolean isRequired() { * @param field * to add annotations to */ - public void annotateFieldJSR303(JFieldVar field, boolean addValidAnnotation) { + public void annotateFieldJSR303(JMethod getter, boolean addValidAnnotation) { if (isRequired()) { - field.annotate(NotNull.class); + getter.annotate(NotNull.class); } if (StringUtils.hasText(getPattern())) { - JAnnotationUse annotation = field.annotate(Pattern.class); + JAnnotationUse annotation = getter.annotate(Pattern.class); annotation.param("regexp", getPattern()); } if (getMinLength() != null || getMaxLength() != null) { - JAnnotationUse annotation = field.annotate(Size.class); + JAnnotationUse annotation = getter.annotate(Size.class); if (getMinLength() != null) { annotation.param("min", getMinLength()); @@ -103,16 +103,16 @@ public void annotateFieldJSR303(JFieldVar field, boolean addValidAnnotation) { } } if (addValidAnnotation) { - field.annotate(Valid.class); + getter.annotate(Valid.class); } if (minimum != null) { - JAnnotationUse annotation = field.annotate(DecimalMin.class); + JAnnotationUse annotation = getter.annotate(DecimalMin.class); annotation.param("value", String.valueOf(minimum)); } if (maximum != null) { - JAnnotationUse annotation = field.annotate(DecimalMax.class); + JAnnotationUse annotation = getter.annotate(DecimalMax.class); annotation.param("value", String.valueOf(maximum)); } } diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue248RulesTest.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue248RulesTest.java new file mode 100644 index 00000000..82601ebc --- /dev/null +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/github/Issue248RulesTest.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.1 + */ +public class Issue248RulesTest extends GitHubAbstractRuleTestBase { + + @Test + public void verify_inherited_properties() throws Exception { + loadRaml("issue-248.raml"); + rule = new Spring4ControllerDecoratorRule(); + rule.apply(getControllerMetadata(), jCodeModel); + verifyGeneratedCode("Issue248Spring4ControllerDecorator"); + } +} diff --git a/src/test/resources/ramls/github/issue-248.raml b/src/test/resources/ramls/github/issue-248.raml new file mode 100644 index 00000000..10b50e1c --- /dev/null +++ b/src/test/resources/ramls/github/issue-248.raml @@ -0,0 +1,36 @@ +#%RAML 1.0 + +title: Problem overriding inherited properties + +types: + Person: + type: object + properties: + notOverriden: string + firstName: + type: string + maxLength: 5 + User: + type: Person + properties: + userId: integer + firstName: + default: Alex + minLength: 3 + pattern: ^[a-zA-Z]+$ + description: Some property description + example: Aleks + example: | + { + "notOverriden": "xx", + "firstName": "Aleks", + "userId": 23 + } + +/users: + get: + responses: + 200: + body: + application/json: + type: User \ No newline at end of file diff --git a/src/test/resources/validations/github/Issue248Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue248Spring4ControllerDecorator.java.txt new file mode 100644 index 00000000..44029820 --- /dev/null +++ b/src/test/resources/validations/github/Issue248Spring4ControllerDecorator.java.txt @@ -0,0 +1,269 @@ +-----------------------------------com.gen.test.model.Person.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class Person implements Serializable +{ + + protected String notOverriden; + protected String firstName; + + /** + * Creates a new Person. + * + */ + public Person() { + super(); + } + + /** + * Creates a new Person. + * + */ + public Person(String notOverriden, String firstName) { + super(); + this.notOverriden = notOverriden; + this.firstName = firstName; + } + + /** + * Returns the notOverriden. + * + * @return + * notOverriden + */ + @NotNull + public String getNotOverriden() { + return notOverriden; + } + + /** + * Set the notOverriden. + * + * @param notOverriden + * the new notOverriden + */ + public void setNotOverriden(String notOverriden) { + this.notOverriden = notOverriden; + } + + /** + * Returns the firstName. + * + * @return + * firstName + */ + @NotNull + @Size(max = 5) + public String getFirstName() { + return firstName; + } + + /** + * Set the firstName. + * + * @param firstName + * the new firstName + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public int hashCode() { + return new HashCodeBuilder().append(notOverriden).append(firstName).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + Person otherObject = ((Person) other); + return new EqualsBuilder().append(notOverriden, otherObject.notOverriden).append(firstName, otherObject.firstName).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).append("notOverriden", notOverriden).append("firstName", firstName).toString(); + } + +} +-----------------------------------com.gen.test.model.User.java----------------------------------- + +package com.gen.test.model; + +import java.io.Serializable; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; + +public class User + extends Person + implements Serializable +{ + + protected Long userId; + + /** + * Creates a new User. + * + */ + public User() { + super(); + } + + /** + * Creates a new User. + * + */ + public User(String notOverriden, String firstName, Long userId) { + super(notOverriden, firstName); + this.userId = userId; + } + + /** + * Returns the notOverriden. + * + * @return + * notOverriden + */ + @NotNull + public String getNotOverriden() { + return notOverriden; + } + + /** + * Returns the firstName. + * + * @return + * firstName + */ + @NotNull + @Pattern(regexp = "^[a-zA-Z]+$") + @Size(min = 3) + public String getFirstName() { + if (firstName == null) { + return "Alex"; + } + return firstName; + } + + /** + * Returns the userId. + * + * @return + * userId + */ + @NotNull + public Long getUserId() { + return userId; + } + + /** + * Set the userId. + * + * @param userId + * the new userId + */ + public void setUserId(Long userId) { + this.userId = userId; + } + + public int hashCode() { + return new HashCodeBuilder().appendSuper(super.hashCode()).append(userId).toHashCode(); + } + + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other == this) { + return true; + } + if (this.getClass()!= other.getClass()) { + return false; + } + User otherObject = ((User) other); + return new EqualsBuilder().appendSuper(super.equals(otherObject)).append(userId, otherObject.userId).isEquals(); + } + + public String toString() { + return new ToStringBuilder(this).appendSuper(super.toString()).append("userId", userId).toString(); + } + +} +-----------------------------------com.gen.test.UserController.java----------------------------------- + +package com.gen.test; + +import com.gen.test.model.User; +import org.springframework.http.ResponseEntity; + + +/** + * No description + * (Generated with springmvc-raml-parser v.@project.version@) + * + */ +public interface UserController { + + + /** + * No description + * + */ + public ResponseEntity getUser(); + +} +-----------------------------------com.gen.test.UserControllerDecorator.java----------------------------------- + +package com.gen.test; + +import com.gen.test.model.User; +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/users") +@Validated +public class UserControllerDecorator + implements UserController +{ + + @Autowired + private UserController userControllerDelegate; + + /** + * No description + * + */ + @RequestMapping(value = "", method = RequestMethod.GET) + public ResponseEntity getUser() { + return this.userControllerDelegate.getUser(); + } + +} From 4aafe8d373f8dd00cf57d6cc6861caf2ab150a76 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Wed, 18 Apr 2018 10:52:40 +0200 Subject: [PATCH 2/7] Added STS4 files to gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 47fbb582..b90b3211 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ target /iconv.dll .idea -/.recommenders \ No newline at end of file +/.recommenders +.sts4-cache/ From 9a971c50376879cd39b182ba6c84c91cb2d900bf Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Wed, 18 Apr 2018 11:34:03 +0200 Subject: [PATCH 3/7] Update raml-parser-2 version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 95b91507..f2e73d19 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 1.6.2 3.3.9 0.5.1 - 1.0.20-SNAPSHOT + 1.0.21-SNAPSHOT 1.1.6.RELEASE 2.6 From 6a8ac74308db8833b625c3966cf1c29cf4200a95 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Wed, 18 Apr 2018 11:37:00 +0200 Subject: [PATCH 4/7] Formatted sample Maven code --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index db6a84c8..8f530d2f 100644 --- a/README.md +++ b/README.md @@ -48,23 +48,23 @@ Then simply include the following code in the POM of the project you wish to gen com.phoenixnap.oss springmvc-raml-plugin - x.x.x + 2.x.x {path.to.raml.file} - {path.to.schema.directory||schema.absolute.url} - /src/generated + {path.to.schema.directory||schema.absolute.url} + /src/generated false com.gen.wow /api true - - false - ... - - false - com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerStubRule - - + + false + ... + + false + com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerStubRule + + From 39dcda819be4a30ac8b5a7d16fc59bfc5b19b3f1 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Wed, 18 Apr 2018 15:34:29 +0200 Subject: [PATCH 5/7] Fixed tests --- .../raml2code/raml/RamlInterpreterTest.java | 76 +++++++++++++++---- ...ecoratorRequestBodyWithValidation.java.txt | 8 +- ...nterfaceRequestBodyWithValidation.java.txt | 8 +- ...BaseStubRequestBodyWithValidation.java.txt | 8 +- .../validations/FeignClient.java.txt | 12 +-- ...ngLogicSpring4ControllerDecorator.java.txt | 12 +-- .../RamlSchemaWithInheritanceTest.java.txt | 49 ++++++++++-- ...ionTypeSpring4ControllerInterface.java.txt | 28 +++---- ...llerInterfaceWithShortcutMappings.java.txt | 12 +-- .../Issue158Spring4ControllerStub.java.txt | 8 +- .../Issue159Spring4ControllerStub.java.txt | 12 +-- .../Issue172Spring4ControllerStub.java.txt | 10 +-- .../github/Issue173Spring4Decorator.java.txt | 6 +- .../Issue181Spring4ControllerStub.java.txt | 8 +- .../Issue183Spring4ControllerStub.java.txt | 28 +++---- .../Issue193-validqueryparameters.java.txt | 16 ++-- .../Issue199Spring4ControllerStub.java.txt | 46 ++++++++--- .../Issue208Spring4ControllerStub.java.txt | 46 ++++++++--- .../Issue210Spring4ControllerStub.java.txt | 8 +- .../Issue211SpringInterfaceForRaml10.java.txt | 8 +- .../Issue212-1-pathvariablesnaming.java.txt | 8 +- .../Issue212-2-pathvariablesnaming.java.txt | 8 +- .../Issue212-3-pathvariablesnaming.java.txt | 8 +- .../Issue224Spring4ControllerStub.java.txt | 40 +++++----- ...ssue235Spring4ControllerDecorator.java.txt | 8 +- ...Issue241Spring4RestTemplateClient.java.txt | 4 +- ...ssue242Spring4ControllerDecorator.java.txt | 8 +- ...isabledSpring4ControllerDecorator.java.txt | 9 +-- ...EnabledSpring4ControllerDecorator.java.txt | 14 ++-- ...ssue247Spring4ControllerDecorator.java.txt | 8 +- .../github/Issue61BaseClient.java.txt | 4 +- 31 files changed, 329 insertions(+), 199 deletions(-) diff --git a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/RamlInterpreterTest.java b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/RamlInterpreterTest.java index 32fc6945..83ab48c2 100644 --- a/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/RamlInterpreterTest.java +++ b/src/test/java/com/phoenixnap/oss/ramlplugin/raml2code/raml/RamlInterpreterTest.java @@ -18,8 +18,13 @@ import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import javax.validation.constraints.DecimalMax; import javax.validation.constraints.DecimalMin; @@ -39,6 +44,7 @@ import com.phoenixnap.oss.ramlplugin.raml2code.data.ApiResourceMetadata; import com.phoenixnap.oss.ramlplugin.raml2code.exception.InvalidRamlResourceException; import com.phoenixnap.oss.ramlplugin.raml2code.helpers.CodeModelHelper; +import com.phoenixnap.oss.ramlplugin.raml2code.helpers.NamingHelper; import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlParser; import com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper; import com.phoenixnap.oss.ramlplugin.raml2code.plugin.Config; @@ -125,9 +131,9 @@ public void checkJSR303_RequiredDefaultsToTrue() { JDefinedClass manager = (JDefinedClass) CodeModelHelper.findFirstClassBySimpleName(jCodeModel, "Manager"); JDefinedClass department = (JDefinedClass) CodeModelHelper.findFirstClassBySimpleName(jCodeModel, "Department"); - checkIfFieldContainsAnnotation(true, manager, NotNull.class); - checkIfFieldContainsAnnotation(true, person, NotNull.class); - checkIfFieldContainsAnnotation(true, department, NotNull.class); + checkIfGetterContainsAnnotation(true, manager, NotNull.class, "firstname", "lastname", "id", "department", "clearanceLevel"); + checkIfGetterContainsAnnotation(true, person, NotNull.class, "firstname", "lastname", "id"); + checkIfGetterContainsAnnotation(true, department, NotNull.class, "name"); } @@ -150,10 +156,10 @@ public void checkJSR303() { JDefinedClass validation = (JDefinedClass) CodeModelHelper.findFirstClassBySimpleName(jCodeModel, "Validation"); - checkIfFieldContainsAnnotation(true, validation, NotNull.class, "lastname", "pattern", "length", "id", "anEnum", "anotherEnum"); - checkIfFieldContainsAnnotation(false, validation, NotNull.class, "firstname", "minLength"); - checkIfFieldContainsAnnotation(true, validation, Size.class, "length", "minLength"); - checkIfFieldContainsAnnotation(true, validation, Pattern.class, "pattern"); + checkIfGetterContainsAnnotation(true, validation, NotNull.class, "lastname", "pattern", "length", "id", "anEnum", "anotherEnum"); + checkIfGetterContainsAnnotation(false, validation, NotNull.class, "firstname", "minLength"); + checkIfGetterContainsAnnotation(true, validation, Size.class, "length", "minLength"); + checkIfGetterContainsAnnotation(true, validation, Pattern.class, "pattern"); checkIfAnnotationHasParameter(validation, Size.class, "length", "min"); checkIfAnnotationHasParameter(validation, Size.class, "length", "max"); @@ -177,7 +183,7 @@ public void checkJSR303() { } private void checkIfAnnotationHasParameter(JDefinedClass classToCheck, Class annotationClass, String field, String param) { - JAnnotationUse annotation = getAnnotationForField(classToCheck, annotationClass, field); + JAnnotationUse annotation = getAnnotationForGetter(classToCheck, annotationClass, field); assertThat(annotation, is(notNullValue())); JAnnotationValue annotationParam = annotation.getAnnotationMembers().get(param); assertThat(annotationParam, is(notNullValue())); @@ -216,6 +222,27 @@ private void checkIfFieldContainsAnnotation(boolean expected, JDefinedClass clas } } + private void checkIfGetterContainsAnnotation(boolean expected, JDefinedClass classToCheck, Class annotationClass, String... fields) { + List expectedMethodNames = Arrays.asList(fields).stream().map(field -> "get" + NamingHelper.convertToClassName(field)) + .collect(Collectors.toList()); + Map actualMethods = classToCheck.methods().stream() + .collect(Collectors.toMap(method -> method.name(), method -> method)); + + for (String expectedMethodName : expectedMethodNames) { + if (actualMethods.keySet().contains(expectedMethodName)) { + boolean found = false; + for (JAnnotationUse annotation : actualMethods.get(expectedMethodName).annotations()) { + if (annotation.getAnnotationClass().name().equals(annotationClass.getSimpleName())) { + found = true; + } + } + assertThat(found, is(expected)); + } else { + fail(); + } + } + } + private JAnnotationUse getAnnotationForField(JDefinedClass classToCheck, Class annotationClass, String field) { for (JFieldVar fieldVar : classToCheck.fields().values()) { if (fieldVar.name().equals(field)) { @@ -229,6 +256,19 @@ private JAnnotationUse getAnnotationForField(JDefinedClass classToCheck, Class annotationClass, String field) { + + Optional methodOptional = classToCheck.methods().stream() + .filter(method -> method.name().equals("get" + NamingHelper.convertToClassName(field))).findFirst(); + if (methodOptional.isPresent()) { + Optional findFirst = methodOptional.get().annotations().stream() + .filter(annotation -> annotation.getAnnotationClass().name().equals(annotationClass.getSimpleName())).findFirst(); + return findFirst.get(); + } + + return null; + } + @Test public void interpretNestedArrays() { assertThat(AbstractRuleTestBase.RAML, is(notNullValue())); @@ -429,23 +469,23 @@ public void checkTypeOfDates() throws Exception { JFieldVar field = getField(pojo, "dateO"); assertThat(field.type().fullName(), is("java.util.Date")); - assertAnnotations(field, "yyyy-MM-dd"); + assertDateFormatAnnotation(field, "yyyy-MM-dd"); field = getField(pojo, "timeO"); assertThat(field.type().fullName(), is("java.util.Date")); - assertAnnotations(field, "HH:mm:ss"); + assertDateFormatAnnotation(field, "HH:mm:ss"); field = getField(pojo, "dateTO"); assertThat(field.type().fullName(), is("java.util.Date")); - assertAnnotations(field, "yyyy-MM-dd'T'HH:mm:ss"); + assertDateFormatAnnotation(field, "yyyy-MM-dd'T'HH:mm:ss"); field = getField(pojo, "dateT"); assertThat(field.type().fullName(), is("java.util.Date")); - assertAnnotations(field, "yyyy-MM-dd'T'HH:mm:ssXXX"); + assertDateFormatAnnotation(field, "yyyy-MM-dd'T'HH:mm:ssXXX"); field = getField(pojo, "datetimeRFC2616"); assertThat(field.type().fullName(), is("java.util.Date")); - assertAnnotations(field, "EEE, dd MMM yyyy HH:mm:ss z"); + assertDateFormatAnnotation(field, "EEE, dd MMM yyyy HH:mm:ss z"); } private void assertAnnotations(JFieldVar field, String expectedPattern) throws Exception { @@ -464,6 +504,16 @@ private void assertAnnotations(JFieldVar field, String expectedPattern) throws E } } + private void assertDateFormatAnnotation(JFieldVar field, String expectedPattern) throws Exception { + assertEquals(1, field.annotations().size()); + Optional optionalAnnotation = field.annotations().stream().findFirst(); + if (optionalAnnotation.isPresent() && JsonFormat.class.getName().equals(optionalAnnotation.get().getAnnotationClass().fullName())) { + assertPatternValue(optionalAnnotation.get(), expectedPattern); + } else { + fail(); + } + } + private void assertPatternValue(JAnnotationUse jAnnotationUse, String expectedPattern) throws Exception { JAnnotationValue jAnnotationValue = jAnnotationUse.getAnnotationMembers().get("pattern"); Field value = jAnnotationValue.getClass().getDeclaredField("value"); diff --git a/src/test/resources/validations/BaseDecoratorRequestBodyWithValidation.java.txt b/src/test/resources/validations/BaseDecoratorRequestBodyWithValidation.java.txt index a0eb6879..19c0e92a 100644 --- a/src/test/resources/validations/BaseDecoratorRequestBodyWithValidation.java.txt +++ b/src/test/resources/validations/BaseDecoratorRequestBodyWithValidation.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Object implements Serializable { - @NotNull - private Long stuffId; - @NotNull - private Long coolerId; + protected Long stuffId; + protected Long coolerId; /** * Creates a new Object. @@ -40,6 +38,7 @@ public class Object implements Serializable * @return * stuffId */ + @NotNull public Long getStuffId() { return stuffId; } @@ -60,6 +59,7 @@ public class Object implements Serializable * @return * coolerId */ + @NotNull public Long getCoolerId() { return coolerId; } diff --git a/src/test/resources/validations/BaseInterfaceRequestBodyWithValidation.java.txt b/src/test/resources/validations/BaseInterfaceRequestBodyWithValidation.java.txt index 526045ef..be119dd7 100644 --- a/src/test/resources/validations/BaseInterfaceRequestBodyWithValidation.java.txt +++ b/src/test/resources/validations/BaseInterfaceRequestBodyWithValidation.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Object implements Serializable { - @NotNull - private Long stuffId; - @NotNull - private Long coolerId; + protected Long stuffId; + protected Long coolerId; /** * Creates a new Object. @@ -40,6 +38,7 @@ public class Object implements Serializable * @return * stuffId */ + @NotNull public Long getStuffId() { return stuffId; } @@ -60,6 +59,7 @@ public class Object implements Serializable * @return * coolerId */ + @NotNull public Long getCoolerId() { return coolerId; } diff --git a/src/test/resources/validations/BaseStubRequestBodyWithValidation.java.txt b/src/test/resources/validations/BaseStubRequestBodyWithValidation.java.txt index 0471674c..acae5850 100644 --- a/src/test/resources/validations/BaseStubRequestBodyWithValidation.java.txt +++ b/src/test/resources/validations/BaseStubRequestBodyWithValidation.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Object implements Serializable { - @NotNull - private Long stuffId; - @NotNull - private Long coolerId; + protected Long stuffId; + protected Long coolerId; /** * Creates a new Object. @@ -40,6 +38,7 @@ public class Object implements Serializable * @return * stuffId */ + @NotNull public Long getStuffId() { return stuffId; } @@ -60,6 +59,7 @@ public class Object implements Serializable * @return * coolerId */ + @NotNull public Long getCoolerId() { return coolerId; } diff --git a/src/test/resources/validations/FeignClient.java.txt b/src/test/resources/validations/FeignClient.java.txt index 9545c8e8..763b0089 100644 --- a/src/test/resources/validations/FeignClient.java.txt +++ b/src/test/resources/validations/FeignClient.java.txt @@ -11,12 +11,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Song implements Serializable { - @NotNull - private String songId; - @NotNull - private String songTitle; - @NotNull - private String artist; + protected String songId; + protected String songTitle; + protected String artist; /** * Creates a new Song. @@ -43,6 +40,7 @@ public class Song implements Serializable * @return * songId */ + @NotNull public String getSongId() { return songId; } @@ -63,6 +61,7 @@ public class Song implements Serializable * @return * songTitle */ + @NotNull public String getSongTitle() { return songTitle; } @@ -83,6 +82,7 @@ public class Song implements Serializable * @return * artist */ + @NotNull public String getArtist() { return artist; } diff --git a/src/test/resources/validations/NamingLogicSpring4ControllerDecorator.java.txt b/src/test/resources/validations/NamingLogicSpring4ControllerDecorator.java.txt index 60a4797c..882df1ad 100644 --- a/src/test/resources/validations/NamingLogicSpring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/NamingLogicSpring4ControllerDecorator.java.txt @@ -11,8 +11,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Message implements Serializable { - @NotNull - private String value; + protected String value; /** * Creates a new Message. @@ -37,6 +36,7 @@ public class Message implements Serializable * @return * value */ + @NotNull public String getValue() { return value; } @@ -87,10 +87,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class MessageSubResource implements Serializable { - @NotNull - private String title; - @NotNull - private String desc; + protected String title; + protected String desc; /** * Creates a new MessageSubResource. @@ -116,6 +114,7 @@ public class MessageSubResource implements Serializable * @return * title */ + @NotNull public String getTitle() { return title; } @@ -136,6 +135,7 @@ public class MessageSubResource implements Serializable * @return * desc */ + @NotNull public String getDesc() { return desc; } diff --git a/src/test/resources/validations/RamlSchemaWithInheritanceTest.java.txt b/src/test/resources/validations/RamlSchemaWithInheritanceTest.java.txt index 17e7b02c..8ae1bb51 100644 --- a/src/test/resources/validations/RamlSchemaWithInheritanceTest.java.txt +++ b/src/test/resources/validations/RamlSchemaWithInheritanceTest.java.txt @@ -11,12 +11,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Person implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; - @NotNull - private Double age; + protected String firstName; + protected String lastName; + protected Double age; /** * Creates a new Person. @@ -43,6 +40,7 @@ public class Person implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -63,6 +61,7 @@ public class Person implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } @@ -83,6 +82,7 @@ public class Person implements Serializable * @return * age */ + @NotNull public Double getAge() { return age; } @@ -135,8 +135,7 @@ public class User implements Serializable { - @NotNull - private String username; + protected String username; /** * Creates a new User. @@ -155,12 +154,46 @@ public class User this.username = username; } + /** + * Returns the firstName. + * + * @return + * firstName + */ + @NotNull + public String getFirstName() { + return firstName; + } + + /** + * Returns the lastName. + * + * @return + * lastName + */ + @NotNull + public String getLastName() { + return lastName; + } + + /** + * Returns the age. + * + * @return + * age + */ + @NotNull + public Double getAge() { + return age; + } + /** * Returns the username. * * @return * username */ + @NotNull public String getUsername() { return username; } diff --git a/src/test/resources/validations/RamlWithUnionTypeSpring4ControllerInterface.java.txt b/src/test/resources/validations/RamlWithUnionTypeSpring4ControllerInterface.java.txt index c5e70170..60d8c300 100644 --- a/src/test/resources/validations/RamlWithUnionTypeSpring4ControllerInterface.java.txt +++ b/src/test/resources/validations/RamlWithUnionTypeSpring4ControllerInterface.java.txt @@ -12,12 +12,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Device implements Serializable { - @NotNull - @Valid - private com.gen.test.model.Phone Phone; - @NotNull - @Valid - private com.gen.test.model.Notebook Notebook; + protected com.gen.test.model.Phone Phone; + protected com.gen.test.model.Notebook Notebook; /** * Creates a new Device. @@ -43,6 +39,8 @@ public class Device implements Serializable * @return * Phone */ + @NotNull + @Valid public com.gen.test.model.Phone getPhone() { return Phone; } @@ -63,6 +61,8 @@ public class Device implements Serializable * @return * Notebook */ + @NotNull + @Valid public com.gen.test.model.Notebook getNotebook() { return Notebook; } @@ -113,10 +113,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Notebook implements Serializable { - @NotNull - private String manufacturer; - @NotNull - private Double numberOfUSBPorts; + protected String manufacturer; + protected Double numberOfUSBPorts; /** * Creates a new Notebook. @@ -142,6 +140,7 @@ public class Notebook implements Serializable * @return * manufacturer */ + @NotNull public String getManufacturer() { return manufacturer; } @@ -162,6 +161,7 @@ public class Notebook implements Serializable * @return * numberOfUSBPorts */ + @NotNull public Double getNumberOfUSBPorts() { return numberOfUSBPorts; } @@ -212,10 +212,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Phone implements Serializable { - @NotNull - private String manufacturer; - @NotNull - private Double numberOfSIMCards; + protected String manufacturer; + protected Double numberOfSIMCards; /** * Creates a new Phone. @@ -241,6 +239,7 @@ public class Phone implements Serializable * @return * manufacturer */ + @NotNull public String getManufacturer() { return manufacturer; } @@ -261,6 +260,7 @@ public class Phone implements Serializable * @return * numberOfSIMCards */ + @NotNull public Double getNumberOfSIMCards() { return numberOfSIMCards; } diff --git a/src/test/resources/validations/Spring4ControllerInterfaceWithShortcutMappings.java.txt b/src/test/resources/validations/Spring4ControllerInterfaceWithShortcutMappings.java.txt index edf007c2..85597ecd 100644 --- a/src/test/resources/validations/Spring4ControllerInterfaceWithShortcutMappings.java.txt +++ b/src/test/resources/validations/Spring4ControllerInterfaceWithShortcutMappings.java.txt @@ -11,12 +11,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Manager implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; - @NotNull - private String departman; + protected String firstName; + protected String lastName; + protected String departman; /** * Creates a new Manager. @@ -43,6 +40,7 @@ public class Manager implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -63,6 +61,7 @@ public class Manager implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } @@ -83,6 +82,7 @@ public class Manager implements Serializable * @return * departman */ + @NotNull public String getDepartman() { return departman; } diff --git a/src/test/resources/validations/github/Issue158Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue158Spring4ControllerStub.java.txt index 6ef54064..eaf0e15a 100644 --- a/src/test/resources/validations/github/Issue158Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue158Spring4ControllerStub.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new User. @@ -40,6 +38,7 @@ public class User implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -60,6 +59,7 @@ public class User implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue159Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue159Spring4ControllerStub.java.txt index 1f264444..c06eba77 100644 --- a/src/test/resources/validations/github/Issue159Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue159Spring4ControllerStub.java.txt @@ -11,12 +11,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Manager implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; - @NotNull - private String departman; + protected String firstName; + protected String lastName; + protected String departman; /** * Creates a new Manager. @@ -43,6 +40,7 @@ public class Manager implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -63,6 +61,7 @@ public class Manager implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } @@ -83,6 +82,7 @@ public class Manager implements Serializable * @return * departman */ + @NotNull public String getDepartman() { return departman; } diff --git a/src/test/resources/validations/github/Issue172Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue172Spring4ControllerStub.java.txt index 8b9303d1..05ef8a19 100644 --- a/src/test/resources/validations/github/Issue172Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue172Spring4ControllerStub.java.txt @@ -16,15 +16,12 @@ public class EventCreate implements Serializable * The Name of the event * */ - @NotNull - private String eventName; + protected String eventName; /** * The event payload * */ - @NotNull - @Valid - private EventPayload eventPayload; + protected EventPayload eventPayload; /** * Creates a new EventCreate. @@ -50,6 +47,7 @@ public class EventCreate implements Serializable * @return * eventName */ + @NotNull public String getEventName() { return eventName; } @@ -70,6 +68,8 @@ public class EventCreate implements Serializable * @return * eventPayload */ + @NotNull + @Valid public EventPayload getEventPayload() { return eventPayload; } diff --git a/src/test/resources/validations/github/Issue173Spring4Decorator.java.txt b/src/test/resources/validations/github/Issue173Spring4Decorator.java.txt index 5cc23429..c21cc13d 100644 --- a/src/test/resources/validations/github/Issue173Spring4Decorator.java.txt +++ b/src/test/resources/validations/github/Issue173Spring4Decorator.java.txt @@ -16,9 +16,7 @@ public class TheResourceId implements Serializable * The UUID of this resource. * */ - @NotNull - @Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$") - private String id; + protected String id; /** * Creates a new TheResourceId. @@ -43,6 +41,8 @@ public class TheResourceId implements Serializable * @return * id */ + @NotNull + @Pattern(regexp = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$") public String getId() { return id; } diff --git a/src/test/resources/validations/github/Issue181Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue181Spring4ControllerStub.java.txt index f56c9e91..44dd6e02 100644 --- a/src/test/resources/validations/github/Issue181Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue181Spring4ControllerStub.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new User. @@ -40,6 +38,7 @@ public class User implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -60,6 +59,7 @@ public class User implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue183Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue183Spring4ControllerStub.java.txt index 6f1970b3..dc1f9afe 100644 --- a/src/test/resources/validations/github/Issue183Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue183Spring4ControllerStub.java.txt @@ -13,26 +13,19 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Data implements Serializable { - @NotNull - private String name; - @NotNull + protected String name; @JsonFormat(pattern = "yyyy-MM-dd") - private Date dateOnly; - @NotNull + protected Date dateOnly; @JsonFormat(pattern = "HH:mm:ss") - private Date timeOnly; - @NotNull + protected Date timeOnly; @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") - private Date datetimeOnly; - @NotNull + protected Date datetimeOnly; @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX") - private Date datetimeNoFormat; - @NotNull + protected Date datetimeNoFormat; @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssXXX") - private Date datetimeRFC3339; - @NotNull + protected Date datetimeRFC3339; @JsonFormat(pattern = "EEE, dd MMM yyyy HH:mm:ss z") - private Date datetimeRFC2616; + protected Date datetimeRFC2616; /** * Creates a new Data. @@ -63,6 +56,7 @@ public class Data implements Serializable * @return * name */ + @NotNull public String getName() { return name; } @@ -83,6 +77,7 @@ public class Data implements Serializable * @return * dateOnly */ + @NotNull public Date getDateOnly() { return dateOnly; } @@ -103,6 +98,7 @@ public class Data implements Serializable * @return * timeOnly */ + @NotNull public Date getTimeOnly() { return timeOnly; } @@ -123,6 +119,7 @@ public class Data implements Serializable * @return * datetimeOnly */ + @NotNull public Date getDatetimeOnly() { return datetimeOnly; } @@ -143,6 +140,7 @@ public class Data implements Serializable * @return * datetimeNoFormat */ + @NotNull public Date getDatetimeNoFormat() { return datetimeNoFormat; } @@ -163,6 +161,7 @@ public class Data implements Serializable * @return * datetimeRFC3339 */ + @NotNull public Date getDatetimeRFC3339() { return datetimeRFC3339; } @@ -183,6 +182,7 @@ public class Data implements Serializable * @return * datetimeRFC2616 */ + @NotNull public Date getDatetimeRFC2616() { return datetimeRFC2616; } diff --git a/src/test/resources/validations/github/Issue193-validqueryparameters.java.txt b/src/test/resources/validations/github/Issue193-validqueryparameters.java.txt index ad7ba0d4..b25bd0b0 100644 --- a/src/test/resources/validations/github/Issue193-validqueryparameters.java.txt +++ b/src/test/resources/validations/github/Issue193-validqueryparameters.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Booking implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new Booking. @@ -40,6 +38,7 @@ public class Booking implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -60,6 +59,7 @@ public class Booking implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } @@ -110,10 +110,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class BookingQuery implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new BookingQuery. @@ -139,6 +137,7 @@ public class BookingQuery implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -159,6 +158,7 @@ public class BookingQuery implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue199Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue199Spring4ControllerStub.java.txt index f5e107f4..e253533a 100644 --- a/src/test/resources/validations/github/Issue199Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue199Spring4ControllerStub.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Employee implements Serializable { - @NotNull - private String name; - @NotNull - private String skill; + protected String name; + protected String skill; /** * Creates a new Employee. @@ -40,6 +38,7 @@ public class Employee implements Serializable * @return * name */ + @NotNull public String getName() { return name; } @@ -60,6 +59,7 @@ public class Employee implements Serializable * @return * skill */ + @NotNull public String getSkill() { return skill; } @@ -112,8 +112,7 @@ public class EmployeeExtended implements Serializable { - @NotNull - private String job; + protected String job; /** * Creates a new EmployeeExtended. @@ -132,12 +131,35 @@ public class EmployeeExtended this.job = job; } + /** + * Returns the name. + * + * @return + * name + */ + @NotNull + public String getName() { + return name; + } + + /** + * Returns the skill. + * + * @return + * skill + */ + @NotNull + public String getSkill() { + return skill; + } + /** * Returns the job. * * @return * job */ + @NotNull public String getJob() { return job; } @@ -191,9 +213,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class EmployeeRq implements Serializable { - @NotNull - @Valid - private List employees = new ArrayList(); + protected List employees = new ArrayList(); /** * Creates a new EmployeeRq. @@ -218,6 +238,8 @@ public class EmployeeRq implements Serializable * @return * employees */ + @NotNull + @Valid public List getEmployees() { return employees; } @@ -271,9 +293,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class EmployeeRs implements Serializable { - @NotNull - @Valid - private List employees = new ArrayList(); + protected List employees = new ArrayList(); /** * Creates a new EmployeeRs. @@ -298,6 +318,8 @@ public class EmployeeRs implements Serializable * @return * employees */ + @NotNull + @Valid public List getEmployees() { return employees; } diff --git a/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt index 3e8764aa..99391324 100644 --- a/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue208Spring4ControllerStub.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Group implements Serializable { - @NotNull - private Long id; - @NotNull - private String name; + protected Long id; + protected String name; /** * Creates a new Group. @@ -40,6 +38,7 @@ public class Group implements Serializable * @return * id */ + @NotNull public Long getId() { return id; } @@ -60,6 +59,7 @@ public class Group implements Serializable * @return * name */ + @NotNull public String getName() { return name; } @@ -112,9 +112,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Product implements Serializable { - private String id; - @Valid - private List groups = new ArrayList(); + protected String id; + protected List groups = new ArrayList(); /** * Creates a new Product. @@ -160,6 +159,7 @@ public class Product implements Serializable * @return * groups */ + @Valid public List getGroups() { return groups; } @@ -202,7 +202,9 @@ public class Product implements Serializable 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; @@ -213,8 +215,7 @@ public class ProductUpdate implements Serializable { - @NotNull - private Double locationId; + protected Double locationId; /** * Creates a new ProductUpdate. @@ -228,17 +229,42 @@ public class ProductUpdate * Creates a new ProductUpdate. * */ - public ProductUpdate(String id, List groups, Double locationId) { + public ProductUpdate(String id, List groups, Double locationId) { super(id, groups); this.locationId = locationId; } + /** + * Returns the id. + * + * @return + * id + */ + public String getId() { + return id; + } + + /** + * Returns the groups. + * + * @return + * groups + */ + @Valid + public List getGroups() { + if (groups == null) { + return new ArrayList(); + } + return groups; + } + /** * Returns the locationId. * * @return * locationId */ + @NotNull public Double getLocationId() { return locationId; } diff --git a/src/test/resources/validations/github/Issue210Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue210Spring4ControllerStub.java.txt index a9a13f6c..0600100f 100644 --- a/src/test/resources/validations/github/Issue210Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue210Spring4ControllerStub.java.txt @@ -13,10 +13,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - private List nickNames = new ArrayList(); - @NotNull - private List orderNumbers = new ArrayList(); + protected List nickNames = new ArrayList(); + protected List orderNumbers = new ArrayList(); /** * Creates a new User. @@ -42,6 +40,7 @@ public class User implements Serializable * @return * nickNames */ + @NotNull public List getNickNames() { return nickNames; } @@ -62,6 +61,7 @@ public class User implements Serializable * @return * orderNumbers */ + @NotNull public List getOrderNumbers() { return orderNumbers; } diff --git a/src/test/resources/validations/github/Issue211SpringInterfaceForRaml10.java.txt b/src/test/resources/validations/github/Issue211SpringInterfaceForRaml10.java.txt index cb36bf50..fe0d9570 100644 --- a/src/test/resources/validations/github/Issue211SpringInterfaceForRaml10.java.txt +++ b/src/test/resources/validations/github/Issue211SpringInterfaceForRaml10.java.txt @@ -13,10 +13,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - private List nickNames = new ArrayList(); - @NotNull - private List orderNumbers = new ArrayList(); + protected List nickNames = new ArrayList(); + protected List orderNumbers = new ArrayList(); /** * Creates a new User. @@ -42,6 +40,7 @@ public class User implements Serializable * @return * nickNames */ + @NotNull public List getNickNames() { return nickNames; } @@ -62,6 +61,7 @@ public class User implements Serializable * @return * orderNumbers */ + @NotNull public List getOrderNumbers() { return orderNumbers; } diff --git a/src/test/resources/validations/github/Issue212-1-pathvariablesnaming.java.txt b/src/test/resources/validations/github/Issue212-1-pathvariablesnaming.java.txt index 57753543..fb3c12a7 100644 --- a/src/test/resources/validations/github/Issue212-1-pathvariablesnaming.java.txt +++ b/src/test/resources/validations/github/Issue212-1-pathvariablesnaming.java.txt @@ -12,10 +12,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Booking implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new Booking. @@ -41,6 +39,7 @@ public class Booking implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -61,6 +60,7 @@ public class Booking implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue212-2-pathvariablesnaming.java.txt b/src/test/resources/validations/github/Issue212-2-pathvariablesnaming.java.txt index dcad32e4..51eda0ca 100644 --- a/src/test/resources/validations/github/Issue212-2-pathvariablesnaming.java.txt +++ b/src/test/resources/validations/github/Issue212-2-pathvariablesnaming.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Booking implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new Booking. @@ -40,6 +38,7 @@ public class Booking implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -60,6 +59,7 @@ public class Booking implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue212-3-pathvariablesnaming.java.txt b/src/test/resources/validations/github/Issue212-3-pathvariablesnaming.java.txt index b07bb473..67909f8c 100644 --- a/src/test/resources/validations/github/Issue212-3-pathvariablesnaming.java.txt +++ b/src/test/resources/validations/github/Issue212-3-pathvariablesnaming.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Booking implements Serializable { - @NotNull - private String firstName; - @NotNull - private String lastName; + protected String firstName; + protected String lastName; /** * Creates a new Booking. @@ -40,6 +38,7 @@ public class Booking implements Serializable * @return * firstName */ + @NotNull public String getFirstName() { return firstName; } @@ -60,6 +59,7 @@ public class Booking implements Serializable * @return * lastName */ + @NotNull public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue224Spring4ControllerStub.java.txt b/src/test/resources/validations/github/Issue224Spring4ControllerStub.java.txt index 265840f1..dfd70155 100644 --- a/src/test/resources/validations/github/Issue224Spring4ControllerStub.java.txt +++ b/src/test/resources/validations/github/Issue224Spring4ControllerStub.java.txt @@ -14,17 +14,11 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Book implements Serializable { - @NotNull - private Double id; - @NotNull - private String title; - @NotNull - private String author; - @NotNull - @Valid - private Publisher publisher; - @Valid - private List chapters = new ArrayList(); + protected Double id; + protected String title; + protected String author; + protected Publisher publisher; + protected List chapters = new ArrayList(); /** * Creates a new Book. @@ -53,6 +47,7 @@ public class Book implements Serializable * @return * id */ + @NotNull public Double getId() { return id; } @@ -73,6 +68,7 @@ public class Book implements Serializable * @return * title */ + @NotNull public String getTitle() { return title; } @@ -93,6 +89,7 @@ public class Book implements Serializable * @return * author */ + @NotNull public String getAuthor() { return author; } @@ -113,6 +110,8 @@ public class Book implements Serializable * @return * publisher */ + @NotNull + @Valid public Publisher getPublisher() { return publisher; } @@ -133,6 +132,7 @@ public class Book implements Serializable * @return * chapters */ + @Valid public List getChapters() { return chapters; } @@ -183,10 +183,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Chapter implements Serializable { - @NotNull - private String chapTitle; - @NotNull - private Double noPages; + protected String chapTitle; + protected Double noPages; /** * Creates a new Chapter. @@ -212,6 +210,7 @@ public class Chapter implements Serializable * @return * chapTitle */ + @NotNull public String getChapTitle() { return chapTitle; } @@ -232,6 +231,7 @@ public class Chapter implements Serializable * @return * noPages */ + @NotNull public Double getNoPages() { return noPages; } @@ -282,11 +282,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Publisher implements Serializable { - @NotNull - private String name; - @NotNull - private String country; - private Long revenue; + protected String name; + protected String country; + protected Long revenue; /** * Creates a new Publisher. @@ -313,6 +311,7 @@ public class Publisher implements Serializable * @return * name */ + @NotNull public String getName() { return name; } @@ -333,6 +332,7 @@ public class Publisher implements Serializable * @return * country */ + @NotNull public String getCountry() { return country; } diff --git a/src/test/resources/validations/github/Issue235Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue235Spring4ControllerDecorator.java.txt index 6fb150d8..e211a815 100644 --- a/src/test/resources/validations/github/Issue235Spring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/github/Issue235Spring4ControllerDecorator.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class DeleteObject implements Serializable { - @NotNull - private Double id; - @NotNull - private String name; + protected Double id; + protected String name; /** * Creates a new DeleteObject. @@ -40,6 +38,7 @@ public class DeleteObject implements Serializable * @return * id */ + @NotNull public Double getId() { return id; } @@ -60,6 +59,7 @@ public class DeleteObject implements Serializable * @return * name */ + @NotNull public String getName() { return name; } diff --git a/src/test/resources/validations/github/Issue241Spring4RestTemplateClient.java.txt b/src/test/resources/validations/github/Issue241Spring4RestTemplateClient.java.txt index af7db069..1c81903b 100644 --- a/src/test/resources/validations/github/Issue241Spring4RestTemplateClient.java.txt +++ b/src/test/resources/validations/github/Issue241Spring4RestTemplateClient.java.txt @@ -11,8 +11,7 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Client implements Serializable { - @NotNull - private String test; + protected String test; /** * Creates a new Client. @@ -37,6 +36,7 @@ public class Client implements Serializable * @return * test */ + @NotNull public String getTest() { return test; } diff --git a/src/test/resources/validations/github/Issue242Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue242Spring4ControllerDecorator.java.txt index 704d13d7..4336ebdd 100644 --- a/src/test/resources/validations/github/Issue242Spring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/github/Issue242Spring4ControllerDecorator.java.txt @@ -18,10 +18,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class Person implements Serializable { - @NotNull - private String kind; - @NotNull - private String name; + protected String kind; + protected String name; /** * Creates a new Person. @@ -47,6 +45,7 @@ public class Person implements Serializable * @return * kind */ + @NotNull public String getKind() { return kind; } @@ -67,6 +66,7 @@ public class Person implements Serializable * @return * name */ + @NotNull public String getName() { return name; } diff --git a/src/test/resources/validations/github/Issue245DisabledSpring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue245DisabledSpring4ControllerDecorator.java.txt index 282f3e9e..5a8bfe09 100644 --- a/src/test/resources/validations/github/Issue245DisabledSpring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/github/Issue245DisabledSpring4ControllerDecorator.java.txt @@ -10,9 +10,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - private String firstName; - private String lastName; - private String nickName; + protected String firstName; + protected String lastName; + protected String nickName; /** * Creates a new User. @@ -143,8 +143,7 @@ public interface UserController { * No description * */ - public ResponseEntity createUser( - User user); + public ResponseEntity createUser(User user); } -----------------------------------com.gen.test.UserControllerDecorator.java----------------------------------- diff --git a/src/test/resources/validations/github/Issue245EnabledSpring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue245EnabledSpring4ControllerDecorator.java.txt index c908ebae..c19645a8 100644 --- a/src/test/resources/validations/github/Issue245EnabledSpring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/github/Issue245EnabledSpring4ControllerDecorator.java.txt @@ -13,13 +13,9 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - @Size(min = 5, max = 50) - private String firstName; - @NotNull - @Pattern(regexp = "^[a-z]+$") - private String lastName; - private String nickName; + protected String firstName; + protected String lastName; + protected String nickName; /** * Creates a new User. @@ -46,6 +42,8 @@ public class User implements Serializable * @return * firstName */ + @NotNull + @Size(min = 5, max = 50) public String getFirstName() { return firstName; } @@ -66,6 +64,8 @@ public class User implements Serializable * @return * lastName */ + @NotNull + @Pattern(regexp = "^[a-z]+$") public String getLastName() { return lastName; } diff --git a/src/test/resources/validations/github/Issue247Spring4ControllerDecorator.java.txt b/src/test/resources/validations/github/Issue247Spring4ControllerDecorator.java.txt index 3660ece5..58ea8ec7 100644 --- a/src/test/resources/validations/github/Issue247Spring4ControllerDecorator.java.txt +++ b/src/test/resources/validations/github/Issue247Spring4ControllerDecorator.java.txt @@ -11,10 +11,8 @@ import org.apache.commons.lang.builder.ToStringBuilder; public class User implements Serializable { - @NotNull - private Long userId; - @NotNull - private Long salary; + protected Long userId; + protected Long salary; /** * Creates a new User. @@ -40,6 +38,7 @@ public class User implements Serializable * @return * userId */ + @NotNull public Long getUserId() { return userId; } @@ -60,6 +59,7 @@ public class User implements Serializable * @return * salary */ + @NotNull public Long getSalary() { return salary; } diff --git a/src/test/resources/validations/github/Issue61BaseClient.java.txt b/src/test/resources/validations/github/Issue61BaseClient.java.txt index 7c72fa67..ac8b6bf0 100644 --- a/src/test/resources/validations/github/Issue61BaseClient.java.txt +++ b/src/test/resources/validations/github/Issue61BaseClient.java.txt @@ -15,8 +15,7 @@ public class SampleUnit implements Serializable * The type string. * */ - @NotNull - private String tString; + protected String tString; /** * Creates a new SampleUnit. @@ -41,6 +40,7 @@ public class SampleUnit implements Serializable * @return * tString */ + @NotNull public String getTString() { return tString; } From a21fc45f131daac7537655be51d59028a09e4ce1 Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Thu, 19 Apr 2018 10:01:15 +0200 Subject: [PATCH 6/7] Updated default values --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8f530d2f..7b866909 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Then simply include the following code in the POM of the project you wish to gen (required) The path to the file, relative to the project base directory ### outputRelativePath -(optional) Relative path where the generated Java classes will be saved to. Package structure folders will be created relative to this path. +(optional, default: "") Relative path where the generated Java classes will be saved to. Package structure folders will be created relative to this path. ### addTimestampFolder (optional, default: false) Should an extra folder be generated using a timestamp to seperate generations @@ -94,10 +94,10 @@ Then simply include the following code in the POM of the project you wish to gen (optional, default: "") The URI or relative path to the folder/network location containing JSON Schemas ### baseUri -(optional) Base URI for generated Spring controllers. This overrules the baseUri attribute from inside the .raml spec. +(optional, default: "") Base URI for generated Spring controllers. This overrules the baseUri attribute from inside the .raml spec. ### generateUnreferencedObjects -(optional) Determines whether POJOs for unreferenced schemas or data types included in the RAML file should be generated. +(optional, default: false) Determines whether POJOs for unreferenced schemas or data types included in the RAML file should be generated. ### generationConfig (optional) This object contains a map of configuration for the JsonSchema2Pojo generator. The full list of configurable attributes, their description and default values can be found here [GenerationConfig][] From 558dae24d482db79ca83a3f9a73a9481f628101f Mon Sep 17 00:00:00 2001 From: Aleksandar Stojsavljevic Date: Tue, 24 Apr 2018 15:22:52 +0200 Subject: [PATCH 7/7] Updated raml-parser-2 version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f2e73d19..31288ce5 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 1.6.2 3.3.9 0.5.1 - 1.0.21-SNAPSHOT + 1.0.20 1.1.6.RELEASE 2.6