Skip to content

Commit 365a98e

Browse files
author
Igor Polevoy
committed
#526 Removing trailing comma when including parents in toJson() method
1 parent 18b5ae7 commit 365a98e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Model.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ public String toString() {
790790
.append("', attributes: ").append(attributes);
791791

792792
if (cachedParents.size() > 0) {
793-
sb.append(", parents: ").append(cachedParents);
793+
sb.append(", parent: ").append(cachedParents);
794794
}
795795

796796
if (cachedChildren.size() > 0) {
@@ -1088,12 +1088,19 @@ private String[] attributeNamesLowerCased() {
10881088
* referential integrity is not enforced in DBMS with a foreign key constraint.
10891089
*
10901090
* @param parentClass class of a parent model.
1091-
* @return instance of a parent of this instance in the "belongs to" relationship.
1091+
* @return instance of a parent of this instance in the "belongs to" relationship if found, ot null if not found.
10921092
*/
10931093
public <P extends Model> P parent(Class<P> parentClass) {
10941094
return parent(parentClass, false);
10951095
}
10961096

1097+
/**
1098+
* Same as {@link #parent(Class)}, with additional argument.
1099+
*
1100+
* @param parentClass class of a parent model
1101+
* @param cache true to also cache a found instance for future reference.
1102+
* @return instance of a parent of this instance in the "belongs to" relationship if found, ot null if not found.
1103+
*/
10971104
public <P extends Model> P parent(Class<P> parentClass, boolean cache) {
10981105
P cachedParent = parentClass.cast(cachedParents.get(parentClass));
10991106
if (cachedParent != null) {

activejdbc/src/test/java/org/javalite/activejdbc/ToJsonSpec.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void shouldKeepParametersCase() {
184184

185185
@Test
186186
@SuppressWarnings("unchecked")
187-
public void shouldIncludeParents() {
187+
public void shouldIncludeParent() {
188188
deleteAndPopulateTables("libraries", "books", "readers");
189189
List<Book> books = Book.findAll().orderBy(Book.getMetaModel().getIdName()).include(Reader.class, Library.class);
190190

@@ -198,5 +198,26 @@ public void shouldIncludeParents() {
198198
Map library = libraries.get(0);
199199
a(library.get("address")).shouldBeEqual("124 Pine Street");
200200
}
201-
}
202201

202+
203+
@Test @SuppressWarnings("unchecked")
204+
public void shouldIncludeParents() {
205+
206+
deleteFromTable("computers");
207+
deleteFromTable("motherboards");
208+
deleteFromTable("keyboards");
209+
210+
populateTable("motherboards");
211+
populateTable("keyboards");
212+
populateTable("computers");
213+
214+
String json = Computer.findAll().include(Motherboard.class, Keyboard.class).toJson(true);
215+
List list = org.javalite.common.JsonHelper.toList(json);
216+
Map m = (Map) list.get(0);
217+
Map parents = (Map) m.get("parents");
218+
List motherboards = (List) parents.get("motherboards");
219+
List keyboards = (List) parents.get("keyboards");
220+
the(motherboards.size()).shouldBeEqual(1);
221+
the(keyboards.size()).shouldBeEqual(1);
222+
}
223+
}

0 commit comments

Comments
 (0)