Skip to content

Commit

Permalink
Fixes #266
Browse files Browse the repository at this point in the history
Collections are always initialized
  • Loading branch information
Aleksandar Stojsavljevic committed Jul 25, 2018
1 parent 757ce95 commit 0d9cef5
Show file tree
Hide file tree
Showing 7 changed files with 693 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -216,6 +217,10 @@ public PojoBuilder withField(String name, String type, String comment, RamlTypeV
jExpression = JExpr._new(narrowedListClass);
}

if (resolveType(Collection.class.getName()).isAssignableFrom(resolvedType) && !Config.getPojoConfig().isInitializeCollections()) {
jExpression = null;
}

// lets ignore this if parent contains it and we will use parent's in
// the constructor
JFieldVar parentField = parentContainsField(this.pojo, name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.phoenixnap.oss.ramlplugin.raml2code.github;

import org.junit.Test;

import com.phoenixnap.oss.ramlplugin.raml2code.plugin.TestConfig;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.GitHubAbstractRuleTestBase;
import com.phoenixnap.oss.ramlplugin.raml2code.rules.Spring4ControllerDecoratorRule;

/**
* @author aleksandars
* @since 2.0.4
*/
public class Issue266RulesTest extends GitHubAbstractRuleTestBase {

@Test
public void verify_collection_initialization_false() throws Exception {
TestConfig.setInitializeCollections(false);
loadRaml("issue-266.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue266-1Spring4ControllerStub");
TestConfig.setInitializeCollections(true);
}

@Test
public void verify_collection_initialization_true() throws Exception {
TestConfig.setInitializeCollections(true);
loadRaml("issue-266.raml");
rule = new Spring4ControllerDecoratorRule();
rule.apply(getControllerMetadata(), jCodeModel);
verifyGeneratedCode("Issue266-2Spring4ControllerStub");
TestConfig.setInitializeCollections(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ public static void setDontGenerateForAnnotation(String dontGenerateForAnnotation
Config.setDontGenerateForAnnotation(dontGenerateForAnnotation);
}

public static void setInitializeCollections(boolean initializeCollections) {
((TestPojoConfig) Config.getPojoConfig()).setInitializeCollections(initializeCollections);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public void setDateType(String dateType) {
public void setTimeType(String timeType) {
this.timeType = timeType;
}

public void setInitializeCollections(boolean initializeCollections) {
this.initializeCollections = initializeCollections;
}
}
26 changes: 26 additions & 0 deletions src/test/resources/ramls/github/issue-266.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#%RAML 1.0
title: Collections are always initialized #266
version: 1.0

types:
Address:
type: object
properties:
city: string
street: string
houseNumber: string
Student:
type: object
properties:
firstName: string
lastName: string
addresses: Address[]

/students:
get:
description: Get all
responses:
200:
body:
application/json:
type: Student[]
Loading

0 comments on commit 0d9cef5

Please sign in to comment.