Skip to content

Commit

Permalink
Merge pull request #249 from phoenixnap/gh_248
Browse files Browse the repository at this point in the history
Fixes #248 - Inherited properties not respected
  • Loading branch information
stojsavljevic authored Apr 24, 2018
2 parents 0707675 + 558dae2 commit 0404b46
Show file tree
Hide file tree
Showing 39 changed files with 740 additions and 248 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
target
/iconv.dll
.idea
/.recommenders
/.recommenders
.sts4-cache/
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ Then simply include the following code in the POM of the project you wish to gen
<plugin>
<groupId>com.phoenixnap.oss</groupId>
<artifactId>springmvc-raml-plugin</artifactId>
<version>x.x.x</version>
<version>2.x.x</version>
<configuration>
<ramlPath>{path.to.raml.file}</ramlPath>
<schemaLocation>{path.to.schema.directory||schema.absolute.url}</schemaLocation>
<outputRelativePath>/src/generated</outputRelativePath>
<schemaLocation>{path.to.schema.directory||schema.absolute.url}</schemaLocation>
<outputRelativePath>/src/generated</outputRelativePath>
<addTimestampFolder>false</addTimestampFolder>
<basePackage>com.gen.wow</basePackage>
<baseUri>/api</baseUri>
<generateUnreferencedObjects>true</generateUnreferencedObjects>
<generationConfig>
<includeAdditionalProperties>false</includeAdditionalProperties>
...
</generationConfig>
<seperateMethodsByContentType>false</seperateMethodsByContentType>
<rule>com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerStubRule</rule>
<ruleConfiguration>
</ruleConfiguration>
<generationConfig>
<includeAdditionalProperties>false</includeAdditionalProperties>
...
</generationConfig>
<seperateMethodsByContentType>false</seperateMethodsByContentType>
<rule>com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerStubRule</rule>
<ruleConfiguration>
</ruleConfiguration>
</configuration>
<executions>
<execution>
Expand All @@ -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
Expand All @@ -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][]
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<powermock.version>1.6.2</powermock.version>
<maven.core.version>3.3.9</maven.core.version>
<jsonschema2pojo.version>0.5.1</jsonschema2pojo.version>
<ramlparser2.version>1.0.18</ramlparser2.version>
<ramlparser2.version>1.0.20</ramlparser2.version>
<feign.client.version>1.1.6.RELEASE</feign.client.version>
<codemodel.version>2.6</codemodel.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
Expand All @@ -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));
}
}
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.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");
}
}
Loading

0 comments on commit 0404b46

Please sign in to comment.