Skip to content

Commit

Permalink
Fixes #265
Browse files Browse the repository at this point in the history
@Valid annotations added on same getter in both
parent and child objects lead to exception
  • Loading branch information
Aleksandar Stojsavljevic committed Jul 17, 2018
1 parent b6298bc commit ef8cc28
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
23 changes: 23 additions & 0 deletions src/test/resources/ramls/github/issue-265.raml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -250,7 +249,6 @@ public class ProductUpdate
* @return
* groups
*/
@Valid
public List<com.gen.test.model.Group> getGroups() {
if (groups == null) {
return new ArrayList<com.gen.test.model.Group>();
Expand Down
Loading

0 comments on commit ef8cc28

Please sign in to comment.